Timing Light Sequences: Build a Traffic Light Controller with |Arduino UNO|

Write and implement program for traffic light signal in Arduino  First turn on red LED for 2 sec  Then turn off red LED  And turn on yellow LED for 0.5 sec  Then turn off yellow LED   And turn on green LED for 2 sec  Then turn off green LED

Source code

int redled =2;

int yellowled =4;

int greenled =6;

// the setup function runs once when you press reset or power the board

void setup() {

  // initialize digital pin LED_BUILTIN as an output.

  pinMode(2, OUTPUT); //For red LED

  pinMode(4, OUTPUT); //For yellow LED

  pinMode(6, OUTPUT); //For green LED

}

 

// the loop function runs over and over again forever

void loop() {

  digitalWrite(redled, HIGH);   // turn the red LED on

  delay(2000);                       // wait for a second

  digitalWrite(redled, LOW);    // turn the  red LED off

  delay(2000);                       // wait for a second

  digitalWrite(yellowled, HIGH);   // turn the yellow LED on

  delay(500);                       // wait for 0.5 second

  digitalWrite(yellowled, LOW);    // turn the  yellow LED off

  delay(500);                       // wait for 0.5 second

  digitalWrite(greenled, HIGH);   // turn the green LED on

  delay(2000);                       // wait for a second

  digitalWrite(greenled, LOW);    // turn the  green LED off

  delay(2000);                       // wait for a second

}

Result:









Post a Comment

0 Comments