S
S
Sergey Karbivnichy2019-10-13 00:17:54
Arduino
Sergey Karbivnichy, 2019-10-13 00:17:54

How to loop the execution of a function until another button on the remote is pressed?

Goodnight. I'm making a police flasher on arduino. I connected everything, sketched a simple code:

The code

#include <IRremoteInt.h>
#include <IRremote.h>
#include <boarddefs.h>
#include <ir_Lego_PF_BitStreamEncoder.h>

IRrecv irrecv(4);
int ledGreen1 = 2;
int ledRed1 = 3;
int w = 50;

decode_results results;

void setup() {

  irrecv.enableIRIn();
  pinMode(ledGreen1,OUTPUT);
  pinMode(ledRed1,OUTPUT);
}

void loop() {
//  digitalWrite(led,HIGH);
//  delay(100);
//  digitalWrite(led,LOW);
//  delay(100);

   //modeBlinkRed();

   if(irrecv.decode(&results))
   {
    switch(results.value)
    {
      case 0xFFB24D:
        modeBlinkRed();
        break;
      case 0xFF6897:
        modeBlinkGreen();
        break;
      case 0xFF2AD5:
        modeBlinkAll();
        break;
    }
    irrecv.resume();
   }

}

void modeBlinkGreen()
{
    for(int i=0;i<6;i++)
  {
    blinkGreen();
  }
  delay(1000);
}

void modeBlinkRed()
{
  for(int i=0;i<6;i++)
  {
    blinkRed();
  }
  delay(1000); 
}

void modeBlinkAll()
{
  for(int i=0;i<5;i++)
  {
    blinkGreen();
  }
  for (int i=0;i<5;i++)
  {
    blinkRed();
  } 
}


void blinkGreen()
{
  digitalWrite(ledGreen1,HIGH);
  delay(w);
  digitalWrite(ledGreen1,LOW);
  delay(w);
  
}

void blinkRed()
{
  digitalWrite(ledRed1,HIGH);
  delay(w);
  digitalWrite(ledRed1,LOW);
  delay(w); 
}

I ran into this problem here:
void loop() {
   if(irrecv.decode(&results))
   {
    switch(results.value)
    {
      case 0xFFB24D:
        modeBlinkRed();
        break;
      case 0xFF6897:
        modeBlinkGreen();
        break;
      case 0xFF2AD5:
        modeBlinkAll();
        break;
    }
    irrecv.resume();
   }
}

If I press the first button, the modeBlinkRed() function is executed, the second - modeBlinkGreen(), the third - modeBlinkAll(). The problem is that the function is executed only once, and I need the function to be executed in a loop until another button on the remote is pressed (that is, another mode is selected).
I guess that the solution here is simple, but something doesn’t come to mind, it’s probably too late already)
PS: Don’t pay attention to the w variable, it’s something to quickly adjust the blinking frequency.
PS2: I know that the police have blue, or blue and red flashing lights, I just didn’t have a blue LED at hand)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pavel K, 2019-10-13
@hottabxp

Goodnight.
Store the state in a global variable.

int mode = 0xFF2AD5;
void loop() {

    if(irrecv.decode(&results)) { //-- Обработку нажатия лучше вообще вынести в прерывание и не использовать delay()
      mode =  results.value;
      irrecv.resume();
    }

    switch(mode) {
      case 0xFFB24D: modeBlinkRed(); break;
      case 0xFF6897: modeBlinkGreen(); break;
      case 0xFF2AD5:  modeBlinkAll();  break;
    }      
}

A
Alexander Skusnov, 2019-10-13
@AlexSku

There is an excellent language - state graph. I advise you to look at one of the implementations on Matlab - Control logic (StateFlow). On-line course (you need to install MatLab, Simulink and StateFlow).
Then you can get the code for Arduino .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question