We introduced “NeoPixel” library in our last article to control the full-color LED string or strip lights. We’ll continue using NeoPixel this time.
Below is a program for an LED string with proximity sensor as input:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
#include <Wire.h> #include <RPR-0521RS.h> #include <Adafruit_NeoPixel.h> #define PIN 6 //pin number of the signal terminal #define LED_NUM 50 //number of LEDs RPR0521RS rpr0521rs; Adafruit_NeoPixel ledtape = Adafruit_NeoPixel(LED_NUM, PIN, NEO_GRB + NEO_KHZ800); //if the control IC of the LED tape used is WS2812, the third parameter is NEO_KHZ800 and if WS2811 then NEO_KHZ400 int wait = 200; void setup() { ledtape.begin(); ledtape.show(); //reflects once in all OFF state byte rc; Serial.begin(9600); while (!Serial); Wire.begin(); rc = rpr0521rs.init(); } void loop() { byte rc; unsigned short ps_val; float als_val; byte near_far; Serial.println("============================="); rc = rpr0521rs.get_psalsval(&ps_val, &als_val); //when the sensor value is acquired if (rc == 0) { Serial.print("PS:"); Serial.print(ps_val); Serial.println(); //LEDs light up when approaching if(ps_val > 5){ ps_val = 500 - ps_val; if(ps_val < 0){ ps_val = 0; } simpleLED(ps_val/10); } if (als_val != RPR0521RS_ERROR) { Serial.print("ALS:"); Serial.print(als_val); Serial.println(); } } delay(5); } int LEDtale[10]; //arrangement for the trajectory of LED // //Lights individually // void simpleLED(int delaytime){ uint16_t i, j; j=0; for(i=0; i < ledtape.numPixels(); i++) { ledtape.setPixelColor(i, rotateColor(((i) * 256 / ledtape.numPixels()) & 255)); LEDtale[0] = rotateColor(((i) * 256 * 9/10 / ledtape.numPixels()) & 255); for(j=1; j < 10; j++){ LEDtale[j] = rotateColor(((i) * 256 * (10-j)/10 / ledtape.numPixels()) & 255); uint16_t m = i-j; if(m < 0){ m += 50; } ledtape.setPixelColor(m, LEDtale[j]); } ledtape.show(); for(j=1; j < 10; j++){ uint16_t m = i-j; if(m < 0){ m += 50; } delay(0); ledtape.setPixelColor(m, ledtape.Color(0,0,0)); ledtape.show(); } ledtape.setPixelColor(i, ledtape.Color(0,0,0)); ledtape.show(); } ledtape.show(); delay(delaytime); } //RGB color transition function uint32_t rotateColor(byte WheelPos) { if(WheelPos < 85) { return ledtape.Color(WheelPos * 3, 255 - WheelPos * 3, 0); } else if(WheelPos < 170) { WheelPos -= 85; return ledtape.Color(255 - WheelPos * 3, 0, WheelPos * 3); } else { WheelPos -= 170; return ledtape.Color(0, WheelPos * 3, 255 - WheelPos * 3); } } |
We changed a couple of things in the last program we modified. First, we added the input processing of the proximity sensor. Second, we changed the way the LEDs light up.
The input of the proximity sensor should check the sample sketch from the Sensor Evaluation Kit. The purpose is to handle the numerical value entered from the sensor. If you simply respond to the sensor with an if statement, you can merely turn on / off the LEDs. By passing the value of the sensor to a function that lights the LEDs and changing the way the LEDs light up, it is possible to create more interactive lighting effect.
1 2 3 4 5 |
//LEDs light up when approaching if(ps_val > 5){ ps_val = 500 - ps_val; simpleLED(ps_val/10); //Use the sensor value to light the LEDs } |
simpleLED is a function that lights the LED string, but we are processing it to light the LEDs one by one from start to end of the LED string. This probably isn’t enough, so this time we’re using an array to make the LED tape shine to some extent linearly.
In the array, we keep the lighting method for 10 times before counting from the position of the currently shining LED.
1 |
int LEDtale[10]; //arrangement for the trajectory of LED |
It’s pretty fun to play with this part of the program. Please feel free to customize according to your preference.