Serial.available() early escape. Trying to calibrate Neopixel LEDs

Sorry for the large amount of code. I am trying the calibrate the brightness of neopixel LEDs using the serial port. The issue seems to be with line 46.

while(Serial.available()==0){}

every second or so program seems to escape the while loop and the following is printed to the terminal.

0: cycle light strips
1: cycle colour channel
7: decrease intensity
8: increase intensity
Side: Red: 10 Green: 10 Blue: 10
Ring: Red: 10 Green: 10 Blue: 10

Any suggestions on how I can fix this?

#include <FastLED.h>

#define NUM_LEDS_SIDE 90
#define NUM_LEDS_RING 40
#define NUM_LEDS_BASE 73

#define DATA_PIN_SIDE 53
#define DATA_PIN_RINGL 49
#define DATA_PIN_RINGR 45
#define DATA_PIN_BASE 41

CRGB LedsSide[NUM_LEDS_SIDE];
CRGB LedsRingL[NUM_LEDS_RING];
CRGB LedsRingR[NUM_LEDS_RING];
CRGB LedsBase[NUM_LEDS_BASE];

int r_base = 10;
int g_base = 10;
int b_base = 10;
int r_side = 10;
int g_side = 10;
int b_side = 10;
int r_ring = 10;
int g_ring = 10;
int b_ring = 10;

int target = 0;
int channel = 4;
int pointer = 0;

void setup() {
  Serial.begin(115200);
  FastLED.addLeds<NEOPIXEL, DATA_PIN_RINGR>(LedsRingR, NUM_LEDS_RING);  
  FastLED.addLeds<NEOPIXEL, DATA_PIN_RINGL>(LedsRingL, NUM_LEDS_RING);  
  FastLED.addLeds<NEOPIXEL, DATA_PIN_SIDE>(LedsSide, NUM_LEDS_SIDE);  
  FastLED.addLeds<NEOPIXEL, DATA_PIN_BASE>(LedsBase, NUM_LEDS_BASE); 
  Serial.println("booting");
  Serial.println("0: cycle light strips");
  Serial.println("1: cycle color channel");
  Serial.println("7: decrease intensity");
  Serial.println("8: increase intensity");
}

void loop(){
  
  while(Serial.available()==0){}
  target = Serial.parseInt();
  while(Serial.available() > 0) {
    char t = Serial.read();
  }

  if(target==0){
    pointer++;
    if(pointer==3){
      pointer = 0;
    }
  }
  if(target==1){
    channel++;
    if(channel==3){
      channel = 0;
    }
  }

  if(target==8&&pointer==0&&channel==0){r_base++;}
  if(target==8&&pointer==0&&channel==1){g_base++;}
  if(target==8&&pointer==0&&channel==2){b_base++;}
  if(target==8&&pointer==1&&channel==0){r_side++;}
  if(target==8&&pointer==1&&channel==1){g_side++;}
  if(target==8&&pointer==1&&channel==2){b_side++;}
  if(target==8&&pointer==2&&channel==0){r_ring++;}
  if(target==8&&pointer==2&&channel==1){g_ring++;}
  if(target==8&&pointer==2&&channel==2){b_ring++;}
  if(target==7&&pointer==0&&channel==0){r_base--;}
  if(target==7&&pointer==0&&channel==1){g_base--;}
  if(target==7&&pointer==0&&channel==2){b_base--;}
  if(target==7&&pointer==1&&channel==0){r_side--;}
  if(target==7&&pointer==1&&channel==1){r_side--;}
  if(target==7&&pointer==1&&channel==2){r_side--;}
  if(target==7&&pointer==2&&channel==0){r_ring--;}
  if(target==7&&pointer==2&&channel==1){g_ring--;}
  if(target==7&&pointer==2&&channel==2){b_ring--;}

  Serial.println("0: cycle light strips");
  Serial.println("1: cycle color channel");
  Serial.println("7: decrease intensity");
  Serial.println("8: increase intensity");
  if(pointer = 0){print_colours(pointer,r_base,g_base,b_base);}
  if(pointer = 1){print_colours(pointer,r_side,g_side,b_side);}
  if(pointer = 2){print_colours(pointer,r_ring,g_ring,b_ring);}
  for(int i = 0; i < NUM_LEDS_BASE; i++){LedsBase[i] = LedsBase[i].setRGB(r_base,g_base,b_base);}
  for(int i = 0; i < NUM_LEDS_SIDE; i++){LedsSide[i] = LedsSide[i].setRGB(r_side,g_side,b_side);}
  for(int i = 0; i < NUM_LEDS_RING; i++){LedsRingL[i] = LedsRingL[i].setRGB(r_ring,g_ring,b_ring);}
  for(int i = 0; i < NUM_LEDS_RING; i++){LedsRingR[i] = LedsRingR[i].setRGB(r_ring,g_ring,b_ring);}
  FastLED.show();
  Serial.println(" ");
}

void print_colours(int pointer, int r,int g,int b){
  if(pointer==0){Serial.print("Base:");}
  if(pointer==1){Serial.print("Side:");}
  if(pointer==2){Serial.print("Ring:");}
  Serial.print(" Red: ");
  Serial.print(r);
  Serial.print(" Green: ");
  Serial.print(g);
  Serial.print(" Blue: ");
  Serial.println(b);
}

What have you got the Line Ending set to in the Serial monitor ?
Anything other than "no line ending" will append CR, LF or both to the message that you enter and will be seen as available()

This loop doesn't do a great job of clearing the input buffer because it might get ahead of the arriving characters and find the buffer empty even though more characters are arriving. If you move it to ABOVE the while(Serial.available()==0){} then your input buffer will be cleared just before you wait for the first character to arrive.

Thank you a lot for the help guys, very much appreciated. I found that lowering the baud rate down to 9600 fixed it.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.