IR Cloud Control

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 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…

// Debug
#define DEBUG    1

#include <Adafruit_NeoPixel.h>
#include <avr/power.h>
#include <IRremote.h>

#ifdef __AVR__
  #include <avr/power.h>
#endif

// Pin definitions
#define CLOUD_A_PIN       6

// Other constants
const int TWINKLE_DELAY = 400;    // ms
const int IN_BUF_MAX = 7;         // bytes
const byte SERIAL_SOF = 0xA5;
const byte SERIAL_EOF = 0x5A;
const int TIMEOUT = 100;          // ms
const int DISCO_DELAY = 85;       // ms
const byte COMM_RGB = 1;
const byte COMM_DISCO = 2;
const byte COMM_BLUE_SKY = 3;
const byte COMM_NIGHT = 4;
const byte COMM_OVERCAST = 5;
const byte COMM_GOLDEN = 6;
const byte COMM_SNOW = 7;
const byte COMM_LIGHTNING = 8;

Adafruit_NeoPixel strip_a = Adafruit_NeoPixel(150, CLOUD_A_PIN, NEO_GRB + NEO_KHZ800);

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel.  Avoid connecting
// on a live circuit...if you must, connect GND first.


int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
#define POWER 0x10EFD827 
#define A 0x10EFF807 
#define B 0x10EF7887
#define C 0x10EF58A7
#define UP 0x10EFA05F
#define DOWN 0x10EF00FF
#define LEFT 0x10EF10EF
#define RIGHT 0x10EF807F
#define SELECT 0x10EF20DF

void setup() {

  // Set initial random delay
  pinMode(A0, INPUT);
  randomSeed(analogRead(A0));
  irrecv.enableIRIn(); // Start the receiver
  Serial.begin(9600);
  strip_a.begin();
  strip_a.show();

}

void loop() {
if (irrecv.decode(&results)) 
  {
    if (results.value == C) 
    {
      sunSet();
    }
      if (results.value == A) 
    {
      blueSky();
    }
      if (results.value == B) 
    {
      lighening();
    }
     if (results.value == UP) 
    {
      overcast();
    }  
     if (results.value == DOWN) 
    {
      whiteClouds();
    }
    if (results.value == LEFT) 
    {
      pink();
    }
    if (results.value == RIGHT) 
    {
      rainbow(20);
    }
    irrecv.resume();
  }
}

  
  

// Bluesky function
void blueSky() {
  for(int i=0; i<300; i++) {    //for all of the LEDs 
    strip_a.setPixelColor(i, 0, 170, 175);   //set LEDs a sky blue color in cloud one
 }
  strip_a.show();
}
void sunSet(){  
  int i;
  for(i=0; i<30; i++) {   //for the first 100 LEDs
    strip_a.setPixelColor(i, 150, 20, 0);   //set LEDs red-ish in cloud one
  }
  for(i=30; i<60; i++) {  //for LEDs 101 to 150
    strip_a.setPixelColor(i, 150, 90, 0);  //set LEDs orange-red in cloud one
  }
  for(i=60; i<90; i++) {  //for LEDs 151 to 250
    strip_a.setPixelColor(i, 150, 50, 0);  //set LEDs red-orage in cloud one
  }
  for(i=90; i<149; i++) {  //for LEDs 251 to 260
    strip_a.setPixelColor(i, 150, 50, 0);  //set LEDs red-orage in cloud one
  }

    strip_a.show(); //show all the colors that were set in cloud one
}

// White clouds function
void whiteClouds() {
  for(int i=0; i<150; i++) {   //for all of the LEDs 
    strip_a.setPixelColor(i, 100, 100, 100);  //set LEDs white-ish in cloud one

  }
  strip_a.show();

}


// Overcast funtion
void overcast() {
  for(int i=0; i<150; i++) {   //for all of the LEDs 
            strip_a.setPixelColor(i, 70, 64, 75);  //set LEDs grey-ish in cloud one
   }
   strip_a.show();
}

