15 Light Sensor Sets Pixels

Set the LED Pixel Color with Light Sensor

/* Use the light sensor to control the color of the LED rainbow pixels */ #include "LEDStrip.h" //The LEDStrip.h library gives your code access to special functions exclusive to the LED Pixels LEDStrip pixels = LEDStrip(4, 13, 12); //Set up the pixels by defining how many pixels (4) and where the pixels connect to the microchip void setup() { pinMode(A2,INPUT); //light sensor } void loop() { int color = analogRead(A2); //create variable called color that is an integer (whole number) with a starting value equal to the reading the light sensor color = map(color,0,1023,0,299); //map the analogReadings (0-1023) to the color values the LED pixels can show (0-299) //Now the light reading of 0 = 0 on the color scale. light reading 1023 = 299 color. light reading 512 = 150 color. pixels.setPixel(pixels.ALL,color); //set all pixels to a color determined by the 'color' variable pixels.draw(); //send the setPixel data to Optimus Prime } // (c) 2024 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 

Walkthrough Video

Watch the video for a line-by-line explanation of how the example program works. Then you'll be ready to make some changes of your own!

LED Pixel Color Spectrum


Use this spectrum to estimate the color values you'll need for your 'setPixel' arguments to achieve your favorite color!


This graphic represents how some values from the light sensor will be 'mapped' onto the pixel colors.

 

Challenges

Can you complete the challenges? Change the code in your code editor above. Upload your code to see the effect when you're finished. Complete a challenge? Check it off the list!


TRANSFORMERS and HASBRO and all related trademarks and logos are trademarks of Hasbro, Inc. ©2024 Hasbro.