[Solved] if statement wont work

hello, im trying to make a light saber.

When i change the blade color, it only changes through 2 colors.
i have add a few print statements to see which path the code is taking, and seems to get stuck when i push the button a 3rd time, it enter the changeColor function on line 101, but doesn’t seem to meet any of the conditions inside that function.

if anyone could please help out!

thank you in advance!

LED_Strip.ino (3.62 KB)

Please post your code using code tags

#include <FastLED.h>



#define buttonPin 2
#define LED_PIN 6
#define NUM_LEDS 58

CRGB leds[NUM_LEDS];





const int Brightness = 200;
const int LEDTime = 40; // delay time between each LED
const int Timer = 100;
float pressLength = 0; // # how long the button is held down in milliseconds
bool lightState = false; // State of light, on or off.
int colorState = 0;



/*
    - - -  Options for different durations of pressLength (milliseconds) - - -
  Remember to put conditions with longer pressLength first

*/

int optionOne = 1000; // power up saber and power down Saber
int optionTwo = 20; // Change Blade color, Light Blue, Blue, Red, Green, Purple


void setup() {
  Serial.begin(9600);
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  pinMode(buttonPin, INPUT);
}



// - - - Start of Main Loop - - -

void loop() {

  pressLength = 0;


  while (digitalRead(buttonPin) == HIGH) {
    delay(Timer);
    pressLength += Timer;
//    Serial.println(pressLength);
  }

  if (pressLength >= optionOne && lightState == false) { // power on blade
    lightState, colorState = powerUp();
  }

  else if (pressLength >= optionOne && lightState == true) { // power on blade
    lightState, colorState = powerDown();
  }

  else if (pressLength >= optionTwo) { // change blade color
    lightState, colorState = changeColor();
    delay(20);
  }
}
// - - - end of Main Loop - - -






// - - - Functions - - -


bool powerUp() { // power on saber

  for (int i = 0; i <= NUM_LEDS; i++) {
    leds[i] = CRGB(0, 100, Brightness);
    delay(LEDTime);
    FastLED.show();
  }
  return true, 1;
}


bool powerDown() { // power down saber

  for (int i = NUM_LEDS; i >= 0; i--) {
    leds[i] = CRGB(0, 0, 0);
    delay(LEDTime);
    FastLED.show();
  }
  return false, 0;
}



int changeColor() { // Change the color of the blade
  Serial.println(colorState);
    delay(20);

  if (colorState == 0) {
    colorState = colorLightBlue();
    Serial.println(lightState);
    Serial.println("Light Blue");
    delay(20);
    return true, 1;
  }

  else if (colorState == 1) {
    colorState = colorBlue();
    Serial.println(lightState);
    Serial.println("blue");
    delay(20);
    return true, 2;
  }

  else if (colorState == 2) {
    colorState = colorRed();
    Serial.println("red");
    delay(20);
    Serial.println(lightState);
    return true, 3;
  }

  else if (colorState == 3) {
    colorState = colorGreen();
    Serial.println("green");
    delay(20);
    Serial.println(lightState);
    return true, 4;
  }

  else if (colorState == 4) {
    colorState = colorPurple();
    Serial.println("purple");
    delay(20);
    Serial.println(lightState);
    return true, 0;
  }
  
  else {
    return true, 0;
  }

}

int colorLightBlue () {
  for (int i = 0; i <= NUM_LEDS; i++) {
    leds[i] = CRGB(0, 100, Brightness);
  }
  FastLED.show();
  lightState = true;
  return 1;
}


int colorBlue() {
  for (int i = 0; i <= NUM_LEDS; i++) {
    leds[i] = CRGB(0, 0, Brightness);
  }
  FastLED.show();
  lightState = true;
  return 2;
}

int colorRed() {
  for (int i = 0; i <= NUM_LEDS; 4) {
    leds[i] = CRGB(Brightness, 00, 0);
  }
  FastLED.show();
  lightState = true;
  return 3;
}


int colorGreen() {
  for (int i = 0; i <= NUM_LEDS; i++) {
    leds[i] = CRGB(0, Brightness, 0);
  }
  FastLED.show();
  lightState = true;
  return 4;

}

