2

好的,我对这段代码有一个莫名其妙的问题。计数器pushCounterz(以 az 命名只是为了排除冲突的变量)将以正确的计数器(1 或 0 或任何一个)开始,并且在按下按钮时会发生以下几种情况之一:

  1. 什么也没做
  2. 正确切换 LED,但计数器以 2 种方式中的 1 种方式发生故障
    1. 计数器将跳转到 -255,然后在第二次按下时重置为 1,并且每次按下时都会在 1 和 -255 之间翻转
    2. 计数器不会增加
    3. 计数器和/或 LED 将随机增加而无需接触任何东西。
    #include <FastLED.h>
    #define AnalogIn A0
    #define SwIn 2
    #define LED_Out 12
    #define NUM_LEDS 100
    int pushCounterz = 0;
    int buttonState;
    int lastButtonState;   // the previous reading from the input pin
    int ledPin = 13;
    int ledState = HIGH; 

    CRGB leds[NUM_LEDS];

    void setup() {
      // put your setup code here, to run once:
      FastLED.addLeds<WS2812, LED_Out, GRB>(leds, NUM_LEDS);
      //pinMode(SwIn, INPUT);
      pinMode(LED_Out, OUTPUT);
      pinMode(ledPin, OUTPUT);
      //Turn Off strip
      for (int i = 0; i <= NUM_LEDS; i++) {
        leds[i] = CRGB ( 255, 0, 0 );
        FastLED.show();
      }
        digitalWrite(ledPin, ledState);
        Serial.begin(115200);
        Serial.println(pushCounterz);
        lastButtonState = digitalRead(SwIn); // Set the button state to the startup state 
    }

    void loop() {

      buttonState = digitalRead(SwIn);

   if (buttonState == LOW && buttonState != lastButtonState) {
    ledState = !ledState;
   }
   if (buttonState == LOW && buttonState != lastButtonState) {
        if (pushCounterz > 3) {
          Serial.println("Reset to 0: ");
          pushCounterz = 0;
        } else {
          pushCounterz = pushCounterz + 1;
          Serial.println("Incerment");
        }
        Serial.println(pushCounterz);
        switch (pushCounterz) {
          case 0:
            for (int i = 0; i <= NUM_LEDS; i++) {
              leds[i] = CRGB (255, 0, 0);
            }
            break;
          case 1:
            for (int i = 0; i <= NUM_LEDS; i++) {
              leds[i] = CRGB ( 0, 255, 0);
             }
             break;
          case 2:
            for (int i = 0; i <= NUM_LEDS; i++) {
               leds[i] = CRGB ( 0, 0, 255);
             }
            break;
          case 3:
           // theaterChaseRainbow(1,50);
            break;
          default:
           for (int i = 0; i <= NUM_LEDS; i++) {
             leds[i] = CRGB ( 0, 0, 0);
           }
           break;
        }
      }
    FastLED.show();
    digitalWrite(ledPin, ledState);
    lastButtonState = buttonState;
    }

但是,如果我禁用 switch 代码块,一切都会完美无缺。我怀疑这是 FastLED 库中的一个错误,但是我想在这里问一下,因为我对 Arduino 编程很陌生。

编辑:上面的代码修复了按钮的一个问题。我已将电路更改为 HIGH 并降至 LOW,但没有更改代码。然而,状态仍然不一致,有时工作,有时在 1 和 -255 之间翻转(更多的是后者)。变更摘要:

void setup() {
...
  lastButtonState = digitalRead(SwIn); // Set the button state to the startup state 
}

  */
  buttonState = digitalRead(SwIn);

  if (buttonState == LOW && buttonState != lastButtonState) {
    ledState = !ledState;
  }
  if (buttonState == LOW && buttonState != lastButtonState) {

编辑:串行输出显示上述编辑后的当前疯狂(带注释):

0 <- initial startup correct
Incerment <- button push
1 <- correct increment
Incerment <- 2nd button push
-255 <- 1 + 1 does NOT equal -255
Incerment <- 3rd button push
1 <- ??? Assuming -255 +1 = 1?
Incerment
-255
4

1 回答 1

2

for (int i = 0; i <= NUM_LEDS; i++)

您正在使用索引 100,它是数组的第 101 个元素。

这是未定义的行为,在您的情况下会导致pushCounterz.

通过使用 更改运算符来修复所有for迭代的循环。leds<=<

for (int i = 0; i < NUM_LEDS; i++)
于 2018-05-12T05:42:32.710 回答