Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

arduino - I don't know what's wrong with my code, random stuff happens

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to pull your input either up or down using a resistor, or you can use the built-in pull-up resistor of the Arduino by initializing the input pin like this:

pinMode(button, INPUT_PULLUP);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...