int colorPurple() {
  for (int i = 0; i <= NUM_LEDS; i++) {
    leds[i] = CRGB(188, 0, 211);
  }
  FastLED.show();
  lightState = true;
  return 0;
}

C++ does not return multiple values like you are doing. Commas are sequence points

thanks for the advice, i was unaware C++ doesn’t allow that.

im having another issue now. The colorState seems to be overwritten, once the blade is turned on, its suppose to set to 1, but when press once serial print shows 25600, then lightState is set to 1 as expected. When i press again both colorState and lightState are overwritten.

Sorry about the code tags, i’ll learn how to use them!!!

cheers,

LED_Strip.ino (3.54 KB)

#include <FastLED.h>



#define buttonPin 2
#define LED_PIN 6
#define NUM_LEDS 58

CRGB leds[NUM_LEDS];





const int Brightness = 200;
const int LEDTime = 40; // delay time between each LED
const int Timer = 100;
float pressLength = 0; // # how long the button is held down in milliseconds
bool lightState = false; // State of light, on or off.
int colorState = 0;



/*
    - - -  Options for different durations of pressLength (milliseconds) - - -
  Remember to put conditions with longer pressLength first

*/

const int optionOne = 500; // power up saber and power down Saber
const int optionTwo = 20; // Change Blade color, Light Blue, Blue, Red, Green, Purple


void setup() {
  Serial.begin(9600);
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  pinMode(buttonPin, INPUT);
}



// - - - Start of Main Loop - - -

void loop() {

  pressLength = 0;


  while (digitalRead(buttonPin) == HIGH) {
    delay(Timer);
    pressLength += Timer;
    //    Serial.println(pressLength);
  }

  if (pressLength >= optionOne && lightState == false) { // power on blade
    colorState = 1;
    lightState = powerUp();
  }

  else if (pressLength >= optionOne && lightState == true) { // power off blade
    lightState = powerDown();
  }

  else if (pressLength >= optionTwo && lightState == true) { // change blade color
    colorState = changeColor();
    //    lightState = true;
    delay(20);
  }
}
// - - - end of Main Loop - - -






// - - - Functions - - -


bool powerUp() { // power on saber

  for (int i = 0; i <= NUM_LEDS; i++) {
    leds[i] = CRGB(0, 100, Brightness);
    delay(LEDTime);
    FastLED.show();
  }
  return true;
}


bool powerDown() { // power down saber

  for (int i = NUM_LEDS; i >= 0; i--) {
    leds[i] = CRGB(0, 0, 0);
    delay(LEDTime);
    FastLED.show();
  }
  return false;
}



int changeColor() { // Change the color of the blade
  
  Serial.print("color Change  ");
  Serial.println(colorState);
  Serial.print("lightState ");
  Serial.println(lightState);
  delay(20);

  if (colorState == 0) {
    colorState = colorLightBlue();
    Serial.print("Light Blue ");
    Serial.println(lightState);
    return 1;
  }

  else if (colorState == 1) {
    colorState = colorBlue();
    Serial.print("Blue  ");
    Serial.println(lightState);
    return 2;
  }

  else if (colorState == 2) {
    colorState = colorRed();
    Serial.println("red  ");
    Serial.println(lightState);
    return 3;
  }

  else if (colorState == 3) {
    colorState = colorGreen();
    Serial.print("green  ");
    Serial.println(lightState);
    return 4;
  }

  else if (colorState == 4) {
    colorState = colorPurple();
    Serial.print("purple  ");
    Serial.println(lightState);
    return 0;
  }

  else {
    return 0;
  }

}

int colorLightBlue () {
  for (int i = 0; i <= NUM_LEDS; i++) {
    leds[i] = CRGB(0, 100, Brightness);
  }
  FastLED.show();
  return 1;
}


int colorBlue() {
  for (int i = 0; i <= NUM_LEDS; i++) {
    leds[i] = CRGB(0, 0, Brightness);
  }
  FastLED.show();
  return 2;
}

int colorRed() {
  for (int i = 0; i <= NUM_LEDS; 4) {
    leds[i] = CRGB(Brightness, 00, 0);
  }
  FastLED.show();
  return 3;
}


