(Question) Interrupt from IR Remote to end for-loops

I am currently a beginner.

I am trying to creat an IR Remote and use it to change my LED-Strip. My overall problem is that i can not change the mode of my LED-display if a mode is in an animation (for loop).
I Want to be able to change the mode, even if it did not complete.

Currently my loop is used to check if a IR signal is detected and if so, the "mapper()" method is used to determin what kind of button was pressed.

For instance:

  1. Button 1 is pressed
  2. Case sorts out what to do if Button 1 is pressed.
  3. Calls the "everyLedGreen()" method.

This works fine.

void loop()
{
  int tmpValue;
  if (irrecv.decode(&results)) // have we received an IR signal?
  {
    for (int i = 0; i < 23; i++)
    {
      if ((keyValue[i] == results.value) && (i<KEY_NUM))
      {
        //Serial.println(keyBuf[i]);
        mapper();
        tmpValue = results.value;
      }
      else if(REPEAT==i)
      {
        results.value = tmpValue;
      }
    }
    irrecv.resume(); // receive the next value
  }
}

void mapper()
{
  switch(results.value)
  {
    case 0xFFA25D: //on off
      if (STATUS){
        Serial.println("off");
        ledOff();}
      else{
      Serial.println("on");
        ledOn(); }
      break;
      
    case 0xFFE21D:  break;
    case 0xFF629D:  //Plus
      brightnessUP();
      break;
... etc
... etc

But if a method with a for loop is called. For example if i want to change every LED to violet but with a delay. I have to wait until the for loop ends until i can change to another mode.

void ledVioletProgression()
{
  for(int i = 0; i < NUM_LEDS; i++)  {
    int red = random(255);
    int blue = random(255);
    leds[i] = CRGB(red, 0, blue);
    delay(300);
    FastLED.show();
  } 
  
}

But i do not want to wait a whole cycle, until i can change it again.
I have heard of interrupts bevore. But how do i Implement an interrupt that is triggered by an remote? Because if i could trigger an interupt, i could change a variable und make an exit statment in the for-loop if the cariable is changed.

Is this possible? Is there a better way? Or am i completle in the wrong here?

I'm pretty new too, but what if you added a second condition for no IR signal in here?

what do you mean by mode and animation loop? they are not obvious from your code

would be more helpful if you post your entire code

which is button 1?

this could be implemented with a timer that is checked each iteration of loop(). no need for interrupts. but need to see entire code

Interrupts won't help you here.

To receive IR commands you have to call irrecv.decode(&results) frequently. NONE of your behaviors can use delay(), particularly in 'for' or 'while' loops. Switch to a Finite State Machine model for your behaviors.

Here my whole programm. It is a bit messy.
Can you elaborate what you mean by timer? How would a timer help me stop the for loop at any given time?
I dont mean to be rude. I just dont know how the conzept of a timer works on arduino. And how it would help me. Can you help me understand by giving an example?

I am sorry for using the word mode and animation interchangably.
Mode: a method witch changes all the LEDs at once and therfore goes back into the loop almost instantly.
Animation: a method witch changes the LEDs with an delay (for loop for example). So it takes time to process and therfor only jumps back to the loop after finishing.
In this time no IR signal can be processed, as it can only be processed in the loop, where the programm is not at.

I hope I cleard some things up.
Thank you for the support!

//www.elegoo.com
//2020.3.12


******************************************************
//

#include "IRremote.h"
#include "IR.h"

#include <FastLED.h>
#define LED_PIN     12 //pin of the board
#define NUM_LEDS    57 // number of LEDs

CRGB leds[NUM_LEDS];
bool STATUS = false; // are the LEDs Currently on or off? only relevant for the Power Button implementation
int BRIGHTNESS = 11; // overall starting brightness

IRrecv irrecv(RECEIVER);     // create instance of 'irrecv'
decode_results results;      // create instance of 'decode_results'

void setup() {
  Serial.begin(9600);
  Serial.println("IR Receiver Button Decode");
  irrecv.enableIRIn();
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(BRIGHTNESS);
}

******************************************************
//Loop checks if IR signal is there and cheks for implementation through the mapper method
//Not my code, but works somehow ^^
void loop()
{
  int tmpValue;
  if (irrecv.decode(&results)) // have we received an IR signal?
  {
    for (int i = 0; i < 23; i++)
    {
      if ((keyValue[i] == results.value) && (i<KEY_NUM))
      {
        //Serial.println(keyBuf[i]);
        mapper();
        tmpValue = results.value;
      }
      else if(REPEAT==i)
      {
        results.value = tmpValue;
      }
    }
    irrecv.resume(); // receive the next value
  }
}


******************************************************
//Mapps the Infrared Vaule of the remote to the implementation
void mapper()
{
  switch(results.value)
  {
    case 0xFFA25D: //on off
      if (STATUS){
        Serial.println("off");
        ledOff();}
      else{
      Serial.println("on");
        ledOn(); }
      break;
      
    case 0xFFE21D:  //FUNC/STOP
      break;
    case 0xFF629D:  //Plus
      brightnessUP();
      break;
    case 0xFF22DD: 
      break;
    case 0xFF02FD:    
      break;
    case 0xFFC23D:    
      break;
    case 0xFFE01F:    
      break;
    case 0xFFA857: //minus
      brightnessDOWN();    
      break;
    case 0xFF906F:    
      break;
    case 0xFF9867:    
      break;
    case 0xFFB04F:   
      break;
    case 0xFF6897:  //0
      randomLight();
      break;
    case 0xFF30CF:  //1
      randomRandom(); //every LED random color;
      break;
    case 0xFF18E7:  //2
      ledVioletProgression(); // change one LED at a time into purple 
      break;
    case 0xFF7A85: //3
      break;
    case 0xFF10EF: //4
      greenUP();
      break;
    case 0xFF38C7:  //5
      break;
    case 0xFF5AA5:  //6
      break;
    case 0xFF42BD: //7
      greenDOWN(); // not implemented
      break;
    case 0xFF4AB5: //8
      break;
    case 0xFF52AD: //9
      break;
    case 0xFFFFFFFF: ;
      break;  
    default: Serial.println(" other button   ");
  }
}
******************************************************
//Methods to use

void ledOn ()
{
  STATUS = true;
  randomLight();
}

void ledOff ()
{
  STATUS = false;
  for(int i = 0; i < NUM_LEDS; i++)  {
    leds[i] = CRGB(0, 0, 0);
    FastLED.show();
  } 
}

//bad minMax method. Change later
void minMaxBright(int a)
{
  if (a>255)
    BRIGHTNESS=255;
  if (a<0)
    BRIGHTNESS=0;
}

void brightnessUP()
{
  BRIGHTNESS = BRIGHTNESS+10;
  Serial.print(BRIGHTNESS);
  minMaxBright(BRIGHTNESS);
  FastLED.setBrightness(BRIGHTNESS);
}

void brightnessDOWN()
{
  BRIGHTNESS = BRIGHTNESS-10;
  Serial.print(BRIGHTNESS);
  minMaxBright(BRIGHTNESS);
  FastLED.setBrightness(BRIGHTNESS);
}

void randomLight()
{
   for(int i = 0; i < NUM_LEDS; i++)  {
    int red = random(255);
    int green = random(255);
    int yellow = random(255);
    leds[i] = CRGB(red, green, yellow);
    
  } 
  FastLED.show();
}

void greenUP()
{
  for(int i = 0; i < NUM_LEDS; i++)  {
    //CRGB lastcolor = leds[i];
    //int green = lastcolor.g + 10;
     int green = 50;
     
    if (i>30)
      int green = 5;
    else 
      int green = 255;
      
    leds[i] = CRGB(100, green, 0);
    //leds[i] = CRGB(lastcolor.r, lastcolor.b, green);
  } 
  FastLED.show();
}

void greenDOWN()
{
  
}

void minMax(int a)
{
  
  if (a>255)
    a=255;
  if (a<0)
    a=0;
    return a;
}

void randomRandom()
{
  
  for(int i = 0; i < NUM_LEDS; i++)  {
    int red = random(random(255));
    int green = random(random(255));
    int blue = random(random(100));
    leds[i] = CRGB(red, green, blue);
    
  } 
  FastLED.show();

}

//want to interupt for loop if is in progression. How?
void ledVioletProgression()
{
  for(int i = 0; i < NUM_LEDS; i++)  {
    int red = random(255);
    int blue = random(255);
    leds[i] = CRGB(red, 0, blue);
    delay(300);
    FastLED.show();
  } 
  
}
   

 
#ifndef _IR_H
#define _IR_H

#define RECEIVER 11

#define KEY_POWER (0xFFA25D)
#define KEY_FUNC_STOP (0xFFE21D)
#define KEY_VOL_ADD (0xFF629D)
#define KEY_FAST_BACK (0xFF22DD)
#define KEY_PAUSE (0xFF02FD)
#define KEY_FAST_FORWARD (0xFFC23D)
#define KEY_DOWN (0xFFE01F)
#define KEY_VOL_DE (0xFFA857)
#define KEY_UP (0xFF906F)
#define KEY_EQ (0xFF9867)
#define KEY_ST_REPT (0xFFB04F)
#define KEY_0 (0xFF6897)
#define KEY_1 (0xFF30CF)
#define KEY_2 (0xFF18E7)
#define KEY_3 (0xFF7A85)
#define KEY_4 (0xFF10EF)
#define KEY_5 (0xFF38C7)
#define KEY_6 (0xFF5AA5)
#define KEY_7 (0xFF42BD)
#define KEY_8 (0xFF4AB5)
#define KEY_9 (0xFF52AD)
#define KEY_REPEAT (0xFFFFFFFF)
#define KEY_NUM 21
#define REPEAT 22



unsigned long keyValue[]={KEY_POWER,KEY_FUNC_STOP,KEY_VOL_ADD,KEY_FAST_BACK,KEY_PAUSE,KEY_FAST_FORWARD,
                KEY_DOWN,KEY_VOL_DE,KEY_UP,KEY_EQ,KEY_ST_REPT,KEY_0,KEY_1,KEY_2,KEY_3,KEY_4,KEY_5,
                KEY_6,KEY_7,KEY_8,KEY_9,KEY_REPEAT};

char keyBuf[][15]={"POWER","FUNC/STOP","VOL+","FAST BACK","PAUSE","FAST FORWARD","DOWN","VOL-",
                  "UP","EQ","ST/REPT","0","1","2","3","4","5","6","7","8","9"};
#endif

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