Arduino 大型项目25 个 LED如何使用按钮来循环切换 3 个不同的功能?

问题描述

我正在尝试使用按钮循环浏览 3 个功能我有一个 arduino mega、25 个 LED 和一个触觉开关。这是我现在拥有的代码,但它没有做任何事情。

    //initializing LEDS

#define PIN_COUNT    25
int pins[] = {2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26};
#define BUTTON_PIN   27

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // prevIoUs state of the button

void setup() {
  // put your setup code here,to run once:
  for (int i=0; i<PIN_COUNT; i++){    
   pinMode(pins[i],OUTPUT);            
  }
pinMode(BUTTON_PIN,INPUT_PULLUP);
}

void loop() {
// read the pushbutton input pin:
 buttonState = digitalRead(BUTTON_PIN);

 // compare the buttonState to its prevIoUs state
 if (buttonState != lastButtonState) {
   // if the state has changed,increment the counter
   if (buttonState == HIGH) {
     // if the current state is HIGH then the button went from off to on:
     buttonPushCounter++;
     Serial.println("on");
     Serial.print("number of button pushes: ");
     Serial.println(buttonPushCounter);
   } else {
     // if the current state is LOW then the button went from on to off:
     Serial.println("off");
   }
   // Delay a little bit to avoid bouncing
   delay(50);
 }
 // save the current state as the last state,for next time through the loop
 lastButtonState = buttonState;


 // turns on the LED every four button pushes by checking the modulo of the
 // button push counter. 
   if (buttonPushCounter == 0) {
     //turn everything off
   }
     if (buttonPushCounter == 1) {
       for(int i=0; i<=24; i++){
        digitalWrite(pins[i],HIGH);
       }
     }
     else if (buttonPushCounter == 2) {
       for(int i=0; i<=24; i++){
        digitalWrite(pins[i],HIGH);
        delay(100);
       }
     }
     else if (buttonPushCounter == 3) {
       for (int i=0; i<PIN_COUNT; i++){    // chasing right
          digitalWrite(pins[i],HIGH);         // switching the LED at index i on
          delay(100);                          // stopping the program for 100 milliseconds
          digitalWrite(pins[i],LOW);          // switching the LED at index i off
        }
        for (int i=PIN_COUNT-1; i>0; i--){   // chasing left (except the outer leds)
          digitalWrite(pins[i],LOW);          // switching the LED at index i off
        }
       buttonPushCounter = 0;
        }
     else {
       buttonPushCounter = 0;
     }
   }



void chaser(){
  for (int i=0; i<PIN_COUNT; i++){    // chasing right
    digitalWrite(pins[i],HIGH);         // switching the LED at index i on
    delay(100);                          // stopping the program for 100 milliseconds
    digitalWrite(pins[i],LOW);          // switching the LED at index i off
  }
  for (int i=PIN_COUNT-1; i>0; i--){   // chasing left (except the outer leds)
    digitalWrite(pins[i],LOW);          // switching the LED at index i off
  }
}

void oneByOne(){
  for(int i=0; i<=24; i++){
  digitalWrite(pins[i],HIGH);
  delay(100);
  }
}

void allOn(){
  for(int i=0; i<=24; i++){
  digitalWrite(pins[i],HIGH);
  }
}

底部,我还编写了 3 个函数,但它们现在在 if 语句中,因为我认为这可能会有所帮助。按钮也需要电阻吗?因为我在不同的 arduino 项目中使用了相同类型的按钮,该项目带有一个 arduino Uno 和 2 个 neopixel 环,并且那里的按钮不需要电阻器。

解决方法

首先,设置引脚模式的好方法!什么都没有发生,因为您的第一个 if 子句从未打开,因为按钮状态要么高要么低,因此您不能那样使用它。您可以通过在每次传递后设置 lastbuttonstate = Buttonstate 来保存最后一个按钮状态,以监听更改。那么它可能会起作用。此外,要使其正常工作,您需要事先将最后一个按钮状态设置为 HIGH 或 LOW。

来自德国的问候