The button doesn't work

Hi, I'm new in Arduino, and I am not a native English speaker, so if I don't express well, please don't mind.
The requirement:

  1. Prepare 10 LED lights and divide them into two groups of 5 lights in each group, red, yellow, and green are used as traffic lights to control the vehicles, and the other 2 lights: red and green are used as traffic lights to control pedestrians.

  2. One group controls the East-West lane, and the other group control the North-South lane

  3. The total time from red light to green light (or green light to red light) in each lane is 50 seconds. The time from yellow light to red light is set to 4 seconds

  4. Traffic lights for pedestrian control shall flash quickly to remind pedestrians 10 seconds before the green light turns red

  5. The start mechanism of traffic lights starts after the user presses the button. When the user presses the button again, the traffic lights will be turned off.

The circuit I am using is:

I need when I press the button, execute the condition(), and press again the condition() off.
But when I press the button, it doesn't work. the condition() still works whatever I press it or not. The content of condition() is that I need to control the LED light at different seconds.
I know my code has some bugs, I don't know how do I solve them...:cry:
Can anyone give me some suggestions for this?
the code I am using is:

#include "Timer.h"
Timer t1;
Timer t2;
const int Green1 = 13;
const int Green8 = 12;
const int Green6 = 11;
const int Green4 = 10;
const int Yellow9 = 9;
const int Yellow2 = 8;
const int Red10 = 7;
const int Red7 = 6;
const int BUTTON_PIN =2;
unsigned long myTime;
boolean buttonUp = true;
int buttonState = 0;
 
void setup() {
  Serial.begin(9600);
   pinMode(BUTTON_PIN, INPUT);
   pinMode(Green1, OUTPUT);
   pinMode(Green8 , OUTPUT);
   pinMode(Green6, OUTPUT);
   pinMode(Green4, OUTPUT);
   pinMode(Yellow9, OUTPUT);
   pinMode(Yellow2, OUTPUT);
   pinMode(Red10, OUTPUT);
   pinMode(Red7, OUTPUT);
   
}

void loop() {
  
  Serial.print("Time: ");
  myTime = millis();
  Serial.println(myTime);
  if(digitalRead(BUTTON_PIN)!= HIGH && buttonUp == true){
    condition();
    buttonUp == false;
  }
  else if(digitalRead(BUTTON_PIN) == HIGH &&buttonUp != true){
    buttonUp == true;
  }
   
  t1.update();
  t2.update();
}

void condition(){
  
    if(  myTime>0 && myTime < 46000){
            digitalWrite(Green1,HIGH);
            digitalWrite(Green6,HIGH);
            digitalWrite(Red10,HIGH);
    }
    if(myTime > 40000 && myTime <  50000 ){
            
        t1.oscillate(Green6, 200, LOW);
    }
    if(myTime > 46000&& myTime <=50000){
            digitalWrite(Green1,LOW);
            digitalWrite(Yellow2,HIGH);
          
    }
    if(myTime== 50000){
          digitalWrite(Yellow2,LOW);
          digitalWrite(Green6,LOW);
          digitalWrite(Red10,LOW);
    }   
    if(myTime > 50000 && myTime<96000 ){
            digitalWrite(Green4,HIGH);
            digitalWrite(Green8,HIGH);
            digitalWrite(Red7,HIGH);
    }
    if(myTime>=90000 && myTime <100000 ){
           t2.oscillate(Green4, 200, LOW);
    }
    if(myTime >=96000&& myTime <=100000 ){
            digitalWrite(Green8,LOW);
            digitalWrite(Yellow9,HIGH);
    }
  
}

Use Serial monitor in the IDE and Serial.print in the code to tell what the code is doing, in strategic, important places.

It is difficult to see how your button is wired since it is a 4 pin version in your drawing. When using those types of buttons, it is best practice to wire one corner to ground and the other diagonal corner to an input pin. This avoid the problem of installing the button rotated 90 degrees since one side is tied together. You then declare the pin as INPUT_PULLUP so when not pressed, it reads HIGH and when pressed, it reads LOW.

I would suggest writing a separate test sketch that does nothing but read the button and print the results to the serial monitor. Get that working before building a bigger sketch.

2 Likes

Yes! That does the trick! Bloody hobby stuff...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.