Concept:
For this week, our task was to get information from at least one analog sensor and at least one digital sensor (switch), and use this information to control at least two LEDs, one in a digital fashion and the other in an analog fashion, in some creative way.
My project is called “The Cookie Jar”, and it’s inspired by my love for chocolate chip cookies.
After recklessly spending my savings on this bucket of cookies, I decided to base my project on it. The idea was to turn a light on every time I opened the lid of this cookie bucket. To do this, I had to code the photoresistor in a way that it could take the difference between the light outside and the light inside. However, the problem relied on the fact that turning on the LED also generated light by itself, so I had to adapt the constraints of it in a way that would work properly. Also, I added a switch to turn on and off manually the other light inside, just in case someone wants to sneak a cookie in the middle of the night.
Breadboard and connections:
Demonstration:
Code:
void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(5,OUTPUT); pinMode(9,OUTPUT); pinMode(2,INPUT); pinMode(A2,INPUT); } bool lightsOn = false; void loop() { // put your main code here, to run repeatedly: int sensorValue = analogRead(A2); int switchValue = digitalRead(2); if(switchValue == 1){ lightsOn = !lightsOn; delay(500); } digitalWrite(9,lightsOn); // The brightness is the difference between inside and outside, but reversed (more light if the difference is low) int brightness = 255-(620-sensorValue); if(brightness<0){ brightness = 0; } if(brightness>255){ brightness = 255; } analogWrite(5,brightness); Serial.println(sensorValue); }
Challenges:
The biggest challenge I faced was the shape and the fact that I had to connect the arduino UNO inside with the lid closed. I fixed this problem by reluctantly making a hole in my cookie bucket. I opened it just enough so it could fit the USB connector, which allowed me to insert the circuit inside even with the lid closed.