While loop with remote button input

I’m brand new to Arduino programming and it’s been over a decade since I’ve done any programming at all, so this is all basically new to me. I am working on programming a string of lights to behave like the ones in the show Stranger Things. I’ve figured out how to write a program to turn on individual lights on my string, control the color and duration and change to the next one and repeat that loop, which is exactly what I want it to do.

The code only works for one sequence however so I had the idea to program in a remote so I could program many sequences and change them with an IR remote button press. I’ve written a code that will let me us a remote that seems to interface with my lights just fine, however, I can’t seem to figure out how to loop the sequence inside of the button press command. Ideally, what I want it to do is wait for me to press a button then when it receives a button signal, start a sequence loop until it receives a different button press. From searching around, it sounds like I need to use a while loop, but I can not figure out how to use it properly. When I add it in where I think it needs to be, it stops the button signal from working at all.

Here is the code as I have it written currently. Right now this runs and when I press the two buttons programmed on the remote, the sequence of turning on and off an LED once works just fine.

#include <IRremote.h>
#include <FastLED.h>
#define NUM_LEDS 50
#define DATA_PIN 6
int IRpin = 11; // pin for the IR sensor
IRrecv irrecv(IRpin);
decode_results results;
CRGB leds[NUM_LEDS];

void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}

void loop()
{

if (irrecv.decode(&results))
{

irrecv.resume(); // Receive the next value
}

switch(results.value)
{

case 0xFFA25D:
// Turn the first led red for 1 second
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
leds[0] = CRGB::Red;
FastLED.show();
delay(1000);
// Set the first led back to black for 1 second
leds[0] = CRGB::Black;
FastLED.show();
delay(1000);
break;

case 0xFF629D:
// Turn the second led red for 1 second
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
leds[1] = CRGB::Red;
FastLED.show();
delay(1000);
// Set the second led back to black for 1 second
leds[1] = CRGB::Black;
FastLED.show();
delay(1000);
break;

}

}

You should separate setting the mode for your light effect and performing the actual effect. Here is some code with details left out:

// possible modes for light effects
const int MONSTER_MODE = 0;
const int WILL_BYERS_MODE = 1;

// current light effect mode
int currentMode = (-1);

void setup()
{

}

void loop()
{

  // set mode from button press
  switch(results.value)
  {

  case 0xFFA25D:
    currentMode = MONSTER_MODE;
    break;

  case 0xFF629D:
    currentMode = WILL_BYERS_MODE;
    break;

  default:
    break;

  } // switch

  // control lights based on mode
  switch( currentMode)
  {

  case MONSTER_MODE:

    // Turn the first led red for 1 second
    FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
    leds[0] = CRGB::Red;
    FastLED.show();
    delay(1000);
    // Set the first led back to black for 1 second
    leds[0] = CRGB::Black;
    FastLED.show();
    delay(1000);
    break;

  case WILL_BYERS_MODE:
    // Turn the second led red for 1 second
    FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
    leds[1] = CRGB::Red;
    FastLED.show();
    delay(1000);
    // Set the second led back to black for 1 second
    leds[1] = CRGB::Black;
    FastLED.show();
    delay(1000);
    break;

  default:
    break;

  } // switch

}

Remember that the built-in loop function keeps running and running so unless you change the mode it will keep doing the same effect. The first switch should be inside your if just before the call to irrecv.resume().

Thanks, I'll give that a go and see if it works. Visually it looks easier to understand it broken out like that too.

Also, kudos for the mode names.

Fixed it completely, thanks!

Hopefully I don't run out of program storage space. That simple program takes up 39%.

My god, it worked :smiley: Thanx for posting this question OP and big thanx to ‘Blue Eyes’ for the solution. I have literally been stuck for almost two weeks with my project. I was just trying to keep the servo in a loop with one button and then stop it with another one. As I am also new to Arduino, I have been trying to solve this with while loop, but ofcourse it did not work; also tried with if statments, switch-case, however incorrectly as it turns out. But now, with your switch-case solution, its voila moment for me :slight_smile: I am so happy right now, at 3 am :smiley:
I just do not understand clearly why the value of “currentMode” is set to “-1”, if you could explain me that briefly, I would appreciate much, or point me a link or so about it.

Thanx again!

Apparently I am even newer to the Arduino. I tried to combine the original code with the suggested fixes that fixed the issue, but I have not got it to compile without errors. Is there a way you could please post the entire code as in working order so I can continue trying to do my project for the Christmas Parade Float??

I tried to combine the original code with the suggested fixes that fixed the issue, but I have not got it to compile without errors.

So, post YOUR code, and the errors.