// Pink funtion
void pink() {
  for(int i=0; i<150; i++) {   //for all of the LEDs 
            strip_a.setPixelColor(i, 100, 0, 100);  //set LEDs pink-ish in cloud one
   }
   strip_a.show();
}


void rainbow(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<strip_a.numPixels(); i++) {
      strip_a.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip_a.show();
    delay(wait);
  }
}


uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip_a.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip_a.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip_a.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

/****************************************************************
 * LED Strip - Lightning Functions
 ***************************************************************/

void singleFlash_a(int areaMin, int areaMax, int duration, int Size, uint32_t color) {
  int i;
  int flashStrike = random(areaMin, areaMax);
  for ((i = flashStrike - Size); (i < flashStrike); i++) {
   strip_a.setPixelColor(i, 255,255,255);//color);
 }
 strip_a.show();
 delay(duration);
 for ((i = flashStrike - Size); (i < flashStrike); i++) {
   strip_a.setPixelColor(i, 0, 0, 0);
 }
 strip_a.show();
}

void flickerFlash_a(int areaMin, int areaMax, int duration, int Size, uint32_t color) {
  int i;
  int flashStrike = random(areaMin, areaMax);
  for ((i = flashStrike - Size); (i < flashStrike); i++) {
   strip_a.setPixelColor(i, color);
 }
 strip_a.show();
 delay(50);
 for ((i = flashStrike - Size); (i < flashStrike); i++) {
   strip_a.setPixelColor(i, 0, 0, 0);
 }
 strip_a.show();
 delay(50);
 flashStrike = random(areaMin, areaMax);
  for ((i = flashStrike - Size); (i < flashStrike); i++) {
   strip_a.setPixelColor(i, color);
 }
 strip_a.show();
 delay(duration);
 for ((i = flashStrike - Size); (i < flashStrike); i++) {
   strip_a.setPixelColor(i, 0, 0, 0);
 }
 strip_a.show();
}



void scrollingFlash_a(int areaMin, int areaMax, int duration, int Size, uint32_t color) {
  int i;
  int flashStrike = random(areaMin, areaMax);
  for ((i = flashStrike - Size); (i < flashStrike); i++) {
    strip_a.setPixelColor(i, color);
    strip_a.show();
  }
  for ((i = flashStrike - Size); (i < flashStrike); i++) {
    strip_a.setPixelColor(i, 0, 0, 0);
    strip_a.show();
  }
  for ((i = flashStrike - Size); (i < flashStrike); i++) {
    strip_a.setPixelColor(i, color);
    strip_a.show();
  }
  delay(duration);
  for ((i = flashStrike - Size); (i < flashStrike); i++) {
    strip_a.setPixelColor(i, 0, 0, 0);
    strip_a.show();
  }
} 





void multipleFlashs_a(int areaOneMin, int areaOneMax, int areaTwoMin, int areaTwoMax, int duration, int Size, uint32_t color){
 int i;
 int oneStrike = random(areaOneMin, areaOneMax);
 int twoStrike = random(areaTwoMin, areaTwoMax);
 for ((i = oneStrike - Size); (i < oneStrike); i++) {
   strip_a.setPixelColor(i, color);
   strip_a.show();
 }
 for ((i = twoStrike - Size); (i < twoStrike); i++) {
   strip_a.setPixelColor(i, color);
   strip_a.show();
 }
 delay(duration);
 for ((i = oneStrike - Size); (i < oneStrike); i++) {
   strip_a.setPixelColor(i, 0, 0, 0);
   strip_a.show();
 }
 for ((i = twoStrike - Size); (i < twoStrike); i++) {
   strip_a.setPixelColor(i, 0, 0, 0);
   strip_a.show();
 }
}



