CONCEPT:
For this week’s assignment, I made a smiley face with two glowing eyes, where they “wink” in between. One of the LEDs brightness was controlled using a photoresistor, and the other turned on/off using a switch.
Arduino UNO
Breadboard
Photoresistor
10K Ohm resistor
330 Ohm resistor
Switch
LED
Jumper wires
WORKING:
So one of the LEDs are controlled using the photoresistor. It gets brighter when the photoresistor is covered(less light), and vice versa. The other LED is controlled using the switch. The code for it is as follows:
const int button = 2; const int led1 = 13; const int led2 = 9; const int ldr = A0; int state = LOW; //state of LED1 int brightness = 0; //brightness of LED2 int ldrValue = 0; //photoresistor value void setup() { pinMode(button, INPUT_PULLUP); pinMode(led1, OUTPUT); pinMode(led2, OUTPUT); //serial monitor for debugging Serial.begin(9600); } void loop() { if (digitalRead(button) == LOW) { state = HIGH; //turn LED1 on } else { state = LOW; //turn LED1 off } //read photoresistor value and map it to LED2 brightness (0 to 120) ldrValue = analogRead(ldr); brightness = map(ldrValue, 0, 1023, 0, 120); Serial.print("LDR Value: "); Serial.println(ldrValue); //control LED1 using switch (physical input) digitalWrite(led1, state); //control LED2 brightness using analog input analogWrite(led2, brightness); delay(100); //small delay for stability }
CHALLENGES :
The challenging part about this was my biggest careless mistake! I put the LED on oppositely (anode and cathode), so I spent a good 30 minutes trying to figure out what went wrong. I also used the serial monitor to help with debugging.
Hand drawn schematic:
LINK TO VIDEO :
https://youtube.com/shorts/UVZ-qTWX1aE?feature=share
IMPROVEMENTS :
I would like the photoresistor-controlled LED to be better, maybe some other form of coding that makes it more…interesting. I also realised that we wink using our eyelids and not the eyes themselves, but overall, as a form of cartoonish smiley-ness, I’m happy with this project. It was a fresh start after the spring break!