Adventures with Arduino Part 8

As I recieved a 2nd Arduino Uno board this week.  Today I had a go at making the LED traffic light sequence project,  the same as I made on the Pi a while back.

This went fairly well, the final code looks like this.

/*
  traffic lights code
  Turns on an LEDs in a traffic light sequence,  based on blink and modified .
 
  This example code is in the public domain.
 */
 
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int ledred = 4;  // RED LED
int ledyellow = 7;  // YELLOW LED
int ledgreen = 8;;  // GREEN LED
 
// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(ledred, OUTPUT);
  pinMode(ledyellow, OUTPUT);
  pinMode(ledgreen, OUTPUT);  
}
 
// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(ledred, HIGH);   // turn the RED LED on (HIGH is the voltage level)
  delay(5000);               // wait for a second
  digitalWrite(ledyellow, HIGH);    // turn the YELLOW LED ON by making the voltage HIGH
  delay(1000);               // wait for a second
  digitalWrite(ledred, LOW);   // turn the RED OFF on (LOW is the voltage level)
  digitalWrite(ledyellow, LOW);    // turn the YELLOW LED OFF by making the voltage LOW
  digitalWrite(ledgreen, HIGH);    // turn the GREEN LED ON by making the voltage high
  delay(1000);               // wait for a second
  digitalWrite(ledgreen, HIGH);    // turn the GREEN LED OFF by making the voltage HIGH
  delay(5000);
  digitalWrite(ledgreen, LOW);    // turn the GREEN LED OFF by making the voltage LOW
    digitalWrite(ledyellow, HIGH);    // turn the YELLOW LED ON by making the voltage HIGH
  delay(1000);               // wait for a second
  digitalWrite(ledyellow, LOW);    // turn the YELLOW LED OFF by making the voltage LOW
}

It takes a while to get the timing of the sequence correct, or near enough.

Notes:

Take care when doing this as it is easy to miss some code out and end up with odd results,  I am not going to tell you what happens if you make errors,  however look at this section of code.

// initialize the digital pin as an output.
  pinMode(ledred, OUTPUT);
  pinMode(ledyellow, OUTPUT);
  pinMode(ledgreen, OUTPUT);

Remove two of those lines and see what happens when you re-run the code.

Programming is about learning and problem solving. You learn more by making errors, learning from the results.    As you learn more you are then able to link symptoms to cause, which should make it easier to debug your code.   In fact this can be applied to other subjects.

Traffic light sequences

You can find a link  to some  info on the UK traffic light sequence

Anyway this is now running on both the Pi and Arduino.

My next project is to use a humidity / temperature sensor to display information on an LCD display then after that myself and Tom were looking at using the sensor to turn a fan on / off.