I'm currently trying to create a traffic simulation project on Arduino, where it can change lights, open a gate, and turn on street lamps based on light. If anybody could help me diagnose why my arduino is behaving this way, that'd be extremely appreciated. Here is my code, if it helps you understand.
//Libraries Used
#include<Servo.h>
//Variable Declaration
int red1 = 10;
int yellow1 = 9;
int green1 = 8;
int red2 = 13;
int yellow2 = 12;
int green2 = 11;
int debug = 3;
int PIR = 5;
int button1 = 2;
int buttonState = 0;
int light = 0;
unsigned long previousTime = 0;
unsigned long currentTime = 0;
int currentState = 0;
int currentPIR = 0;
int pirState = LOW;
Servo servo;
void setup()
{
pinMode(red1, OUTPUT);
pinMode(yellow1, OUTPUT);
pinMode(green1, OUTPUT);
pinMode(red2, OUTPUT);
pinMode(yellow2, OUTPUT);
pinMode(green2, OUTPUT);
pinMode(debug, OUTPUT);
pinMode(PIR, INPUT);
pinMode(button1, INPUT);
servo.attach(4);
servo.write(90);
}
void loop()
{
currentPIR = digitalRead(PIR);
if(currentPIR == 1){
servo.write(180);
}else{
servo.write(90);
}
buttonState = digitalRead(2);
light = analogRead(A0);
if(light > 100){
digitalWrite(debug, HIGH);
}else{
digitalWrite(debug, LOW);
}
if(buttonState == HIGH){
if(currentState == 3){
digitalWrite(debug, HIGH);
currentState++;
}
}
//Using millis() instead of delay(), so program doesn't pause completely.
currentTime = millis();
changeLights();
}
void changeLights(){
if(currentState == 0){
//Red, Green
digitalWrite(red2, LOW);
digitalWrite(red1, HIGH);
digitalWrite(green2, HIGH);
asyncDelay(4000);
}
else if(currentState == 1){
//Red, Yellow
digitalWrite(green2, LOW);
digitalWrite(yellow2, HIGH);
asyncDelay(2000);
}
else if(currentState == 2){
//Red, Red
digitalWrite(yellow2, LOW);
digitalWrite(red2, HIGH);
asyncDelay(750);
}
else if(currentState == 3){
//Green, Red
digitalWrite(red1, LOW);
digitalWrite(green1, HIGH);
asyncDelay(4000);
}
else if(currentState == 4){
//Yellow, Red
digitalWrite(green1, LOW);
digitalWrite(yellow1, HIGH);
asyncDelay(2000);
}
else if(currentState == 5){
//Red, Red
digitalWrite(yellow1, LOW);
digitalWrite(red2, HIGH);
asyncDelay(750);
}
}
void asyncDelay(int delayTime){
if(currentTime - previousTime == delayTime){
currentState++;
previousTime = currentTime;
if(currentState == 6){
currentState = 0;
}
}
}
-
add debugging code to your sketchjsotola– jsotola2020年01月09日 00:36:20 +00:00Commented Jan 9, 2020 at 0:36
1 Answer 1
You should replace:
if(currentTime - previousTime == delayTime){
with
if(currentTime - previousTime >= delayTime){
There is always a chance that, since you are using a servo which is quite intensive with interrupts, that millis()
may skip a beat and that statement can never be true.
-
Thank you! I don't know why I hadn't thought of this before, but thank you for such a simple fix! I will accept your answer as solved in a few minutes when it allows me to, but thank you so much, I've been so confused over this the whole day.Bill Bai– Bill Bai2020年01月09日 00:04:02 +00:00Commented Jan 9, 2020 at 0:04
-
@BillBai You should confirm it fixes it - it's only a suggestion...Majenko– Majenko2020年01月09日 00:05:13 +00:00Commented Jan 9, 2020 at 0:05
-
1yup, I confirmed it worked, and it's been working for a few minutes already - rather than the 20 seconds max as before.Bill Bai– Bill Bai2020年01月09日 00:07:40 +00:00Commented Jan 9, 2020 at 0:07