I'm new to Arduino and I have two devices that I'm trying to control:
- A RGB LED light strip that can change colors
- A light sensor that can detect the ambient light level
What I'd like to do is have the LED strip continuously cycle through a rainbow of colors, and the light sensor will adjust the brightness of the LED strip based on the ambient light level. Here are the two pieces of code by themselves, which I've tested in isolation and confirmed that they work properly:
LED strip:
j = (j + 1) % 256; // current iteration of the light cycle
// set the strip color
for(int i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
// display the new colors and wait 20ms before the next cycle
strip.show();
delay(20);
Light sensor:
sensors_event_t event;
tsl.getEvent(&event); // get a light measurement
strip.setBrightness(event.light); // set the brightness of the LED strip
The problem is that the light sensor takes anywhere from 100ms to 600ms to get a reading, and tsl.getEvent(&event)
is a blocking call, so the end result is that the LED strip updates way too slowly. I would need to run the two side by side so that the light sensor read does not block the LED strip update. Any idea how I can accomplish this?