Stopping a loop, neopixel and infrared.

Hi All, this is my first post here, great place to start learning!!!.

I’m trying to create a sort of lamp using neopixel, arduino nano, and an infrared sensor with its remote control. every time i press a button on the remote control the strip of leds changes color, no problem with that. I am having a problem creating some effect like strobe, or flashing. Let me explain, once i press, for example the strobe button on my control a loop start making the strip blinking, but once the loop starts there is no way to stop it. I would like to be able to change colors and even effects during the loop execution. I do not know if i am clear enough, this is the code:

#include <Adafruit_NeoPixel.h>
#define PIN 6
#define N_LEDS 30

Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_LEDS, PIN, NEO_GRB + NEO_KHZ800);

// IR sensor connected to Arduino D11
#define IRpin_PIN      PINB
#define IRpin          3

#define MAXPULSE 5000 // max IR pulse length, default 5 milliseconds
#define NUMPULSES 100 // max IR pulse pairs to sample
#define RESOLUTION 2 // time between IR measurements

uint16_t pulses[NUMPULSES][2];  // pair is high and low pulse 
uint8_t currentpulse = 0; // index for pulses we're storing
int bro = 255;
void setup() {
 strip.begin();
 Serial.begin(9600);
 Serial.println("TinyPCRemote_CodeReader Ready!");
 pinMode(11, INPUT); // Set IR pin as input
 pinMode(13, OUTPUT); //power pin for ir rx
 digitalWrite(13, HIGH);
 strip.show();
}

void loop() {  

 unsigned int highpulse, lowpulse;  // temporary storage timing
 highpulse = lowpulse = 0; // start out with no pulse length 
  
 while (IRpin_PIN & _BV(IRpin)) { // got a high pulse
    highpulse++;
    delayMicroseconds(RESOLUTION);
    if (((highpulse >= MAXPULSE) && (currentpulse != 0))|| currentpulse == NUMPULSES) {
      currentpulse=0;
      printpulses();
      return;
    }
 }
 pulses[currentpulse][0] = highpulse;
 
 while (! (IRpin_PIN & _BV(IRpin))) { // got a low pulse
    lowpulse++;
    delayMicroseconds(RESOLUTION);
    if (((lowpulse >= MAXPULSE)  && (currentpulse != 0))|| currentpulse == NUMPULSES) {
      currentpulse=0;
      printpulses();
      return;
    }
 }
 pulses[currentpulse][1] = lowpulse;
 currentpulse++;

}

void printpulses() {
  
 unsigned long irCode=0;
  
 for (int i = 0; i < 32; i++) {
   irCode=irCode<<1;
   if((pulses[i][0] * RESOLUTION)>0&&(pulses[i][0] * RESOLUTION)<500) {
    irCode|=0; 
   } else {
    irCode|=1;
   }
 }

 Serial.print("Code for this button: ");  
 Serial.println(irCode);

   switch (irCode) {
     case 3225276463:
      chase(strip.Color(0, 0, 0));//Off
      break;
     case 3225274423:
      chase(strip.Color(255, 0, 0));//Red
      break;
     case 3225282583:
      chase(strip.Color(0, 102, 0));//Green
      break;
     case 3225278503:
      chase(strip.Color(0, 0, 204));//Blue
      break;
     case 3225277483:
      chase(strip.Color(51, 51, 255));//Light Blue
      break;
     case 3225279523:
      chase(strip.Color(51, 5, 97));//Dark Wine
      break;
     case 3225272893:
      chase(strip.Color(255, 131, 51));//Sand
      break;
     case 3225286663:
      chase(strip.Color(255, 255, 255));//Withe
      break;
     case 3225274933:
      chase(strip.Color(255, 255, 0));//Yellow
      break;
     case 3225281563:
      chase(strip.Color(26, 255, 26));//Spring Green
      break;
     case 3225283603:
      chase(strip.Color(0, 125, 255));//Cyan
      break;
     case 3225275443:
      chase(strip.Color(255, 163, 102));//Orange
      break;
     case 3225279013:
      chase(strip.Color(255, 102, 178));//Dark Pink
      break;
     case 3225276973:
      chase(strip.Color(102, 0, 102));//Light  Pink
      break;
     case 3225281053:
      chase(strip.Color(0, 102, 102));//Dark Cyan
      break;
     case 3225273403:
      chase(strip.Color(255, 50, 0));//Dark Orange
      break;
     case 3225283093:
      chase(strip.Color(0, 50, 51));//Dark Green
      break;
     case 3225285643:
      FlashStrobe(strip.getPixelColor(11),irCode);//Flash
      break;
     case 3225287683:
      FlashStrobe(strip.getPixelColor(11),irCode);//Strobe
      break;
     case 3225272383:
      bro = bro + 10;
      strip.setBrightness(bro);//+light
      strip.show();
      break;
     case 3225280543:
      bro = bro - 10;
      strip.setBrightness(bro);//-light
      strip.show();
      break;
    default: 
      // if nothing else matches, do the default
      // default is optional
    break;
  }
}
 
 static void chase(uint32_t c) {
  for(uint16_t i=0; i<strip.numPixels()+4; i++) {
      strip.setPixelColor(i  , c); // Draw new pixel
      //strip.setPixelColor(i-4, 0); // Erase pixel a few steps back
      strip.show();
      delay(25);
  }
}

static void FlashStrobe(uint32_t c, unsigned long code){
  int f = 0;
  int millSec = 150;
  if(code==3225287683){
    millSec =20;
  }
  
  while(f<100){
   for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
   }
   delay(millSec);
   for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, 0);
      strip.show();
   }
   delay(millSec);
   f++;
  }
}

Sorry for my english.

Simone

You need to ditch the for loops and delay and try to write non-blocking code. That means that each one of the functions you have shouldn't run through the entire process but should check to see if it is time to take the next step, take it if so, and return. Then your main loop can check for a new IR code and if it gets one it can do something different. If not it can call the same function again and take the next step in that routine.

The Blink Without Delay example is a good place to start learning about non-blocking code.

Thank you so much for your reply, i have started to read The Blink Without Delay example and make a lot of sense, the only thing i am having troubles is how to applied that to my code. Where should i put the if statement in my loop?

Simone

It's not a matter of copying that code into yours. Actually it is going to look very very different in your code. The thing you gotta do is understand the strategy involved and apply that strategy to restructuring the logic in your code.