Else if statement not captured

Good day,

I have issue with my sketch, the sketch is for a radio controlled robot which I wish to had addressable individual LED strip.

The remote control has two thumb joystick, joystick 1 is to turn on/off lights when pressed, joystick 2 is to cycle through LED patterns when pressed.

Problem : ELse if doesn’t seem to work, once it goes through the first else if the serial monitor display 255. I treid removing 2 Else IF and it behaved differently.

  1. How to fix else if?

  2. I have tried to see how to clear led so next mode can have correct LED lit but I am unable to figure this as well, I have tried FastLED.clear(); but seem to not do anything.

thank you in advance.

this is a sketch I am testing with to figure out things.

#include <FastLED.h>
#define LED_PIN     3
#define NUM_LEDS    30

//Predefined group of LEDS
int allLEDS[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
int frontLEDS[] = {1, 2, 3, 4, 5, 6, 7, 8};
int leftLEDS[] = {9, 10, 11, 12, 13, 14, 15, 16};
int rightLEDS[] = {17, 18, 19, 20, 21, 22, 23, 24};
int backLEDS[] = {25, 26, 27, 28, 29, 30};

//predefined Joysticks input
const int JoystickPushButton = A5;     // the pin that the pushbutton is attached to
const int Joystick1PushButton = A4;


//array of lEDS
CRGB leds[NUM_LEDS];

//predefined state for push joystick 1
int JoystickbuttonState      = 0;     // current state of the button
int JoysticklastButtonState  = 0;     // previous state of the button
int JoystickmodeState        = 0;     // remember current led state
int JoystickstateNum;

//predefined state for push joystick 2
int Joystick1buttonState      = 0;     // current state of the button
int Joystick1lastButtonState  = 0;     // previous state of the button
int Joystick1modeState        = 0;     // remember current led state
int Joystick1stateNum;


//function to lit all lights
void lightsOnAll() {
  for (int i = 0; i < 30; i++)
  {
    leds[allLEDS[i] ] = CRGB::White;
  }
  FastLED.show();

}

//function to turn all lights off
void lightsOffAll() {
  for (int i = 0; i < 30; i++)
  {
    leds[allLEDS[i] ] = CRGB::Black;
  }
  FastLED.show();
}

//function to lit front lights
void lightsforwardOnly() {
  for (int i = 0; i < 30; i++)
  {
    leds[frontLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}

//function to lit back lights
void lightsbackOnly() {
  for (int i = 0; i < 30; i++)
  {
    leds[backLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}

//function to lit right lights
void lightsrightOnly() {
  for (int i = 0; i < 30; i++)
  {
    leds[rightLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}

//function to lit left lights
void lightsleftOnly() {
  for (int i = 0; i < 30; i++)
  {
    leds[leftLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}
void setup() {
  Serial.begin(9600);
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);
  FastLED.setBrightness(255);

  pinMode(JoystickPushButton, INPUT_PULLUP);  // initialize the button pin as a input
  pinMode(Joystick1PushButton, INPUT_PULLUP);  // initialize the button pin as a input


}


void loop() {

  JoystickbuttonState = digitalRead(JoystickPushButton);
  Joystick1buttonState = digitalRead(Joystick1PushButton);

  // check if the button is pressed or released
  // by comparing the buttonState to its previous state
  if (JoystickbuttonState != JoysticklastButtonState) {
    // change the state of the led when someone pressed the button
    if (JoystickbuttonState == 1) {
      JoystickstateNum++;
      if (JoystickstateNum > 2) JoystickstateNum = 1;
    }
    // remember the current state of the button
    //   JoysticklastButtonState = JoystickbuttonState;
  }
  // Serial.println(JoystickstateNum);
  Serial.println(Joystick1stateNum);
  // adding a small delay prevents reading the buttonState to fast
  // ( debouncing )
  delay(20);



  // check if the button is pressed or released
  // by comparing the buttonState to its previous state
  if (Joystick1buttonState != Joystick1lastButtonState) {
    // change the state of the led when someone pressed the button
    if (Joystick1buttonState == 1) {
      Joystick1stateNum++;
      if (Joystick1stateNum > 5) Joystick1stateNum = 1;
    }
    // remember the current state of the button
    //Joystick1lastButtonState = Joystick1buttonState;
  }

  // adding a small delay prevents reading the buttonState to fast
  // ( debouncing )
  delay(20);





  if (JoystickstateNum == 1) {
    lightsOffAll();
    Serial.println("OFF");
  }

 else if (JoystickstateNum == 2 && Joystick1stateNum == 1) {
    lightsOnAll();
    Serial.println("Mode ON");
  }
  else if (JoystickstateNum == 2 && Joystick1stateNum == 2) {
    Serial.println("Mode 1A set front Lights On only");
  
    lightsforwardOnly();
  }
  else if (JoystickstateNum == 2 && Joystick1stateNum == 3) {
    Serial.println("Mode 1A set left Lights On only");
    lightsleftOnly();
  }

  else if (JoystickstateNum == 2 && Joystick1stateNum == 4) {
    Serial.println("Mode 1A set right Lights On only");
    lightsrightOnly();
  }
  else if (JoystickstateNum == 2 && Joystick1stateNum == 5) {
    Serial.println("Mode 1A set back Lights On only");
    lightsbackOnly();
  }

  

  // remember the current state of the button
  JoysticklastButtonState = JoystickbuttonState;
  Joystick1lastButtonState = Joystick1buttonState;
}

You have a lot of else statements- which one isn't working?

The FastLED.clear() function sets all the pixels to Black. You do of course have to do FastLED.show() to send the data out to the LEDs. The clear function takes an optional parameter:

FastLED.clear(true);

By default FastLED.clear() clears the CRGB array, then you need FastLED.show() afterwards.
The parameter true clears the CRGB array and also turns the LEDs off.

Thank you for the feedback on FastLED.clear function and how to apply. :slight_smile:

I have successfully reorganized my IF ELSE IF statement and can now toggle between the LED preset patterns BUT I now need to look into clearing the previous pattern before toggling to next LED pattern.

I will keep trying and report back. :slight_smile:

Thank you very much

new updated sketch.

/*
   Created by ArduinoGetStarted.com

   This example code is in the public domain

   Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-library

   This example:
     + uses debounce for multiple buttons.
     + reads state of multiple buttons
     + detects the pressed and released events of multiple buttons
*/

#include <ezButton.h>
#include <FastLED.h>
#define LED_PIN     3
#define NUM_LEDS    30

//Predefined group of LEDS
int allLEDS[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
int frontLEDS[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
int leftLEDS[] = {9, 10, 11, 12, 13, 14, 15, 16};
int rightLEDS[] = {17, 18, 19, 20, 21, 22, 23, 24};
int backLEDS[] = {25, 26, 27, 28, 29, 30};

//array of lEDS
CRGB leds[NUM_LEDS];

ezButton button1(A5);  // create ezButton object that attach to pin 7;
ezButton button2(A4);  // create ezButton object that attach to pin 8;


//function to lit all lights
void lightsOnAll() {

  for (int i = 0; i < 30; i++)
  {
    leds[allLEDS[i] ] = CRGB::White;
  }
  FastLED.show();

}

//function to turn all lights off
void lightsOffAll() {

  for (int i = 0; i < 30; i++)
  {
    leds[allLEDS[i] ] = CRGB::Black;
  }
  FastLED.show();
}

//function to lit front lights
void lightsforwardOnly() {
  for (int i = 0; i < 9; i++)
  {
    leds[frontLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}

//function to lit back lights
void lightsbackOnly() {
  ;
  for (int i = 0; i < 6; i++)
  {
    leds[backLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}

//function to lit right lights
void lightsrightOnly() {

  for (int i = 0; i < 30; i++)
  {
    leds[rightLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}

//function to lit left lights
void lightsleftOnly() {
  for (int i = 0; i < 8; i++)
  {
    leds[leftLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}



void setup() {
  Serial.begin(9600);

  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setMaxPowerInVoltsAndMilliamps(3, 500);
  FastLED.setBrightness(255);

  button1.setDebounceTime(50); // set debounce time to 50 milliseconds
  button2.setDebounceTime(50); // set debounce time to 50 milliseconds
  // button3.setDebounceTime(50); // set debounce time to 50 milliseconds
}

void loop() {
  button1.loop(); // MUST call the loop() function first
  button2.loop(); // MUST call the loop() function first
  // button3.loop(); // MUST call the loop() function first


  unsigned long button1count = button1.getCount();
  unsigned long button2count = button2.getCount();

  Serial.println(button1count);
  if (button1count >= 3)
    button1.resetCount();
  if (button2count >= 4)
    button2.resetCount();



  if (button1count == 1 && button2count == 0) {
    lightsforwardOnly();
    Serial.println("Mode1A forward lights On");
  }

  else  if (button1count == 1 && button2count == 1) {
    lightsbackOnly();
    Serial.println("Mode1B back lights On");
  }

  else  if (button1count == 1 && button2count == 2) {
    lightsleftOnly();
    Serial.println("Mode1C left lights On");
  }

  else  if (button1count == 1 && button2count == 3) {
    lightsrightOnly();
    Serial.println("Mode1D right lights On");
  }

  else {
    lightsOffAll();
    Serial.println("OFF");
  }






  if (button1count == 2) {
    // lightsOnAll();
    Serial.println("Mode 2 On");


  }



}

I have successfully added the fastled.clear function.

One question I have is how it is integrated into the sketch, because it loop constantly it makes the leds flicker.

video:

Is there ways to eliminate this? maybe I have taken the wrong approach?

thank You

/*
   Created by ArduinoGetStarted.com

   This example code is in the public domain

   Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-library

   This example:
     + uses debounce for multiple buttons.
     + reads state of multiple buttons
     + detects the pressed and released events of multiple buttons
*/

#include <ezButton.h>
#include <FastLED.h>
#define LED_PIN     3
#define NUM_LEDS    30

//Predefined group of LEDS
int allLEDS[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
int frontLEDS[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
int leftLEDS[] = {9, 10, 11, 12, 13, 14, 15, 16};
int rightLEDS[] = {17, 18, 19, 20, 21, 22, 23, 24};
int backLEDS[] = {25, 26, 27, 28, 29, 30};

//array of lEDS
CRGB leds[NUM_LEDS];

ezButton button1(A5);  // create ezButton object that attach to pin 7;
ezButton button2(A4);  // create ezButton object that attach to pin 8;


//function to lit all lights
void lightsOnAll() {

  for (int i = 0; i < 30; i++)
  {
    leds[allLEDS[i] ] = CRGB::White;
  }
  FastLED.show();

}

//function to turn all lights off
void lightsOffAll() {

  for (int i = 0; i < 30; i++)
  {
    leds[allLEDS[i] ] = CRGB::Black;
  }
  FastLED.show();
}

//function to lit front lights
void lightsforwardOnly() {
  FastLED.clear ();
  FastLED.show();
  for (int i = 0; i < 9; i++)
  {
    leds[frontLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}

//function to lit back lights
void lightsbackOnly() {
  FastLED.clear ();
  FastLED.show();
  for (int i = 0; i < 6; i++)
  {
    leds[backLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}

//function to lit right lights
void lightsrightOnly() {
  FastLED.clear ();
  FastLED.show();
  for (int i = 0; i < 8; i++)
  {
    leds[rightLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}

//function to lit left lights
void lightsleftOnly() {
  FastLED.clear ();
  FastLED.show();
  for (int i = 0; i < 8; i++)
  {
    leds[leftLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}



void setup() {
  Serial.begin(9600);

  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setMaxPowerInVoltsAndMilliamps(3, 500);
  FastLED.setBrightness(255);

  button1.setDebounceTime(50); // set debounce time to 50 milliseconds
  button2.setDebounceTime(50); // set debounce time to 50 milliseconds
  // button3.setDebounceTime(50); // set debounce time to 50 milliseconds
}

void loop() {
  button1.loop(); // MUST call the loop() function first
  button2.loop(); // MUST call the loop() function first
  // button3.loop(); // MUST call the loop() function first


  unsigned long button1count = button1.getCount();
  unsigned long button2count = button2.getCount();

  Serial.println(button1count);
  if (button1count >= 3)
    button1.resetCount();
  if (button2count >= 5)
    button2.resetCount();



  if (button1count == 1 && button2count == 0) {
    lightsforwardOnly();
    Serial.println("Mode1A forward lights On");
  }

  else  if (button1count == 1 && button2count == 1) {
    lightsbackOnly();
    Serial.println("Mode1B back lights On");
  }

  else  if (button1count == 1 && button2count == 2) {
    lightsleftOnly();
    Serial.println("Mode1C left lights On");
  }

  else  if (button1count == 1 && button2count == 3) {
    lightsrightOnly();
    Serial.println("Mode1D right lights On");
  }

  else  if (button1count == 1 && button2count == 4) {
    lightsOnAll();
    Serial.println("Mode1E all lights On");
  }  

  else {
    lightsOffAll();
    Serial.println("OFF");
  }






  if (button1count == 2) {
    // lightsOnAll();
    Serial.println("Mode 2 On");


  }



}

I fixed the issue by leveraging the EZbutton library and isPressed() function. When button is pressed to switch LED pattern then there is a clear being done on the LED strip and this eliminated the flickering problem by looping and reading fastled function rapidly.

ezbutton library is pretty cool

Now that the sketch is behaving great I can move to plan wiring to the robot.
:slight_smile:

/*
   Created by ArduinoGetStarted.com

   This example code is in the public domain

   Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-library

   This example:
     + uses debounce for multiple buttons.
     + reads state of multiple buttons
     + detects the pressed and released events of multiple buttons
*/

#include <ezButton.h>
#include <FastLED.h>
#define LED_PIN     3
#define NUM_LEDS    30

//Predefined group of LEDS
int allLEDS[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
int frontLEDS[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
int leftLEDS[] = {9, 10, 11, 12, 13, 14, 15, 16};
int rightLEDS[] = {17, 18, 19, 20, 21, 22, 23, 24};
int backLEDS[] = {25, 26, 27, 28, 29, 30};

//array of lEDS
CRGB leds[NUM_LEDS];

ezButton button1(A5);  // create ezButton object that attach to pin 7;
ezButton button2(A4);  // create ezButton object that attach to pin 8;


//function to lit all lights
void lightsOnAll() {

  for (int i = 0; i < 30; i++)
  {
    leds[allLEDS[i] ] = CRGB::White;
  }
  FastLED.show();

}

//function to turn all lights off
void lightsOffAll() {

  for (int i = 0; i < 30; i++)
  {
    leds[allLEDS[i] ] = CRGB::Black;
  }
  FastLED.show();
}

//function to lit front lights
void lightsforwardOnly() {
  for (int i = 0; i < 9; i++)
  {
    leds[frontLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}

//function to lit back lights
void lightsbackOnly() {
  for (int i = 0; i < 6; i++)
  {
    leds[backLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}

//function to lit right lights
void lightsrightOnly() {
  for (int i = 0; i < 8; i++)
  {
    leds[rightLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}

//function to lit left lights
void lightsleftOnly() {
  for (int i = 0; i < 8; i++)
  {
    leds[leftLEDS[i] ] = CRGB::White;
  }
  FastLED.show();
}



void setup() {
  Serial.begin(9600);

  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setMaxPowerInVoltsAndMilliamps(3, 500);
  FastLED.setBrightness(255);

  button1.setDebounceTime(50); // set debounce time to 50 milliseconds
  button2.setDebounceTime(50); // set debounce time to 50 milliseconds
  // button3.setDebounceTime(50); // set debounce time to 50 milliseconds
  FastLED.clear();  // clear all pixel data
  FastLED.show();

}

void loop() {
  button1.loop(); // MUST call the loop() function first
  button2.loop(); // MUST call the loop() function first
  // button3.loop(); // MUST call the loop() function first


  unsigned long button1count = button1.getCount();
  unsigned long button2count = button2.getCount();

  Serial.println(button1count);
  if (button1count >= 3)
    button1.resetCount();
  if (button2count >= 5)
    button2.resetCount();

  if(button2.isPressed()){
      FastLED.clear();  // clear all pixel data
  FastLED.show();
        Serial.println("The button is pressed");
  }




  if (button1count == 1 && button2count == 0) {
    lightsforwardOnly();
    Serial.println("Mode1A forward lights On");
  }

  else  if (button1count == 1 && button2count == 1) {
    lightsbackOnly();
    Serial.println("Mode1B back lights On");
  }

  else  if (button1count == 1 && button2count == 2) {
    lightsleftOnly();
    Serial.println("Mode1C left lights On");
  }

  else  if (button1count == 1 && button2count == 3) {
    lightsrightOnly();
    Serial.println("Mode1D right lights On");
  }

  else  if (button1count == 1 && button2count == 4) {
    lightsOnAll();
    Serial.println("Mode1E all lights On");
  }  

  else {
    lightsOffAll();
    Serial.println("OFF");
  }






  if (button1count == 2) {
    // lightsOnAll();
    Serial.println("Mode 2 On");


  }


}

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