【问题标题】:Arduino: Cannot move from one state to another (FastLED + Pixel Matrix)Arduino:无法从一种状态移动到另一种状态(FastLED + Pixel Matrix)
【发布时间】:2022-11-17 11:23:03
【问题描述】:

我正在尝试使用我构建的 LED 矩阵在 Arduino 上编写测试代码。 我写了几个函数来为 LED 矩阵上的图形设置动画,并想通过串行监视器更改不同整数“parseInt”处的动画。 但是,我能够进入任何一个“状态”,但是一旦我处于其中一个状态,当我发送一个可能激活其相应状态的不同整数时,我就无法转换到另一个状态。

有谁知道我在这里做错了什么? 代码如下所示:

int state = 0;
void loop() {

  while (Serial.available() > 0) {
    int mm = Serial.parseInt();
    if (mm == 1) {
      state = 1;
      while (state == 1) {
        animation1();

      }

    } else if (mm == 2) {
      state = 2;
      while (state == 2) {
        animation2();
        Serial.println(state);
      }} else if (mm == 3) {
        state=3;
        while (state ==3) {
        animation3();
        }
      }
    }
  }

我尝试创建布尔变量,并尝试打印出像素正在动画的状态,但似乎一旦它进入其中一种状态,它甚至不会读取通过串行监视器发送的新字符。

【问题讨论】:

    标签: arduino fastled


    【解决方案1】:

    你不应该使用 while loop ,它会在那里导致无限循环。所以它会无限期地卡在那里。

    尝试这个

    int state = 0;
    void loop() {
    
      while (Serial.available() > 0) 
      {
          int mm = Serial.parseInt();
          if (mm == 1) 
          {
            state = 1;
            animation1();
          } 
          else if (mm == 2) 
          {
              state = 2;
              animation2();
          } 
          else if (mm == 3) 
          {
              state = 3;
              animation3();
          }
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多