Skip to main content
4 of 4
edited tags
Nick Gammon
  • 38.9k
  • 13
  • 70
  • 126

Adding button which resets to top of void

I'm quite new to Arduino and the programming of the board, but I wanted to have a go at making a traffic light based on an example I found online.

The example supplied a simple layout which I roughly copied with the components I had.

enter image description here

I then created the following code to mimic the traffic lights of my local area.

int red = 13;   int yellow = 12;   int green = 11;

void setup(){
    pinMode(red,OUTPUT);
    pinMode(yellow,OUTPUT);
    pinMode(green,OUTPUT);
}

void loop(){
    changeLights();
}

void changeLights(){
    // red on, other off for 3 seconds
    digitalWrite(green,LOW); 
    digitalWrite(yellow,LOW);
    digitalWrite(red,HIGH); 
    delay(3000);

    // red on, amber on,green off for 3 seconds 
    digitalWrite(green,LOW);
    digitalWrite(yellow,HIGH); 
    digitalWrite(red,HIGH); 
    delay(3000);

    // red off, amber off,green on for 3 seconds 
    digitalWrite(green,HIGH);
    digitalWrite(yellow,LOW); 
    digitalWrite(red,LOW); 
    delay(3000);

    // red off, amber on,green on for 3 seconds 
    digitalWrite(green,HIGH);
    digitalWrite(yellow,HIGH); 
    digitalWrite(red,LOW); 
    delay(3000);
}

What I'm wanting to achieve next is a button which resets the code back to the top of void changeLights. I assume I would be able to do this using a push-to-make switch, but I'm not sure how to incorporate it into my circuit or if it is even possible to reset using this.

Terry
  • 213
  • 2
  • 6