void jumpingFlash_a(int areaStart, int areaEnd, int duration, uint32_t color) {
 int i;
 for (i = areaStart; (i < areaStart + 15); i++){
   strip_a.setPixelColor(i, color);
 }
 strip_a.show();
 delay(duration);
 for (i = areaStart; (i < areaStart + 15); i++){
   strip_a.setPixelColor(i, 0, 0, 0);
   strip_a.show();
 }
 for ((i = areaEnd - 15); i < areaEnd; i++){
   strip_a.setPixelColor(i, color);
 }
 strip_a.show();
 delay(duration);
 for ((i = areaEnd - 15); i < areaEnd; i++){
   strip_a.setPixelColor(i, 0, 0, 0);
 }
 strip_a.show();
 delay(duration);
 for ((i = areaEnd - 15); i < areaEnd; i++){
   strip_a.setPixelColor(i, color);
 }
 strip_a.show();
 delay(duration + 50);
 for ((i = areaEnd - 15); i < areaEnd; i++){
   strip_a.setPixelColor(i, 0, 0, 0);
   strip_a.show();
 }
}





void wholeCloudFlash_a(int durationShort, int durationLong, uint32_t color) {
  int i;
  for (i = 60; i < 75; i++){
   strip_a.setPixelColor(i, color);
 }
 strip_a.show();
 delay(durationShort);
 for (i = 60; i < 75; i++){
   strip_a.setPixelColor(i, 0, 0, 0);
 }
    strip_a.show();
    delay(durationLong);
 for (i = 60; i < 75; i++){
   strip_a.setPixelColor(i, color);
   strip_a.show();
 }
 delay(durationShort);
 for (i = 60; i < 75; i++){
   strip_a.setPixelColor(i, 0, 0, 0);
   strip_a.show();
 }
  for (i = 100; i < 115; i++){
   strip_a.setPixelColor(i, color);
   strip_a.show();
 }
 for (i = 100; i < 115; i++){
   strip_a.setPixelColor(i, 0, 0, 0);
   strip_a.show();
 }
 for (i = 100; i < 115; i++){
   strip_a.setPixelColor(i, color);
   strip_a.show();
 }
 delay(durationShort);
 for (i = 100; i < 115; i++){
   strip_a.setPixelColor(i, 0, 0, 0);
   strip_a.show();
 }
 for (i = 115; i < 130; i++){
   strip_a.setPixelColor(i, color);
 }
 strip_a.show();
 delay(durationShort);
 for (i = 115; i < 130; i++){
   strip_a.setPixelColor(i, 0, 0, 0);
 }
 strip_a.show();
 delay(durationShort);
 for (i = 115; i < 130; i++){
   strip_a.setPixelColor(i, color);
 }
 strip_a.show();
 delay(durationShort);
 for (i = 115; i < 130; i++){
   strip_a.setPixelColor(i, 0, 0, 0);
   strip_a.show();
 }
 for (i = 130; i < 150; i++){
   strip_a.setPixelColor(i, color);
 }
 strip_a.show();
 delay(durationShort);
 for (i = 130; i < 150; i++){
   strip_a.setPixelColor(i, 0, 0, 0);
 }
 strip_a.show();
 delay(durationShort);
 for (i = 130; i < 150; i++){
   strip_a.setPixelColor(i, color);
 }
 strip_a.show();
 delay(durationLong);
 for (i = 130; i < 150; i++){
   strip_a.setPixelColor(i, 0, 0, 0);
   strip_a.show();
 }
}
void clearCloud(){
  for (int i = 0; i < 300; i++){ //for all the LEDs
    strip_a.setPixelColor(i, 0, 0, 0); //turn off in cloud one
  }
  strip_a.show(); //show what was set in cloud one
}

