简体   繁体   中英

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

I am relatively new to Arduino and C++ and I am stuck on this error. I am trying to have LEDs streak across a matrix at the same time.

The error message I receive is

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

Any help would be great.

#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;
  }
}

Your for loop doesn't work.

A for loop is: for ( initial ; test ; update )

You have all of those three parts twice with an "and" between them, that is invalid syntax.

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

What you can do is this:

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

There are a number of problems in your provided code (like not defining your variables - I'm assuming, however, that you have merely not provided all relevant code). The main issue is your "for loop" syntax, which you may want to look like this:

#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;
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM