Creating a circuit with Arduino code

ft traffic light

Hey Fellas!

So this week we learned some cool stuff in Physical Computing class. We went through the basics of coding with Arduino’s language, digital and analog inputs, outputs and a few other things.

For our homework we should come up with a simpleĀ application using digital or analog input and digital output. My idea was to create a traffic light that could be controlled by an analog input.

Then I thought it would be nice if the traffic light could change it’s programming functions according to the time in the day. On day-time it would work normally, with the red, green and yellow lights working. But once it is night-time and it is dark, it would just blink the yellow light meaning the cars wouldn’t really have to stop.

In order to do the day&night thing I used a very basic a light sensitive sensor. So, in the end I had 2 analog sensors.

This is my final result:

I guess you could say that all roads lead to Arduino.

This is how the breadboard and Arduino’s circuit looked like before connecting the external wires and LEDs:

wiring beforelight sensor

The wiring inside the traffic light:

hidden wires

The boards after plugging the remaining wires:

circuit traffic light

The schematics of the circuit:

schematics

And this is the code:

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode (3, OUTPUT);
pinMode (4, OUTPUT);
pinMode (5, OUTPUT);

}

void loop() {
// put your main code here, to run repeatedly:

int potValue;
int lightButton;

potValue = analogRead(A5);
lightButton = analogRead(A4);

Serial.println(lightButton);

// 0 – 1023
if (lightButton > 100) {
// during the daytime
if (potValue < 341 ) {
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
}
else if (potValue > 341 && potValue < 682) {
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
}
else if (potValue > 682) {
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
}

}
else {
// night time
// blink
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
delay (500);
digitalWrite(4, HIGH);
delay (500);
}

}

 

 

 

Leave a Reply

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