FastLED 问题和一个简单的计数器

Issue with FastLED and a simple counter

好的,这段代码有一个令人费解的问题。计数器 pushCounterz(以 z 命名只是为了排除冲突变量)将从正确的计数器(1 或 0 或任何一个)开始,按下按钮时会发生以下情况之一:

  1. 什么都不做
  2. 正确切换 LED 但计数器以 2 种方式之一出现故障
    1. 计数器将跳转到 -255,然后在第 2 次按下时重置为 1,并且每次按下都会在 1 和 -255 之间翻转
    2. 计数器不会递增
    3. 计数器 and/or LED 会随机增加 w/o 触摸任何东西。
    #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;
    }

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

编辑:上面的代码修复了按钮的一个问题。我已经将电路更改为高电平并降低到低电平,但是没有更改代码。然而,状态仍然不一致,有时有效,有时在 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

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

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

这是未定义的行为,在您的情况下会导致 pushCounterz 出现问题。

修复遍历 leds 的所有 for 循环,方法是将 <= 运算符更改为 <

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