Hi I'm very much a beginner in the world of Arduino and I just got my Arduino Uno, I wanted to try something out, so I found some instructions on a traffic light. Pretty simple, but I wanted to ad a pedestrian crossing button. However, when I did that, without touching the button, after three cycles of green yellow red, it would act if I pushed the button. I have decided (from removing components) that the code is the problem. Does anybody know how to fix this?
int red = 10;
int yellow = 9;
int green = 8;
int button = 12;
int pedgreen = 11;
int pedred = 13;
int pedcross;
int count;
void setup(){
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
pinMode(pedgreen, OUTPUT);
pinMode(pedred, OUTPUT);
pinMode(button, INPUT);
}
void loop() {
pedcross = 0;
digitalWrite(pedred, HIGH);
pedcross = 0;
changeLights();
pedcross = 0;
}
void changeLights(){
pedc = 0;
digitalWrite(pedred, HIGH);
digitalWrite(yellow, LOW);
digitalWrite(red, LOW);
digitalWrite(green, HIGH);
count = 40;
while (count > 0){
if (digitalRead(button) == HIGH) {
delay(15);
if (digitalRead(button) == HIGH) {
pedcross = 1;
}
}
count = count - 1;
delay(100);
}
digitalWrite(yellow, HIGH);
digitalWrite(red, LOW);
digitalWrite(green, LOW);
count = 20;
while (count > 0){
if (digitalRead(button) == HIGH) {
delay(15);
if (digitalRead(button) == HIGH) {
pedcross = 1;
}
}
count = count - 1;
delay(100);
}
digitalWrite(yellow, LOW);
digitalWrite(red, HIGH);
digitalWrite(green, LOW);
if (pedcross == 1){
pedcross = 0;
pedcycle();
pedcross = 0;
}
else {
delay(5000);
}
}
void pedcycle(){
pedcross = 0;
digitalWrite(pedgreen, HIGH);
digitalWrite(pedred, LOW);
delay(3000);
digitalWrite(pedgreen, LOW);
digitalWrite(pedred, LOW);
delay(500);
digitalWrite(pedred, HIGH);
delay(500);
digitalWrite(pedred, LOW);
delay(500);
digitalWrite(pedred, HIGH);
delay(500);
digitalWrite(pedred, LOW);
delay(500);
digitalWrite(pedred, HIGH);
delay(500);
digitalWrite(pedred, LOW);
delay(500);
digitalWrite(pedred, HIGH);
delay(1500);
pedcross = 0;
}
question from:
https://stackoverflow.com/questions/65903413/i-dont-know-whats-wrong-with-my-code-random-stuff-happens 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…