Go Down

Topic: Pushbutton and LEDs? (Read 500 times) previous topic - next topic

qbrod

Hi,
The task for my assignment is to make 2 sets of traffic lights, with one of them including a light for the left turn lane when I push a button, delay for a few seconds, then turn off. I'm not sure I understand why the button isn't working at all (when I use the code below it runs the traffic light program without the turning lane).
I'd really appreciate some help!




Code: [Select]
const int buttonPin = 2; //This allows the button to read the turning light
const int ledPin = 9;
int buttonState = 0;

void setup() {

//This is the first set of lights
pinMode(3,OUTPUT); //First green
pinMode(4,OUTPUT); //First yellow
pinMode(5,OUTPUT); //First red

//This is the second set of lights
pinMode(10,OUTPUT); //Second green
pinMode(11,OUTPUT); //Second yellow
pinMode(12,OUTPUT); //Second red
pinMode(ledPin,OUTPUT); //Turning lane light

//This is the button
pinMode(buttonPin,INPUT); //Button
}

void loop() {

//Here is the code for the button

/*Have not figured out how to integrate this.
 Right now what I have is the turning light integrated automatically,
 so the button doesn't do anything as of yet.
 */

//THIS BELOW IS NOT WORKING
buttonState = digitalRead(buttonPin); //The button reads the turning light
 
  if (buttonState == HIGH) {
  digitalWrite(ledPin,HIGH);
} else {
  digitalWrite(ledPin,LOW);
}


digitalWrite(12,HIGH); //Second red light turns on
delay(1300);

digitalWrite(ledPin,HIGH); //THIS IS WHAT I HAVE FOR NOW FOR THE TURNING PIN
delay(2700);
digitalWrite(ledPin,LOW);
delay(900);

digitalWrite(3,HIGH); //First green light turns on
delay(5000);

digitalWrite(3,LOW); //First green light turns off
digitalWrite(4,HIGH); //First yellow light turns on
delay(3000);

digitalWrite(4,LOW); //First yellow light turns off
digitalWrite(12,LOW); //Second red light turns off
digitalWrite(5,HIGH); //First red light turns on
delay(1000);

digitalWrite(10,HIGH); //Second green light turns on
delay(5000);

digitalWrite(10,LOW); //Second green light turns off
digitalWrite(11,HIGH); //Second yellow light turns on
delay(3000);
digitalWrite(11,LOW); //Second yellow light turns off
digitalWrite(5,LOW); //First red light turns off

}

dwightthinker

pinMode(buttonPin,INPUT); //Button

Might want
pinMode(buttonPin,INPUT_PULLUP); //Button
if you don't have an external pullup or down resistor.
Dwight

larryd

#2
Sep 16, 2015, 01:27 am Last Edit: Sep 16, 2015, 01:27 am by LarryD
You may also want to consider using BlinkWithoutDelay techniques to get rid of all those delays (which are blocking your code).
Also study State Machine programming.
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html
No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

Grumpy_Mike

Look at your code. How often is the button looked at? Only at one point in the whole long sequence.

dwightthinker

Make a chart, defining what all the states are.
For each state define how you might exit that state to get to another state,
such as, it it valid to go from a red to a green someplace else without a yellow cycle.
Put the chart in your header comments.
For a large number of states look up switch/case. I think you may find it useful.
Try to write code that doesn't need redundant comments.

avoid redundant comments like:
digitalWrite(10,LOW); //Second green light turns off

better:
digitalWrite(Green2, off );

or maybe

digital Write(NSGreen, off);

Use const at the top of the code to define things like off/on and Green2.
That way, if you find that the LEDs are wired for LOW being on instead of
HIGH, you can quickly change it at the head of the code.

Try to write comments about what you are doing in English, such as:
" I'm recognizing the car in the turn lane and stopping the traffic
before turning on the turn light "

Dwight



Go Up