Coding Help - Traffic Light

hi guys, beginner coder here.

I’m having difficulty coding the pedestrian stop light on my Traffic Light circuit.

The main function is that the Motor’s speed corresponds with the LED light, so basically when the light is green the motor moves faster, amber makes the motor slow down, and red makes the motor stop.

I got that to work but I also wanted to add a pedestrian crosswalk.

So when the button is pushed, there will be a slight delay for the Crosswalk light to be Green, and then the Traffic light being red with the motor stopped. However, when I tried programming it myself with If statements, the button doesn’t work and it doesn’t turn red for the traffic light and green for the crosswalk light.

CIRCUIT:

CODE:

int led_red = 0; // the red LED is connected to Pin 0 of the Arduino
int led_yellow = 1; // the yellow LED is connected to Pin 1 of the Arduino
int led_green = 2; // the green LED is connected to Pin 2 of the Arduino
int motorpin_1 = 6; // pin 1 is connected to Pin 6 of the arduino
int motorpin_2 = 7; // pin 2 is connected to pin 7 of the arduino
int button1 = 12;
int pad_go = 5;
int pad_stop = 4;


void setup() {
  // set up all the LEDs as OUTPUT
  pinMode(led_red, OUTPUT);
  pinMode(led_yellow, OUTPUT);
  pinMode(led_green, OUTPUT);
  pinMode(motorpin_1, OUTPUT);
  pinMode(motorpin_2, OUTPUT);
  pinMode(button1, INPUT);
  pinMode(pad_go, OUTPUT);
  pinMode(pad_stop, OUTPUT);
  
  
}

void loop(){
   
  
  if (button1 == 0){
  
   digitalWrite(led_green, LOW);
   digitalWrite(led_yellow, HIGH);
 
   digitalWrite(led_yellow, LOW);
   digitalWrite(led_red, HIGH);
  
   digitalWrite(pad_stop, LOW);
   digitalWrite(pad_go, HIGH);
   
  
  }
  

 else{ 
   
    
  digitalWrite(led_red, LOW); 
  digitalWrite(led_yellow, LOW);
  digitalWrite(led_green, HIGH);
  digitalWrite(pad_go, LOW);
  digitalWrite(pad_stop, HIGH);
  rotateLeft(500,10000);
     // wait 10 seconds

  
  // turn the yellow LED on and the other LEDs off
  digitalWrite(led_red, LOW);   
  digitalWrite(led_yellow, HIGH);
  digitalWrite(led_green, LOW);
  digitalWrite(pad_go, LOW);
  digitalWrite(pad_stop, HIGH);
  rotateLeft(250,6000);

  

  
  // turn the red LED on and the other LEDs off
  digitalWrite(led_red, HIGH);  
  digitalWrite(led_yellow, LOW);
  digitalWrite(led_green, LOW);
  rotateLeft(0,10000);
  delay(2000); // delay 2 seconds before going green 
  digitalWrite(pad_go, HIGH);
  digitalWrite(pad_stop, LOW);
   
 }
  
  }

void rotateLeft(int speedofRotate, int length){
  analogWrite(motorpin_1, speedofRotate);
  digitalWrite(motorpin_2, LOW);
  delay(length);
  digitalWrite(motorpin_1, LOW);
  
}

 
//pnsy

*attached is also the notepad file of the code

Thanks!

codetl.txt (1.96 KB)

  pinMode(button1, INPUT);

I'm not going to study your fritzing diagram to see how the switch is actually wired, because you didn't bother producing a decent schematic image. You could use different color lines, and you CAN make them not cross each other (at least not at other than right angles).

You can't connect a motor directly to an Arduino.

