Help with a looping issue

Ok so i built a cloud for the little girl where she can select the type of setting using a IR remote. Everything looks to work fine except two things.

  1. The rainbow and lightening functions only run once instead of looping. Thoughts? Do i need a if/ else if statement or switch case?

  2. I cannot interrupt the rainbow or lightening functions (with a new input) without waiting for it to complete its cycle. Any thoughts on how to fix this??

Also i apologize for the crazy code. I cobbled it together using the Sparkfun cloud cloud code (remove and added a bunch) so it is a bit of a mess…

storm_coud.ino (12.4 KB)

Don't turn off the IR receiving code while the animations are running.

Why would you expect the rainbow or lightning functions to loop? You're not calling it in a loop, you just call it once when it gets the IR signal to do it.

You need to separate the code where you read the ir value and start the lighting effect. Here's my idea:

First define a new global variable which will run the effect.

unsigned log showType = 0x0;

Then define a function that sets the showType if a code is ready and returns true if so.

// return true if a code is available
boolean checkIR()
{
  bool retVal = false;

  if( irrecv.decode(&results) )
  {
    showType = results.value;  // save code
    irrecv.resume();
    retVal = true;

  }

  return retVal;
}

Now your loop code can use the new variable.

void loop() {

  checkIR();  // read an IR code

  if (showType == C) 
  {
    sunSet();
  }
  if (showType == A) 
  {
    blueSky();
  }
  if (showType == B) 
  {
    lighening();
  }
  if (showType == UP) 
  {
    overcast();
  }  
  if (showType == DOWN) 
  {
    whiteClouds();
  }
  if (showType == LEFT) 
  {
    pink();
  }
  if (showType == RIGHT) 
  {
    rainbow(20);
  }


}

This way the last showType set will repeat. To return from one of the effects when a new button is pressed keep calling checkIR() and exit the effect if it returns true.

Thanks.

I put the above into my code (maybe wrong) and i get the same results except i have to press the ir remote button twice to get it change. I did have to modify unassigned log to long.

It still doesn’t interrupt the lightening and rainbow functions. Thoughts? My cleaned up, modified code is attached. I am also still having issues trying to get the lightening and rainbow functions to loop.

storm_coud3.ino (14.9 KB)

I was able to get the rainbow function to loop by adding strip.show_a() to the end of the rainbow function but the same doesn't work for the lightening () function. I also cannot get the ir inputs the function and switch to another.

Thoughts?