Button not being read by program at all times

Hi, I am creating a 3 way traffic system with pedestrian lights and a button. So for my program, when I click the button, it should change the timing of the lights, however, nothing really happens when I click on the button.

But if I hold the button, after a certain amount of time, the timing changes and stays like that as long I keep the button pressed. This should not be happening.

What should happen is if I press the button once, the timing interval should quicken so the lights change, and this should happen only once, unless the button is pressed again. I was told by my teacher that something in my code is wrong/missing and that the program is not reading the input from the button at all times, hence why it does work sometimes when holding it down.

I’m not sure why it is doing this, and I was given a suggestion that it could be fixed by having something to do with adding a delay() but I’m not sure how that will work.

Please if someone could help me as this is due tomorrow, it would be greatly appreciated!

  #include <Servo.h> 

#define IRinputAnalogPin 3

int servoPin = 8;
Servo servo;
int servoAngle;
int servoChange = 1; 

int val = 0;
int count = 0; 

//strings
String outputString;

// constants won't change. They're used here to
// set pin numbers:
const int redPin = 10;
const int yellowPin = 11;
const int greenPin = 12;
const int newRedPin = 4;
const int newYellowPin = 5;
const int newGreenPin = 6;
const int pedRedPin = 2;
const int pedGreenPin = 3;
const int buttonPin = 0;
const int streetlightPin = 9;

// variables will change:
int redState = HIGH;
int yellowState = LOW;
int greenState = LOW;
int newRedState = LOW;
int newYellowState = LOW;
int newGreenState = HIGH;
int pedRedState = LOW;
int pedGreenState = HIGH;
int LDR = 1;  //analog pin to which LDR is connected, here we set it to 1 so it means A1
int LDRValue = 0;  //that's a variable to store LDR values
int light_sensitivity = 100;  //This is the approx value of light surrounding your LDR
int buttonState = 0;  // variable for reading the pushbutton status
boolean buttonTiming = false;
unsigned long previousMillis = 0;  // will store last time LED was updated
unsigned long interval = 0;  // interval at which to blink (milliseconds)
unsigned long yourUnsignedLongTimestamp = 0;
void setup() 
{
  // put your setup code here, to run once  
  pinMode(redPin, OUTPUT);
  pinMode(yellowPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(newRedPin, OUTPUT);
  pinMode(newYellowPin, OUTPUT);
  pinMode(newGreenPin, OUTPUT);
  pinMode(pedRedPin, OUTPUT);
  pinMode(pedGreenPin, OUTPUT);
  Serial.begin(9600);          
  pinMode(streetlightPin, OUTPUT);
  pinMode(buttonPin, INPUT);  
  servo.attach(servoPin);        
}

void streetlight()
{
  LDRValue = analogRead(LDR);      //reads the ldr's value through LDR 
  Serial.println(LDRValue);       //prints the LDR values to serial monitor
  delay(50);        //This is the speed by which LDR sends value to arduino
  if (LDRValue <= light_sensitivity) 
  {
    digitalWrite(streetlightPin, HIGH);
  }
    else
    {
      digitalWrite(streetlightPin, LOW);
    }
}

void timing()
{
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH)
  {
    buttonTiming = true;
  }
    if (buttonTiming == true)
    {
      
      if ((newYellowState == HIGH) && (pedRedState == HIGH))
      {
        interval = 5000;
      }
        if ((greenState == HIGH) && (newRedState == HIGH))
        {
          interval = 3000;
        }
    }    
      
  if (buttonTiming == false)
  {
    if (((newYellowState == HIGH) && (pedRedState == HIGH)) || (yellowState == HIGH))
      {
        interval = 1000;
      } 
        else
        {
          interval = 800;
        }
  }
}

void loop() 
{
  streetlight();
  timing();
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) 
  {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    // if the LED is off turn it on and vice-versa:
    if ((redState == HIGH) && (newGreenState == HIGH) && (pedGreenState == HIGH))
    {
      newYellowState = HIGH;
      newGreenState = LOW;
      pedRedState = HIGH;
      pedGreenState = LOW;
    } 
      else if ((newYellowState == HIGH) && (pedRedState == HIGH))
      {
        redState = LOW;
        greenState = HIGH;
        newRedState = HIGH;
        newYellowState = LOW;
      }
        else if ((greenState == HIGH) && (newRedState == HIGH))
        {
          yellowState = HIGH;
          greenState = LOW;
        }
          else if (yellowState == HIGH)
          {
            redState = HIGH;
            yellowState = LOW;
            newRedState = LOW;
            newGreenState = HIGH;
            pedRedState = LOW;
            pedGreenState = HIGH;
            buttonTiming = false;
          }
  }
    // set the LED with the ledState of the variable:
    digitalWrite(redPin, redState);
    digitalWrite(yellowPin, yellowState);
    digitalWrite(greenPin, greenState);
    digitalWrite(newRedPin, newRedState);
    digitalWrite(newYellowPin, newYellowState);
    digitalWrite(newGreenPin, newGreenState);
    digitalWrite(pedRedPin, pedRedState);
    digitalWrite(pedGreenPin, pedGreenState);
    
 {
  // read ir sensor and move servo 
    if (millis()- yourUnsignedLongTimestamp >= 2000UL) {
      yourUnsignedLongTimestamp = millis();
      val = analogRead(IRinputAnalogPin);
    if (val >= 900)
      servo.write(100);
    else
      servo.write (0);
    Serial.print(count);
    Serial.print(" : ");
    Serial.println(val);
    count = count + 1;
}
}
}

Yes during a delay call you aren't reading buttons or doing anything else. Google "Blink Without Delay" and start studying up on how to use millis to handle timing. If it starts to look scary just remember that if you can use a clock to tell how long ago you did something then you already know how to do this.

Delta_G:
Yes during a delay call you aren't reading buttons or doing anything else. Google "Blink Without Delay" and start studying up on how to use millis to handle timing. If it starts to look scary just remember that if you can use a clock to tell how long ago you did something then you already know how to do this.

Thanks for your reply, but will that help my program read my button?

Nah, I just like wasting my time giving bogus advice.

Seriously dude, look up what I told you to look up and see for yourself. Or search this forum for any of the ten million daily threads about the same issue that have been solved the same way. You're not the first person to learn that nothing happens during a delay.

Delta_G:
Nah, I just like wasting my time giving bogus advice.

Seriously dude, look up what I told you to look up and see for yourself. Or search this forum for any of the ten million daily threads about the same issue that have been solved the same way. You're not the first person to learn that nothing happens during a delay.

Sorry, I sent that without actually looking it up, but I did look it up right now. I see that an example of "Blink without delay" uses

unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

and I have also used that. However i noticed that they used const long interval = 1000; while i used unsigned long interval = 0; // interval at which to blink (milliseconds), now does using const or unsigned mean anything? Also should my interval equal something other than 0 since the example used 1000?

0 makes no sense at all. If you want something to happen for 0 milliseconds just don't do it at all.

The unsigned is very important.

Like I said. Go read. There are literally thousands of great tutorials on this topic that explain exactly how it all works. There's no need to write a new one here just special for you cause you can't be bothered to search.

Delta_G:
0 makes no sense at all. If you want something to happen for 0 milliseconds just don't do it at all.

The unsigned is very important.

Like I said. Go read. There are literally thousands of great tutorials on this topic that explain exactly how it all works. There's no need to write a new one here just special for you cause you can't be bothered to search.

I appreaciate your help but not your attitude, sometimes, like my situation right now, it is very stressful to be searching and looking for the right way for it to work because of a lot of other things happening and being time clogged.

You have to understand that we are not free consultants. We’re just volunteers with some spare time that answer questions. The whole point is to teach people programming, and you do that by nudging them into the right direction, and then let them do some research themselves. You don’t do that by writing the code for them.

If you’re looking for the exaxt code for your situation, that’s fine, but then this forum is not the place to ask for it.
If you do want to learn yourself, look up some examples, as Delta_G suggested, and try figuring it out for yourself, this way, you’ll learn a lot.
If you’re really stuck on something, feel free to ask a (concrete) question.

Pieter

Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

...R

sakibur001:
like my situation right now, it is very stressful to be searching and looking for the right way for it to work because of a lot of other things happening and being time clogged.

There’s a pithy little saying that seems apropos here: “Lack of planning on your part does not constitute and emergency on my part”.

OP,
After grasping the millis() skillset, you may like to read up on ‘state-machines’ as traffic light simulations and sequencing can become very complex very quickly.
Thy are a perfect application for stateful control methods.