For my project, I created a photosensor controlled RGB led. As you cover each of the three photosensors either the Red, Green, or Blue value will go down based on which respective photosensor is covered.
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 | #include "Adafruit_NeoPixel.h" #def ine PIXEL_PIN 11 #def ine PIXEL_COUNT 1 #def ine PIXEL_TYPE NEO_RGB + NEO_KHZ 800 #def ine RED_PIN 0 #def ine GRN_PIN 1 #def ine BLU_PIN 2 #def ine VLOWR ( 2.75 * ( 1024.0 / 5.0 )) #def ine VHIGHR ( 3.95 * ( 1024.0 / 5.0 )) #def ine VLOWG ( 2.4 * ( 1024.0 / 5.0 )) #def ine VHIGHG ( 3.72 * ( 1024.0 / 5.0 )) #def ine VLOWB ( 2.4 * ( 1024.0 / 5.0 )) #def ine VHIGHB ( 3.72 * ( 1024.0 / 5.0 )) Adafruit_NeoPixel pixels = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE); void setup() { // Initialize the serial UART at 9600 bits per second. Serial.begin( 9600 ); // Initialize NeoPixels and turn them off. pixels.begin(); lightPixels(pixels.Color( 0 , 0 , 0 )); } void loop() { // Read the voltage on the sensor input. This function returns a value // between 0 and 1023 representing a voltage between 0 and 5 V. int redSensor = analogRead(RED_PIN); int grnSensor = analogRead(GRN_PIN); int bluSensor = analogRead(BLU_PIN); // Compute proportional signals to drive the LEDS by mapping an input range // to the output range. // // The PWM output is scaled from 0 to 255 . // // The input range for a typical photocell with a 5.6 K bias resistor is // centered around 4 volts. This can be verified using a voltmeter to // check the range of inputs on A 0 and adjust the following values. Note // the use of the scaling factor to express voltage in terms of the input // range: int red = constrain(map(redSensor, VLOWR, VHIGHR, 0 , 255 ), 0 , 255 ); int green = constrain(map(grnSensor, VLOWG, VHIGHG, 0 , 255 ), 0 , 255 ); int blue = constrain(map(bluSensor, VLOWB, VHIGHB, 0 , 255 ), 0 , 255 ); Serial. print ( "\nRED: " ); Serial. print ( red ); Serial. print ( "\nGREEN: " ); Serial. print ( green ); Serial. print ( "\nBLUE: " ); Serial. print ( blue ); // Emit PWM signals with a proportional average power; the LEDs will have // variable brightness. The constrain function will ensure the values stay // within the correct limits. lightPixels(pixels.Color( red , green , blue )); // Delay for a short interval to create a periodic sampling rate. delay( 200 ); } void lightPixels(uint 32 _t color) { for (int i = 0 ; i < PIXEL_COUNT; ++i) { pixels.setPixelColor(i, color); } pixels. show (); } |
Leave a Reply
You must be logged in to post a comment.