void lighening()
{
  clearCloud();
     jumpingFlash_a(50, 80, 50, strip_a.Color(255,255,255));
        jumpingFlash_a(50, 80, 50, strip_a.Color(255,255,255));
        scrollingFlash_a(20, 65, 50, 5, strip_a.Color(255,255,255));
        singleFlash_a(100, 200, 50, 15, strip_a.Color(255,255,255));
        singleFlash_a(100, 200, 50, 15, strip_a.Color(255,255,255));
        singleFlash_a(100, 200, 50, 15, strip_a.Color(255,255,255));
        scrollingFlash_a(200, 250, 50, 15, strip_a.Color(255,255,255));
        singleFlash_a(50, 100, 50, 5, strip_a.Color(200,200,255));
        scrollingFlash_a(200, 250, 50, 15, strip_a.Color(255,255,255));
        jumpingFlash_a(100, 130, 50, strip_a.Color(255,255,255));
        jumpingFlash_a(100, 130, 50, strip_a.Color(255,255,255));
        multipleFlashs_a(20, 125, 150, 300, 50, 5, strip_a.Color(255,255,255));
        flickerFlash_a(50, 300, 50, 25, strip_a.Color(200,200,255));
        multipleFlashs_a(20, 125, 150, 300, 50, 5, strip_a.Color(255,255,255));
        scrollingFlash_a(10, 60, 100, 15, strip_a.Color(225,200,255));
        flickerFlash_a(75, 175, 40, 25, strip_a.Color(255,255,255));
        flickerFlash_a(75, 175, 40, 25, strip_a.Color(255,255,255));
        flickerFlash_a(75, 175, 40, 25, strip_a.Color(255,255,255));
        jumpingFlash_a(200, 130, 50, strip_a.Color(255,255,255));
        flickerFlash_a(75, 175, 40, 25, strip_a.Color(255,255,255));
        flickerFlash_a(50, 300, 50, 25, strip_a.Color(200,200,255));
        scrollingFlash_a(200, 250, 100, 10, strip_a.Color(255,255,255));
        wholeCloudFlash_a(40, 100, strip_a.Color(255,255,255));
        multipleFlashs_a(20, 125, 175, 300, 50, 5, strip_a.Color(255,255,255));
        scrollingFlash_a(20, 65, 50, 3, strip_a.Color(255,255,255));
        flickerFlash_a(0, 100, 50, 50, strip_a.Color(200,200,255));
        flickerFlash_a(0, 100, 50, 50, strip_a.Color(200,200,255));
        singleFlash_a(75, 175, 40, 3, strip_a.Color(255,255,255));
        singleFlash_a(100, 200, 50, 15, strip_a.Color(255,255,255));
        scrollingFlash_a(20, 65, 50, 5, strip_a.Color(255,255,255));
        multipleFlashs_a(20, 125, 200, 300, 50, 5, strip_a.Color(255,255,255));
        singleFlash_a(100, 200, 50, 30, strip_a.Color(255,255,255));
        flickerFlash_a(0, 100, 50, 50, strip_a.Color(200,200,255));
        scrollingFlash_a(200, 500, 50, 15, strip_a.Color(255,255,255));
        jumpingFlash_a(250, 300, 50, strip_a.Color(255,255,255));
        jumpingFlash_a(250, 300, 50, strip_a.Color(255,255,255));
        multipleFlashs_a(20, 125, 200, 300, 50, 5, strip_a.Color(255,255,255));
        scrollingFlash_a(10, 60, 100, 7, strip_a.Color(225,200,255));
        singleFlash_a(75, 175, 40, 3, strip_a.Color(255,255,255));
        singleFlash_a(75, 175, 40, 3, strip_a.Color(255,255,255));
        jumpingFlash_a(150, 180, 50, strip_a.Color(255,255,255));
        jumpingFlash_a(50, 80, 50, strip_a.Color(255,255,255));
        scrollingFlash_a(250, 300, 50, 5, strip_a.Color(255,255,255));
        flickerFlash_a(0, 100, 50, 50, strip_a.Color(200,200,255));
        scrollingFlash_a(250, 300, 50, 5, strip_a.Color(255,255,255));
        multipleFlashs_a(20, 125, 150, 300, 50, 5, strip_a.Color(200,200,255));
        wholeCloudFlash_a(40, 100, strip_a.Color(255,255,255));
        
}