0

我是Arduino的新手,寻求帮助我制作的程序是在按下按钮时进行一系列的灯打开和关闭机制是当按钮被按下一次它会闪烁灯,如果再次按下它第二次它将执行运行灯,最后按三次它将执行 2 个闪烁灯和 3 个运行灯。不幸的是,我无法在循环函数中连续运行它。这是我的代码:

int buttonCount = 0;
int pushButton = 2;
int ledState = LOW;
int buttonState = 0;
int prevbuttonState = LOW;
int led[3] = {13,12,11};
void setup(){
    Serial.begin(9600);
    pinMode(pushButton,INPUT_PULLUP);
    for(int i = 0; i < 3; i++){
        pinMode(led[i],OUTPUT);
    }
    Serial.println("Button Count: ");
}

void loop(){
    buttonState = digitalRead(pushButton);
    if(buttonState != prevbuttonState){
        buttonCount++;
        Serial.println(buttonCount);
        sequence(buttonCount);
        if(buttonCount >= 3){
            buttonCount = 0;
        }
    }
        prevbuttonState == buttonState;
}
void sequence(int buttonCount){
            if(buttonCount == 1){
                for(int i = 0; i < 2; i++){
                    digitalWrite(led[0], HIGH);
                    digitalWrite(led[1], HIGH);
                    digitalWrite(led[2], HIGH);
                    delay(500); 
                    digitalWrite(led[0], LOW);
                    digitalWrite(led[1], LOW);
                    digitalWrite(led[2], LOW);
                    delay(500);
                }
            }
            if(buttonCount == 2){
                for(int i = 0; i < 3; i++){
                    for(int count = 0; count < 3; count++){
                        digitalWrite(led[count],HIGH);
                        delay(500);
                        digitalWrite(led[count],LOW);
                        delay(500);
                    }
                }
            }
            if(buttonCount == 3){
                for(int i = 0; i < 2; i++){
                    digitalWrite(led[0], HIGH);
                    digitalWrite(led[1], HIGH);
                    digitalWrite(led[2], HIGH);
                    delay(500); 
                    digitalWrite(led[0], LOW);
                    digitalWrite(led[1], LOW);
                    digitalWrite(led[2], LOW);
                    delay(500);
                }
                for(int i = 0; i < 3; i++){
                    for(int count = 0; count < 3; count++){
                        digitalWrite(led[count],HIGH);
                        delay(500);
                        digitalWrite(led[count],LOW);
                        delay(500);
                    }
                }
            }

}
4

0 回答 0