int button1 = 12;

  if (button1 == 0){

12 is not 0 in my universe. Is it in yours?

Okay, so I added an H-bridge to control the motor.

The pedestrian and traffic light work.
However, my program is still not reading my If statement when I push the button.
I believe I connected the switch correctly on the breadboard.

Circuit:


Code:

const int led_red = 1; // the red LED is connected to Pin 0 of the Arduino
const int led_yellow = 2; // the yellow LED is connected to Pin 1 of the Arduino
const int led_green = 3; // the green LED is connected to Pin 2 of the Arduino
const int motorpin_1 = 12; // H-bridge leg 1 is connected to Pin 12 of arduino
const int motorpin_2 = 11; // H-bridge leg 2 is connected to Pin 11 of arduino
const int enablePin = 13; // H-bridge enable is connected to Pin 13
const int button_1 = 6;

const int crosswalkGO = 5;
const int crosswalkSTOP = 4;

int buttonState = 0;

void setup() {
  //OUTPUTS
  pinMode(led_red, OUTPUT);
  pinMode(led_yellow, OUTPUT);
  pinMode(led_green, OUTPUT);
  pinMode(motorpin_1, OUTPUT);
  pinMode(motorpin_2, OUTPUT);
  pinMode(enablePin, OUTPUT);
  pinMode(crosswalkGO, OUTPUT);
  pinMode(crosswalkSTOP, OUTPUT);
  //INPUTS
  pinMode(button_1, INPUT);
  
  digitalWrite(enablePin, HIGH);
 

 
  
  
}

void loop(){
  
  buttonState = digitalRead(button_1);
  
  if (buttonState == HIGH){
   digitalWrite(led_red, LOW);
   digitalWrite(led_yellow, HIGH);
   digitalWrite(led_green, LOW);
   rotateRight(250, 6000);
   
   digitalWrite(led_red, HIGH);
   digitalWrite(led_yellow, LOW);
   digitalWrite(led_green, LOW);
   rotateRight(0,10000);
      
   digitalWrite(led_red, LOW);
   digitalWrite(led_yellow, LOW);
   digitalWrite(led_green, HIGH);
   rotateRight(500,10000);
   
  
  }
  

 else { 
   
    
  digitalWrite(led_red, LOW); 
  digitalWrite(led_yellow, LOW);
  digitalWrite(led_green, HIGH);
  digitalWrite(crosswalkGO, LOW);
  digitalWrite(crosswalkSTOP, HIGH);
  rotateRight(500,10000);
     

  
  // turn the yellow LED on and the other LEDs off
  digitalWrite(led_red, LOW);   
  digitalWrite(led_yellow, HIGH);
  digitalWrite(led_green, LOW);
  digitalWrite(crosswalkGO, LOW);
  digitalWrite(crosswalkSTOP, HIGH);
  rotateRight(250,6000);

  

  
  // turn the red LED on and the other LEDs off
  digitalWrite(led_red, HIGH);  
  digitalWrite(led_yellow, LOW);
  digitalWrite(led_green, LOW);
  digitalWrite(crosswalkGO, HIGH);
  digitalWrite(crosswalkSTOP, LOW);
  rotateRight(0,10000);
  delay(2000); // delay 2 seconds before going green 
 }
  
  }

void rotateRight(int speedofRotate, int length){
  analogWrite(motorpin_1, speedofRotate);
  digitalWrite(motorpin_2, LOW);
  delay(length);
  digitalWrite(motorpin_1, LOW);
  
}

Thanks again

any help is appreciated.

You're making the mistake of throwing together a complex circuit and program all at once. The way to approach a project like this is incrementally. You start with just the button circuit and a simple sketch that reads the button and provides some output. Next, you do the same with an LED, then the same with the motor. Once you have verified that you know how to use each of the individual components of the project, and that each is working correctly, you can start combining them together, testing as you go along.

Save those simple sketches you wrote along the way to use for tests later in case you run into problems.

You can often find the simple sketches written for you in the File > Examples menu. However, make sure you always understand what every line of code does, even if you didn't write it. You will need that understanding to complete the final project anyway.

You have your pull-down resistor in the wrong place. It should be positioned in the circuit so that it pulls pin 6 LOW when the button is not pressed. Do you see why your current circuit will not do that?

pert:
You're making the mistake of throwing together a complex circuit and program all at once. The way to approach a project like this is incrementally. You start with just the button circuit and a simple sketch that reads the button and provides some output. Next, you do the same with an LED, then the same with the motor. Once you have verified that you know how to use each of the individual components of the project, and that each is working correctly, you can start combining them together, testing as you go along.

Save those simple sketches you wrote along the way to use for tests later in case you run into problems.

You can often find the simple sketches written for you in the File > Examples menu. However, make sure you always understand what every line of code does, even if you didn't write it. You will need that understanding to complete the final project anyway.

You have your pull-down resistor in the wrong place. It should be positioned in the circuit so that it pulls pin 6 LOW when the button is not pressed. Do you see why your current circuit will not do that?

my bad, i meant to put this as the picture.


This is what you mean by connecting the button to pin 6 right?

That's the same picture as before, and the pull-down resistor is still in the wrong place.

pert:
That's the same picture as before, and the pull-down resistor is still in the wrong place.

this was the old circuit:

this is the circuit that's posted rn:


(the code in my second post is the code that goes with this circuit)

How come the pull-down resistor is still in the wrong place?

thanks

However, my program is still not reading my If statement when I push the button.

You have this ass backwards. The program can not help but EXECUTE the if statement, which may evaluate to false. The program may not see that the switch is/has become pressed, if you haven't wired it correctly.

A program to determine that would be about 10 lines long.

ehpians:
How come the pull-down resistor is still in the wrong place?

The pull-down is correct in the second Fritzing. I was wrong. Sorry for the confusion. However, all the Fritzings except the first one shows the button with only two pins inserted into the breadboard. The other two pins are just sitting in the groove that goes down the center of the breadboard. That certainly won’t work.

I still think my advice to start with a basic button circuit and code to eliminate any unnecessary complexity until you have verified the button is working correctly is the best approach.

It’s a bad idea to use pin 0 and 1 (I know you’re only using pin 1 in the third Fritzing) because these pins are also used for communication with the computer (printing some debug output to the Serial Monitor for example). You have plenty of other pins free so you might as well avoid using the problematic pin 1.

This may or may not help, but I'm a newbie with Arduino as well and I have found it super helpful to put in some Serial.println's here and there to test my code and see what's going on with variables and state changes. It helps when things aren't going right.

Also, the suggestion of incremental steps is very good advice.

jasonwnc:
I have found it super helpful to put in some Serial.println's here and there to test my code and see what's going on with variables and state changes.

K++ for that realisation and suggestion.