For this week’s assignment, I have implemented a small lighting system that interfaces both analog and digital sensors.
The task was to use at least one analog sensor and one digital sensor to control two LEDs differently—one in a digital fashion and the other in an analog fashion. For this, I used:
- Analog sensor: A photoresistor to sense ambient light levels.
- Digital sensor: A tactile button switch to change modes.
- LEDs: A standard LED and an RGB LED for digital and analog control, respectively.
I have used the tactile button and the photoresistor to control the RGB LED in two distinct modes: Night Mode and Party Mode.
- Night Mode: The RGB LED’s behavior is influenced by the ambient light level detected by the photoresistor. When it’s dark, the RGB LED cycles through colors at a slower pace, providing a soothing nighttime light effect.
- Party Mode: Activated by pressing the tactile button, this mode turns on an additional LED indicator to show that Party Mode is active. In this mode, the RGB LED cycles through colors more quickly, creating a dynamic and festive atmosphere.
The tactile button toggles between these two modes, allowing the RGB LED to adapt its behavior based on the selected mode and the ambient light conditions.
The button debouncing was tricky for me. Button debouncing is basically managing the button input to toggle between normal and party modes without accidental triggers.
int reading = digitalRead(A1); if (reading != lastButtonState) { lastDebounceTime = millis(); // Reset the debouncing timer } if ((millis() - lastDebounceTime) > debounceDelay) { if (reading != buttonState) { buttonState = reading; if (buttonState == HIGH) { partyMode = !partyMode; digitalWrite(8, partyMode ? HIGH : LOW); } } } lastButtonState = reading;
Another challenging part was figuring out the color change minimally. I used bitwise operations for this.
if (!partyMode && sensorValue >= 400) { digitalWrite(10, LOW); digitalWrite(11, LOW); digitalWrite(12, LOW); } else { int interval = partyMode ? partyInterval : normalInterval; if (millis() - lastChangeTime >= interval) { lastChangeTime = millis(); rgbLedState = (rgbLedState + 1) % 8; // Determine which LEDs to turn on based on the current state digitalWrite(10, (rgbLedState & 0x01) ? HIGH : LOW); // Red digitalWrite(11, (rgbLedState & 0x02) ? HIGH : LOW); // Green digitalWrite(12, (rgbLedState & 0x04) ? HIGH : LOW); // Blue } }