int colorGreen() {
  for (int i = 0; i <= NUM_LEDS; i++) {
    leds[i] = CRGB(0, Brightness, 0);
  }
  FastLED.show();
  return 4;

}

int colorPurple() {
  for (int i = 0; i <= NUM_LEDS; i++) {
    leds[i] = CRGB(188, 0, 211);
  }
  FastLED.show();
  return 0;
}

You are writing beyond the end of your led array. It goes from 0…NUM_LEDS-1 and you are writing to led[NUM_LEDS] which is one too far. This is undefined behavior but most likely, the very next memory location is your colorState variable and it is getting stomped.

You don’t need to use all those for() loops to fill the led array since FastLED provides a fill_solid() function which will do it for you. You can eliminate a lot of duplicate code as well since you really don’t need to have each color in its own function.

Finally, do you have a pull-down resistor on your button? The way you have your code, you will need it to make sure the button is LOW when not pressed. If not, you can declare the button as INPUT_PULLLUP and wire the other side of the button to ground. This reverses the logic of the button (LOW = pressed) but ensures the pin never “floats”

#include <FastLED.h>

#define buttonPin 2
#define LED_PIN 6
#define NUM_LEDS 58

CRGB leds[NUM_LEDS];

const byte Brightness = 200;
const int LEDTime = 40; // delay time between each LED
const int Timer = 100;
unsigned long pressLength = 0; // # how long the button is held down in milliseconds
bool lightState = false; // State of light, on or off.
int colorState = 0;

/*
    - - -  Options for different durations of pressLength (milliseconds) - - -
  Remember to put conditions with longer pressLength first

*/

const int optionOne = 500; // power up saber and power down Saber
const int optionTwo = 20; // Change Blade color, Light Blue, Blue, Red, Green, Purple

void setup() {
  Serial.begin(9600);
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  pinMode(buttonPin, INPUT);
}


// - - - Start of Main Loop - - -

void loop() {

  pressLength = 0;

  while (digitalRead(buttonPin) == HIGH) {
    delay(Timer);
    pressLength += Timer;
    //    Serial.println(pressLength);
  }

  if (pressLength >= optionOne && lightState == false) {
    // power on blade
    powerUp();
    colorState = 0;
    lightState = true;
  }

  else if (pressLength >= optionOne && lightState == true) {
    // power off blade
    powerDown();
    lightState = false;
  }

  else if (pressLength >= optionTwo && lightState == true) {
    // change blade color
    changeColor();
    delay(20);
  }
}
// - - - end of Main Loop - - -

// - - - Functions - - -

void powerUp() { // power on saber

  for (int i = 0; i < NUM_LEDS; i++) {
    leds[i] = CRGB(0, 100, Brightness);
    delay(LEDTime);
    FastLED.show();
  }
}


void powerDown() { // power down saber

  for (int i = NUM_LEDS - 1; i >= 0; i--) {
    leds[i] = CRGB(0, 0, 0);
    delay(LEDTime);
    FastLED.show();
  }
}


void changeColor() { // Change the color of the blade

  Serial.print("color Change  ");
  Serial.println(colorState);
  Serial.print("lightState ");
  Serial.println(lightState);
  delay(20);

  switch (colorState) {
    case 0:
      Serial.print("Light Blue ");
      fill_solid(leds, NUM_LEDS, CRGB(0, 100, Brightness));
      break;

    case 1:
      Serial.print("Blue  ");
      fill_solid(leds, NUM_LEDS, CRGB(0, 0, Brightness));
      break;

    case 2:
      Serial.println("red  ");
      fill_solid(leds, NUM_LEDS, CRGB(Brightness, 0, 0));
      break;

    case 3:
      Serial.print("green  ");
      fill_solid(leds, NUM_LEDS, CRGB(0, Brightness, 0));
      break;

    case 4:
      Serial.print("purple  ");
      fill_solid(leds, NUM_LEDS, CRGB(188, 0, 211));
      FastLED.show();
  }
  FastLED.show();
  colorState++;
  if (colorState > 4) colorState = 0;
}

thank you for all your suggestions!
Fixed everything!

i am using a pullup resistor, i made that mistake before.

looks i need to read some more about the FastLED library.

thank you for you help!!!!