Arduino 语句错误。预期的 ')' 在 ';' 之前令牌。如何解决这个问题?

Arduino for statement error. Expected ')' before ';' token. How to fix this?

我对 Arduino 和 C++ 比较陌生,而且一直卡在这个错误上。我试图让 LED 同时穿过矩阵。

我收到的错误信息是

"exit status 1. expected ')' before ';' token"

任何帮助都会很棒。

#define NUM_LEDS 64
#define DATA_PIN 3
CRGB leds[NUM_LEDS];
int count1 = 0;
int count2 = 0;

void setup() {
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
}

void loop() {
  for ((count1 = 0; count1 <= 15; count1++) and (count2 = 31; count2 >= 16; count2--)) {
    leds[count1] = CRGB::Blue;
    leds[count2] = CRGB::Blue;
    FastLED.show();
    delay(100);
    leds[count1] = CRGB::Black;
    leds[count2] = CRGB::Black;
  }
}

您的 for 循环不起作用。

一个 for 循环是:for ( initial; test; update )

你将所有这三个部分都重复了两次,它们之间有一个 "and",这是无效的语法。

for ((count1 = 0; count1 <= 15; count1++) and (count2 = 31; count2 >= 16; count2--)) { <- 无效!

你能做的是:

for (count1 = 0, count2 = 31; count1 <= 15 && count2 >= 16; count1++, count2--)

您提供的代码中存在许多问题(例如未定义变量 - 但是,我假设您只是没有提供所有相关代码)。主要问题是您的 "for loop" 语法,您可能希望它看起来像这样:

#define NUM_LEDS 64
#define DATA_PIN 3
CRGB leds[NUM_LEDS];
int count1 = 0;
int count2 = 0;

void setup() {
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
}

void loop() {
  for (count1 = 0; count1 <= 15; count1++){
    for (count2 = 31; count2 >= 16; count2--) {
        leds[count1] = CRGB::Blue;
        leds[count2] = CRGB::Blue;
        FastLED.show();
        delay(100);
        leds[count1] = CRGB::Black;
        leds[count2] = CRGB::Black;
    }
  }
}