Homework 2

I decided to make a circuit that involved 3 LEDs (green, yellow, red), 3×330 ohm resistors, 1x10K ohm resistor and LDR. I developed a loop with 2 if statements:

  • if the light sensor read an integer >800, the green LED was blinking,
  • if the integer lied between 600 and 800, the yellow LED blinked,
  • otherwise, the red LED blinked.

The respective LED, based on darkness, blinked every half a second. This circuit might be helpful for someone like me, who rarely notices that the room has become too dark for reading, writing or working. I used the classic green-yellow-red scheme, because people are the most familiar with the implications of these colors in case of warnings. 

Issues:

It took me a while to remember that I had to use 3 wires for properly connecting the LDR, because the serial port monitor gave out 0s, or the numbers seemed random, when LDR was connected to A0 pin and ground only. I found exploring basic LDR circuits on internet very helpful. Once I got it to work and connected LEDs, the serial port monitor displayed the output every half a second – same interval as the blinking lights. As I played with the LED blinking intervals, the output delay monitored the change in blinking. I could not come up with a solution to it, even after switching the “delay” line before or after  the if statements. I also struggled with implementing multiple if statements, where the internet came to help as I explored Arduino language, and “else if” statements. The developed circuit worked otherwise the way that I had intended.

Code:

int greenLight = 11, yellowLight = 12, redLight = 13; //define variables based on which light is connected to which pin
void setup() {
pinMode(redLight, OUTPUT); //red
pinMode(yellowLight, OUTPUT); //yellow
pinMode(greenLight, OUTPUT); //green
Serial.begin(9600);
}

// the loop function runs over and over again forever
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A1);
// print out the value you read:
Serial.println(sensorValue);

//if the value is greater than 800, LED 13 blinks every second
if (sensorValue > 800) {
digitalWrite(greenLight, HIGH);
delay(500);
digitalWrite(greenLight, LOW);
delay(500);
}

//if the value is less than 800 but higher than or equal to 600, LED 12 blinks every second
else if (sensorValue < 800 && sensorValue > 600) {
digitalWrite(yellowLight, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for half a second
digitalWrite(yellowLight, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for half a second
}
//otherwise, LED connected to pin 13 blinks every second
//the other two pins receive LOW
else {
digitalWrite(redLight, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for half a second
digitalWrite(redLight, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for half a second
}

delay(1); // delay in between reads for stability
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *