Pages: [1]   Go Down
Author Topic: Traffic light circuit help?  (Read 507 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello all,
I made a double traffic light circuit that I learned how to make (I am a beginner) using the Blink example. The coding works perfectly. However, I now want to incorporate a button into the circuit that will turn both lights to yellow, then red. Here is the code:
Code:
int button =2;
int red =9;
int yellow =10;
int green =11;
int red2 =5;
int yellow2 =6;
int green2 =7;
int buttonState =0;
void setup() {
  pinMode(red, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(red2, OUTPUT);
  pinMode(yellow2, OUTPUT);
  pinMode(green2, OUTPUT);
  pinMode(button, INPUT);
}
void loop() {
  buttonState = digitalRead(button);
  if (buttonState == HIGH) {
    digitalWrite(yellow, HIGH);
    digitalWrite(yellow2, HIGH);
    digitalWrite(green, LOW);
    digitalWrite(green2, LOW);
    digitalWrite(red, LOW);
    digitalWrite(red2, LOW);
    delay(1000);
    digitalWrite(yellow, LOW);
    digitalWrite(yellow2, LOW);
    digitalWrite(red, HIGH);
    digitalWrite(red2, HIGH);
    delay(3000);
  }
  else {
  digitalWrite(red, HIGH);
  digitalWrite(yellow,LOW);
  digitalWrite(green, LOW);
  digitalWrite(red2, LOW);
  digitalWrite(yellow2, LOW);
  digitalWrite(green2, HIGH);
  delay(5000);
  digitalWrite(yellow2, HIGH);
  digitalWrite(green2,LOW);
  delay(1000);
  digitalWrite(red, LOW);
  digitalWrite(green, HIGH);
  digitalWrite(green2, LOW);
  digitalWrite(yellow2, LOW);
  digitalWrite(red2, HIGH);
  delay(3000);
  digitalWrite(green,LOW);
  digitalWrite(yellow,HIGH);
  delay(1000);
  }
}
I use the 5v pin to supply power across my button switch and a 10k pull down resistor. However, when I push the button, the lights don't turn yellow then red as I wanted. Nothing happens except for the 'else' part continually loops. Is there a way to make it so when I push the button, the circuit will immediately go to the 'if' part instead of continually looping the 'else'?
Logged

Offline Offline
Edison Member
*
Karma: 57
Posts: 2078
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Could you use a wire and connect pin 2 to 5V and to GND ?
I can't see what the problem is with the code, so I want to be sure that pin 2 is high and low.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Erdin, I think it's that aj88 wants to be able to interrupt the delays as to get an immediate output.
To actually do what you are wanting to do you need to look into threading. That is the only way I know how to get rid of the Delay problem.
If it's just that it's always looping to Else
I think something like this might help.
Code:
char oldVal='2';
int button =2;
int red =9;
int yellow =10;
int green =11;
int red2 =5;
int yellow2 =6;
int green2 =7;
int buttonState =0;
void setup() {
  pinMode(red, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(red2, OUTPUT);
  pinMode(yellow2, OUTPUT);
  pinMode(green2, OUTPUT);
  pinMode(button, INPUT);
}
  void lightHIGH()
  {
    digitalWrite(yellow, HIGH);
    digitalWrite(yellow2, HIGH);
    digitalWrite(green, LOW);
    digitalWrite(green2, LOW);
    digitalWrite(red, LOW);
    digitalWrite(red2, LOW);
    delay(1000);
    digitalWrite(yellow, LOW);
    digitalWrite(yellow2, LOW);
    digitalWrite(red, HIGH);
    digitalWrite(red2, HIGH);
    delay(3000);
  }
  void lightLOW()
  {
  digitalWrite(red, HIGH);
  digitalWrite(yellow,LOW);
  digitalWrite(green, LOW);
  digitalWrite(red2, LOW);
  digitalWrite(yellow2, LOW);
  digitalWrite(green2, HIGH);
  delay(5000);
  digitalWrite(yellow2, HIGH);
  digitalWrite(green2,LOW);
  delay(1000);
  digitalWrite(red, LOW);
  digitalWrite(green, HIGH);
  digitalWrite(green2, LOW);
  digitalWrite(yellow2, LOW);
  digitalWrite(red2, HIGH);
  delay(3000);
  digitalWrite(green,LOW);
  digitalWrite(yellow,HIGH);
  delay(1000);
  }
void loop() {
  buttonState = digitalRead(button);
  
  if (buttonState == HIGH) {
      if(oldVal!='1')
lightHIGH();
oldVal='1';
  }
  else if (buttonState == LOW) {
    if(oldVal!='0')
  lightLOW();
  oldVal='0';
  }
}

...I don't know enough about threading on Arduino's yet but a good resource seems to be located at: http://playground.arduino.cc/Code/TimedAction#Example
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have 5v at one side of the button, pin 2 and the 10k resistor to ground is on the other side.


Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 354
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Could you use a wire and connect pin 2 to 5V and to GND ?
I can't see what the problem is with the code, so I want to be sure that pin 2 is high and low.

Poor phrasing, that!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Erdin, I think it's that aj88 wants to be able to interrupt the delays as to get an immediate output.
To actually do what you are wanting to do you need to look into threading. That is the only way I know how to get rid of the Delay problem.
If it's just that it's always looping to Else
I think something like this might help.
Code:
char oldVal='2';
int button =2;
int red =9;
int yellow =10;
int green =11;
int red2 =5;
int yellow2 =6;
int green2 =7;
int buttonState =0;
void setup() {
  pinMode(red, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(red2, OUTPUT);
  pinMode(yellow2, OUTPUT);
  pinMode(green2, OUTPUT);
  pinMode(button, INPUT);
}
  void lightHIGH()
  {
    digitalWrite(yellow, HIGH);
    digitalWrite(yellow2, HIGH);
    digitalWrite(green, LOW);
    digitalWrite(green2, LOW);
    digitalWrite(red, LOW);
    digitalWrite(red2, LOW);
    delay(1000);
    digitalWrite(yellow, LOW);
    digitalWrite(yellow2, LOW);
    digitalWrite(red, HIGH);
    digitalWrite(red2, HIGH);
    delay(3000);
  }
  void lightLOW()
  {
  digitalWrite(red, HIGH);
  digitalWrite(yellow,LOW);
  digitalWrite(green, LOW);
  digitalWrite(red2, LOW);
  digitalWrite(yellow2, LOW);
  digitalWrite(green2, HIGH);
  delay(5000);
  digitalWrite(yellow2, HIGH);
  digitalWrite(green2,LOW);
  delay(1000);
  digitalWrite(red, LOW);
  digitalWrite(green, HIGH);
  digitalWrite(green2, LOW);
  digitalWrite(yellow2, LOW);
  digitalWrite(red2, HIGH);
  delay(3000);
  digitalWrite(green,LOW);
  digitalWrite(yellow,HIGH);
  delay(1000);
  }
void loop() {
  buttonState = digitalRead(button);
  
  if (buttonState == HIGH) {
      if(oldVal!='1')
lightHIGH();
oldVal='1';
  }
  else if (buttonState == LOW) {
    if(oldVal!='0')
  lightLOW();
  oldVal='0';
  }
}

...I don't know enough about threading on Arduino's yet but a good resource seems to be located at: http://playground.arduino.cc/Code/TimedAction#Example
That is what I want, but when I tried your code, it stopped at the end of lightLow. Thank you for your help, I will look into that!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Another thing that would work as pointed out by one of the moderators, Nick Gammon, in another post: http://www.gammon.com.au/blink
Logged

Pages: [1]   Go Up
Jump to: