Code in my for loop isn't running. (Uses Serial to connect Arduino & Processing)

Hello!

So I’m trying to use Processing and Arduino together by managing the flow of data with Serial.
The code below is basically supposed to get an LED strip to turn red and carry out ‘basicFade’ when the number ‘1’ is received, and turn green and carry out ‘rollingFade’ if the number ‘2’ is received. As of now, the LED’s turn green, and not red, and the first function to carry out stays the same even though the value of ‘val’ changes. There are no errors. Any ideas as to where I’m going wrong?

Note: I’ve edited out a lot of other code to keep things simple, but if needed, I can post all of it.

Thanks so much in advance!

void loop() {

  if (Serial.available() > 0) { // 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 is received  
       strip.setPixelColor(i, 255, 0, 0); 
       basicFade(false,10,strip.numPixels(),0); 
       rollingFade(true, 10, strip.numPixels());
     }
      if (val == '2') { // If 2 is received
       strip.setPixelColor(i, 15, 224, 67);
       basicFade(true,10,strip.numPixels(),0);
       rollingFade(false, 10, strip.numPixels());          
       }
      } 
  }
 

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, 15, 224, 67);
          
              }//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, 15, 224, 67);
          
              }//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
}
for (int i = 0; i <= strip.numPixels();

You said there were no errors.

No errors are popping up, yes.

Are you implying I should get rid of the ‘i++’ part of that line?

No, I’m suggesting you replace the <= with a single <

Thank you. I Just tried that, but it still doesn’t change the results. I can’t get this part of the code to work:

void loop() {

  if (Serial.available() > 0) { // 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 is received  
       strip.setPixelColor(i, 255, 0, 0); 
       basicFade(false,10,strip.numPixels(),0); 
       rollingFade(true, 10, strip.numPixels());
     }
      if (val == '2') { // If 2 is received
       strip.setPixelColor(i, 15, 224, 67);
       basicFade(true,10,strip.numPixels(),0);
       rollingFade(false, 10, strip.numPixels());          
       }
      } 
  }

Use Ctrl-T to reformat your code in the IDE before you post it. It makes it easier for us to read and sometimes will point out errors before you post them.

Don’t post code snippets. Often a problem reduces to where a variable’s definition occurs due to a scoping problem. Show all of your code.

In the code block:

    for (int i = 0; i <= strip.numPixels(); i++) {
      if (val == '1') {                                           // If 1 is received
        strip.setPixelColor(i, 255, 0, 0);
        basicFade(false, 10, strip.numPixels(), 0);
        rollingFade(true, 10, strip.numPixels());
      }
      if (val == '2') {                                          // If 2 is received
        strip.setPixelColor(i, 15, 224, 67);
        basicFade(true, 10, strip.numPixels(), 0);
        rollingFade(false, 10, strip.numPixels());
      }
    }
  }

You check val to see if it’s equal to ‘1’ and then immediately check to see if it’s ‘2’. Clearly, if it is a ‘1’ and you execute the if statement block, why are you wasting time checking it again to see if it’s ‘2’ when you already know it’s a ‘1’? You can fix this with an if/else statement block.

This is simply a style issue, but some comments really don’t add any clarity. Telling that a ‘1’ or a ‘2’ is received is pretty obvious. If those situations where a comment does make sense, I find it easier to read if you slide the comment away from the code to the right. Otherwise, comments tend to “get lost” in the code itself.

Sorry about that, I’m new here and still trying to figure things out. Thank you for letting me know!

I did as you said, and it definitely helped, but the fade function under the else command still doesn’t work. Do you have any other ideas?

I also tried the digitalWrite function to get the LED on my arduino to light up and turn off according to the value of ‘val’, but once it turned on, it didn’t turn off again. I have a feeling something in this piece of code is off.

void loop() {

  if (Serial.available() > 0) { 
 
     val = Serial.read(); 
  
     for (int i = 0; i <= strip.numPixels(); i++) {

      if (val == '1') { 

        digitalWrite(13, HIGH); 

        strip.setPixelColor(i, 255, 0, 0); 

        basicFade(false,10,strip.numPixels(),0); 

        rollingFade(true, 10, strip.numPixels());

      }
        
        else (val == '2'); { 

           digitalWrite(13,LOW); 

           strip.setPixelColor(i, 15, 224, 67);

           basicFade(true,10,strip.numPixels(),0);

           rollingFade(false, 10, strip.numPixels());          

           }

          } 

      }

If the pixels are turning red instead of green, it's probably because you have GRB LEDs and are initialising the strip to be RGB (or vice versa).

i <= strip.numPixels()I thought we’d talked this one through.

GypsumFantastic:
If the pixels are turning red instead of green, it’s probably because you have GRB LEDs and are initialising the strip to be RGB (or vice versa).

Oh, my LED’s are turning Green and not Red, so it’s the other way round.

AWOL:
i <= strip.numPixels()I thought we’d talked this one through.

And the <= vs < didn’t solve anything. If you insist, I could try that again. Is it just better syntax or will <= cause problems?

amumu97:
Oh, my LED’s are turning Green and not Red, so it’s the other way round.

It’s the same issue. Red and green are inverted. Change this:

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

to this:

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

And the i <= strip.NumPixels() thing: the pixels are numbered starting from zero, so you need to stop before i runs off the end of the strip. That means you need i < strip.NumPixels()

That worked. Thank you so much!