Saturday, July 30, 2011

Pedestrian Interactive Traffic Light using Arduino UNO

This is a pedestrian interactive traffic light made using Arduino UNO. This is project 4 from the manual that can be found at this link.

Photo of circuit:

IMG_20110730_142344

The code is as follows:

int carRed = 12;
int carYellow = 11;
int carGreen = 10;
int pedRed = 9;
int pedGreen = 8;
int button = 2;
int crossTime = 5000;
unsigned long changeTime;

int x;

void setup(){
  pinMode(carRed, OUTPUT);
  pinMode(carYellow, OUTPUT);
  pinMode(carGreen, OUTPUT);
  pinMode(pedRed, OUTPUT);
  pinMode(pedGreen, OUTPUT);
  pinMode(button, INPUT);
  digitalWrite(carGreen, HIGH);
  digitalWrite(pedRed, HIGH);

}

void loop(){
  int state = digitalRead(button);
  if (state == HIGH && (millis() - changeTime) > 5000){
    changeLights();
  }
}
void changeLights(){

  digitalWrite(carGreen, LOW);
  digitalWrite(carYellow, HIGH);
  delay(2000);
  digitalWrite(carYellow, LOW);
  digitalWrite(carRed, HIGH);
  delay(1000);
  digitalWrite(pedRed, LOW);
  digitalWrite(pedGreen, HIGH);
  delay(crossTime);
  for(int x=0; x<10; x++){
    digitalWrite(pedGreen, HIGH);
    delay(250);
    digitalWrite(pedGreen, LOW);
    delay(250);

  digitalWrite(pedRed, HIGH);
  delay(500);
  digitalWrite(carYellow, HIGH);
  digitalWrite(carRed, LOW);
  delay(1000);
  digitalWrite(carGreen, HIGH);
  digitalWrite(carYellow, LOW);
  changeTime = millis();

}

In human language, the following describes what the traffic light was doing:

1. Traffic light waits for user input. The time since the circuit was turned on was continuously logged.
2. If button is pushed, and the last light change was more than 5 seconds ago (determined by subtracting the time recorded at end of last light change and time since circuit was turned on), the yellow light turns on for 2 seconds.
3. Then the red light turns on.
4. After 1 second, the green light for pedestrian turns on for 10 seconds.
5. Pedestrian green light starts blinking 10 times after that.
6. Pedestrian red light turns on.
7. After 0.5 seconds, yellow lights for cars turn on for 1 second.
8. Yellow and red lights for cars turn off, and green light turns on.
9. Traffic light listens for user input. Time since circuit was turned on was recorded at this point.

1 comment:

You must be bored for you to come all the way to comment on my boring posts.

You should be clear that there're some other things that are more worthy to waste your online time on rather than commenting on someone's blog ya noe... Like DL stuff, read news, play games, flirt around...

But for you to be willing enough to comment must mean a lot to you. Afterall you could've picked up a chick/dude with the time spent commenting here.

So wutcha doin reading these stuff here at this commenting section that I wrote out of boredom? Comment!

Wanna flame? Flame then! It's ok to flame online as long as it stays online...