How to fix the " 'i' was not declared in this scope" error

Hello,

I'm a beginner with the Arduino and I'm having trouble with my code.

I've essentially got 5 different functions that control the lights on LED strips, and then I've got code on Processing (another program) that plays one of two audio tracks when the keys 'h' or 'r' are pressed. The processing code also returns the numbers 1 and 2 respectively when either of these tracks are played.

I used the inbuilt library named Serial to connect both these pieces of code, and essentially all I'm trying to do is set the color of the LED strips in my Arduino code to red or green depending on whether I receive a '1' or '2' from Processing. I keep getting this error below: 'i' was not declared in this scope.

I'm not sure where I'm meant to define 'i' in the code below for this error to stop.

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

#define PIN 6

Adafruit_NeoPixel strip = Adafruit_NeoPixel(18, PIN, NEO_GRB + NEO_KHZ800);

int setting = 0;
char val; // Data received fromt the serial port

void setup() {
  Serial.begin(9600); // Starts serial communication at 9600 bps
  strip.begin();
  strip.show(); // Initialize all pixels to 'off' 
}

void loop() {

  if (Serial.available()) // If data is available to read,
   { 
     val = Serial.read(); // read it and store it in val
   }
   
   for (int i = 0; i <= strip.numPixels(); i++);{    
    if (val == '1') // If 1 was received
   { 
     strip.setPixelColor(i, 255, 0, 0); 
   }
   else if (val == '2') // If 2 was received
   {
     strip.setPixelColor(i, 15, 224, 67); 
   }
    } 
    
  simpleTest(false, 100);
  contraryChase(false,100);
  littleChase(false,150);
  basicFade(false,10,strip.numPixels(),0);
  rollingFade(false, 10, strip.numPixels());
}
  
}

void simpleTest(boolean on, int spd){

  while(on){
    
      for (int i = 0; i <= strip.numPixels(); i++){
    
      strip.setPixelColor(i,255,0, 0);
      strip.setPixelColor(i-1, 0, 0, 0);
      strip.show();
      delay(spd);
    
      }
  }
}//end simpleTest

void contraryChase(boolean on, int spd){

  while(on){
    
      for (int i = 0; i <= strip.numPixels(); i++){
    
      strip.setPixelColor(i, random(255),200+random(55),255);
      strip.setPixelColor(i-1, 0, 0, 0);
      strip.setPixelColor(strip.numPixels()-i, 255,255,255);
      strip.setPixelColor(strip.numPixels()-i+1, 0, 0, 0);
      strip.show();
      delay(spd);
    
      }
  }
}//end contraryChase

void basicFade(boolean on, int spd, int numPix, int begPix){

  int level = 0;
  int dir = 0;

  while(on){

      switch(dir){
    
        case 0:
              for (int i = begPix; i <= numPix+begPix; i++){
    
                strip.setPixelColor(i,level,level,level);
          
              }//end for
    
              strip.show();
              delay(spd);
    
              if (level < 255) level++;
              else dir = 1;
    
              break;
    
        case 1:
              for (int i = begPix; i <= numPix+begPix; i++){
    
                strip.setPixelColor(i,level,level,level);
          
              }//end for
    
              strip.show();
              delay(spd);
    
              if (level > 0) level--;
              else dir = 0;
    
              break;
        
      }//end switch
    
  }//end big while

}// end basic fade

void rollingFade(boolean on, int spd, int numPix){

  int lvl1=0;
  int lvl2=0;
  int lvl3=0;
  int state = 0;

  while(on){
Serial.println(state);
    switch(state){

      case 0:
              stripUtility(lvl1,lvl2,lvl3);   
              strip.show();
              delay(spd);
    
              if (lvl1 < 85) lvl1++;
              else state = 1;
    
              break;
    
        case 1:
              stripUtility(lvl1,lvl2,lvl3);
              strip.show();
              delay(spd);
    
              if (lvl1 < 170){
                lvl1++;
                lvl2++;
              }
              else state = 2;
    
              break;

        case 2:
              stripUtility(lvl1,lvl2,lvl3);  
              strip.show();
              delay(spd);
    
              if (lvl2 < 170 ){
                lvl1--;
                lvl2++;
                lvl3++;
              }
              else state = 3;
    
              break;

       case 3:
              stripUtility(lvl1,lvl2,lvl3);   
              strip.show();
              delay(spd);
    
              if (lvl3 < 170 ){
                lvl1--;
                lvl2--;
                lvl3++;
              }
              else state = 4;
    
              break;

        case 4:
              stripUtility(lvl1,lvl2,lvl3);
              strip.show();
              delay(spd);
    
              if (lvl1 > 0 ){
                lvl1--;
                lvl2--;
                lvl3--;
              }
              else state = 5;
    
              break;

        case 5:
              stripUtility(lvl1,lvl2,lvl3);
              strip.show();
              delay(spd);
    
              if (lvl2 > 0 ){
                lvl1++;
                lvl2--;
                lvl3--;
              }
              else state = 6;
    
              break;

       case 6:
              stripUtility(lvl1,lvl2,lvl3);
              strip.show();
              delay(spd);
    
              if (lvl3 > 0 ){
                lvl1++;
                lvl2++;
                lvl3--;
              }
              else state = 1;
    
              break;

         }//end switch   
  }//end big while  
}//end rollingFade

void stripUtility(int lvl1, int lvl2, int lvl3){
  for (int i = 0; i <= 5; i++){
    
                strip.setPixelColor(i,lvl1,lvl1,lvl1);
                strip.setPixelColor(i+6,lvl2,lvl2,lvl2);
                strip.setPixelColor(i+12,lvl3,lvl3,lvl3);
          
              }//end for
}


void littleChase(boolean on, int spd){

  for (int i = 0; i<13; i++){
    if(i<7){
      strip.setPixelColor(i,255,255,255);
      strip.setPixelColor(i+6,255,255,255);
      strip.setPixelColor(i+12,255,255,255);
      strip.show();
      delay(spd);
      strip.setPixelColor(i,0,0,0);
      strip.setPixelColor(i+6,0,0,0);
      strip.setPixelColor(i+12,0,0,0);
      strip.show();
    }
     else{
      strip.setPixelColor(13-i,255,255,255);
      strip.setPixelColor(13-i+6,255,255,255);
      strip.setPixelColor(13-i+12,255,255,255);
      strip.show();
      delay(spd);
      strip.setPixelColor(13-i,0,0,0);
      strip.setPixelColor(13-i+6,0,0,0);
      strip.setPixelColor(13-i+12,0,0,0);
      strip.show();
      
     }
      
  }
}

1.) Use a while loop instead of a for loop. Like this:

int i = 0;

while(i < argument)
{
   //do stuff

   i++;
}

2.) Add "return;" commands to the end of every function you make (except setup() and loop()).

3.) Get rid of the extra bracket on line 46

Your "for" loop:

for (int i = 0; i <= strip.numPixels(); i++);{

Is actually empty because of that trailing semi-colon. The code that follows is NOT inside the for loop. Get rid of the semi-colon:

for (int i = 0; i <= strip.numPixels(); i++)
{
   for loop code here
}

Regards,
Ray L.

2.) Add "return;" commands to the end of every function you make (except setup() and loop()).

Not necessary, unless the function is supposed to return a value. A return will happen when the end of the function is reached.

Power_Broker:
1.) Use a while loop instead of a for loop. Like this:

int i = 0;

while(i < argument)
{
  //do stuff

i++;
}

That's utterly pointless - all you've done is write a for loop in longhand.

RayLivingston:
Your "for" loop:

for (int i = 0; i <= strip.numPixels(); i++);{

Is actually empty because of that trailing semi-colon. The code that follows is NOT inside the for loop. Get rid of the semi-colon:

for (int i = 0; i <= strip.numPixels(); i++)

{
  for loop code here
}




Regards,
Ray L.

That worked perfectly, thank you so much. :slight_smile: