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;
}
}
}