Help with case code not working

Hey. It's been a while. I am close to finishing this code for a model kit for a friend, and I can't seem to get one of the case functions to work. Code follows. On power up, the FastLED strip is dark. With

button push 1, the string lights up blue in a sequential manner.
Button push 2 should send the strip into a build in intensity and a white flash, returning to the previous blue.
Push 3 fades to black, and
push 4 resets the Arduino to be able to go again.

The problem is that the case 2 FN_WARP does not do anything. The serial print registers the click, but no action is seen.

What am I missing?

Sketch:

/* Enterprise E warp nacelle functions
 *  0-start black on power-up
 *  1-fill in sequence blue
 *  2- warp flash - will not work**. 
 *  3-fadeout
 *  4- reset
 *  **-code by itself does: fade up blue to white @100,flash white @ 250,return blue
 *   this warp effect is the one I need help with- what am I missing??
 *   
  */
  
void(* resetFunc) (void) = 0;

#include <OneButton.h>

#include <FastLED.h>
#define NUM_LEDS  20
#define LED_PIN   2
#define BTN_PIN   14
// Push button connected between pin 7 and GND (no resistor required)
OneButton btn = OneButton(BTN_PIN, true, true);

CRGB leds[NUM_LEDS];
CRGB Warp[NUM_LEDS];
int hue = 160;
int saturation = 255;
int saturation2 = 0;
int brightness = 100;
int brightness2 = 200;
uint8_t patternCounter = 0;

//tell function to run once
int N = 1;
int runXTimes = 0;

void setup() {
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
 // FastLED.setBrightness(50);
  Serial.begin(9600);
  btn.attachClick(nextPattern);
}

void loop() {

  switch (patternCounter) {
    case 0:
      fadeToBlackBy(leds, NUM_LEDS, 8);
      Serial.println("begin");
      break;

    case 1:
      if (runXTimes < N)
      {
        lightup();
        runXTimes++;
      }
      Serial.println("lightup");
      break;

    case 2:
      FN_WARP();
      Serial.println("warp");
      break;

    case 3:
      fadeToBlackBy(leds, NUM_LEDS, 8);
      Serial.println("blackout");
      break;
    

    case 4:
      resetFunc();
      break;
  }

  FastLED.show();
  btn.tick();
}

void nextPattern() {
  patternCounter = (patternCounter + 1) % 5;           // Change the number after the % to the number of patterns you have
}

//------- Put your patterns below -------//

void lightup() {
  FastLED.clear();
  for (int i = 0; i < NUM_LEDS; i++) {
    leds[i].setRGB(0, 0, 255);
    FastLED.show();
    delay(80);
  }
}

void FN_WARP() {
  if (runXTimes < N)
  {
    FastLED.clear();
    delay(1000);
    fill_solid (Warp, 20,  CHSV (hue, saturation, brightness));


    FastLED.show();
    for (int i = 0; i < 40; i++) {
      saturation = saturation - 5;
      brightness = brightness + 4;

      fill_solid (Warp, 20,  CHSV (hue, saturation, brightness));

      FastLED.show();
      FastLED.delay(20);
    }
    fill_solid (Warp, 20,  CHSV (hue, saturation2, brightness2));

    FastLED.show();
    delay(250);
    brightness = 100;
    saturation = 255;
    fill_solid (Warp, 20,  CHSV (hue, saturation, brightness));
    FastLED.show();
  }
  runXTimes++;
}

Print runXTimes to reveal the trouble.

1st or 2nd ? or both? Should they be different? I'll give it a go.

In case 0 you have

        lightup();
        runXTimes++;

What will the value of runXTimes be after that when you test it in subsequent cases ?

X is increasing all the time. I know increment is "++", should it not be there? If I remove the line, or the ++, or try --, it doesn't stop after one loop. I admit I don't know exactly how this works- I have just been experimenting with examples I find, and I think I kinda know how it works, but obviously I do not, and I am not exactly a newbie, but not very skilled, either. I guess I know just enough to be able to paint myself into corner.

In case 0 you increment runXTimes from its initial value of 0 so it now has a value of 1

In case 1 and in the FN_WARP() function you test whether the value of runXTimes is less than the value of N, which is 1

Will that test ever return true ?

Ah, so I have to tell it to return to 0 in order for it to be true again, after the event. correct?

Yes

Thanks. And I can do that by just writing

runXTimes==0;

just before the break?

Nearly

I assume that you meant

runXTimes=0;
1 Like

Ah. It seems I need to call
runXTimes = 1;
to make it go once, otherwise it continues because I just set it again to <N.

So now I have it working. I had to also fix up leds, NUM_LEDS stuff. I am sure that there is a cleaner way to code this, but I am learning.

Thank you for pointing me to the right questions.

/* Enterprise E warp nacelle functions
    0-start black on power-up
    1-fill in sequence blue
    2- warp flash - will not work**.
    3-fadeout
    4- reset
 *  **-code by itself does: fade up blue to white @100,flash white @ 250,return blue
     this warp effect is the one I need help with- what am I missing??

*/

void(* resetFunc) (void) = 0;

#include <OneButton.h>

#include <FastLED.h>
#define NUM_LEDS  20
#define LED_PIN   2
#define BTN_PIN   14
OneButton btn = OneButton(BTN_PIN, true, true);
// Push button connected between pin 7 and GND (no resistor required)



CRGB leds[NUM_LEDS];

int hue = 160;
int saturation = 255;
int saturation2 = 0;
int brightness = 100;
int brightness2 = 200;
uint8_t patternCounter = 0;

//tell function to run once
int N = 1;
int M = 1;
int runXTimes = 0;
int runYTimes = 0;
void setup() {
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  // FastLED.setBrightness(50);
  Serial.begin(9600);
  btn.attachClick(nextPattern);
}

void loop() {

  switch (patternCounter) {
    case 0:
      fadeToBlackBy(leds, NUM_LEDS, 8);
      Serial.println("begin");
      break;

    case 1:
      if (runXTimes < N)
      {
        lightup();
        runXTimes++;
        Serial.println(runXTimes);
        Serial.println("lightup");

      } runXTimes = 1;
      break;

    case 2:
      FN_WARP();

      break;

    case 3:
      fadeToBlackBy(leds, NUM_LEDS, 8);
      Serial.println("blackout");
      break;

    case 4:
      resetFunc();
      break;
  }

  FastLED.show();
  btn.tick();
}

void nextPattern() {
  patternCounter = (patternCounter + 1) % 5;           // Change the number after the % to the number of patterns you have
}

//------- Put your patterns below -------//

void lightup() {
  FastLED.clear();
  for (int i = 0; i < NUM_LEDS; i++) {
    leds[i].setRGB(0, 0, 255);
    FastLED.show();
    delay(80);
  }
}

void FN_WARP() {
  if (runYTimes < M)
  { fill_solid (leds, NUM_LEDS,  CHSV (hue, saturation, brightness));
    FastLED.show();
    FastLED.delay(20);
    fill_solid (leds, NUM_LEDS,  CHSV (hue, saturation2, brightness2));
    FastLED.show();
    delay(250);
    brightness = 100;
    saturation = 255;
    fill_solid (leds, NUM_LEDS,  CHSV (hue, saturation, brightness));
    FastLED.show();
    runYTimes++;
  }
  runYTimes = 1;
  Serial.println("warp");
}
1 Like

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