Concept
For this assignment, I decided to expand upon the concept I previously developed. In the previous assignment, I built a switch that illuminated my drawer upon opening. Given the utilization of sensors in this assignment, I contemplated enhancing the mechanism from a mechanical process to an automated one. The core idea is to enable the LED to deactivate in total darkness, and as the drawer is opened, with increasing light levels, the LED will illuminate.
Code highlight
const int LED_PIN = 9; // the PWM pin the LED is attached to const int LIGHT_SENSOR_PIN = A2; // the analog pin the light sensor is attached to const int BUTTON_PIN = A3; // the pin where the button is connected const int EXTRA_LED_PIN = 10; // the pin for the additional LED int brightness = 0; // how bright the LED is int ledState = LOW; // initial LED state is off int lastButtonState = LOW; // previous button state void setup() { pinMode(LED_PIN, OUTPUT); pinMode(EXTRA_LED_PIN, OUTPUT); pinMode(BUTTON_PIN, INPUT); Serial.begin(9600); } void loop() { int light_value = analogRead(LIGHT_SENSOR_PIN); brightness = map(light_value, 0, 1023, 0, 255); Serial.println(brightness); analogWrite(LED_PIN, brightness); // 0-255 int buttonState = digitalRead(BUTTON_PIN); if (buttonState == HIGH && lastButtonState == LOW) { // Toggle the LED state ledState = (ledState == LOW) ? HIGH : LOW; // Update the LED state digitalWrite(EXTRA_LED_PIN, ledState); // Update the last button state lastButtonState = HIGH; } else if (buttonState == LOW) { lastButtonState = LOW; } }
To meet the second requirement of obtaining input in a digital format, I incorporated a switch that allows the user to control the light, toggling it on or off as needed. This idea materialized during my reflections on the project when I realized one of the limitations of the initial design – it relied on the room’s illumination to function. Hence, the addition of the switch serves as an effective solution to overcome this constraint.
Reflection
Coming up with an idea for this assignment was a bit difficult but I found the process of revisiting and refining an existing idea very satisfying.
The current version kind of contradicts the intial idea but i think it still works out fine.