Answer the question
In order to leave comments, you need to log in
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:
#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);
}
void loop() {
if(irrecv.decode(&results))
{
switch(results.value)
{
case 0xFFB24D:
modeBlinkRed();
break;
case 0xFF6897:
modeBlinkGreen();
break;
case 0xFF2AD5:
modeBlinkAll();
break;
}
irrecv.resume();
}
}
Answer the question
In order to leave comments, you need to log in
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;
}
}
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 questionAsk a Question
731 491 924 answers to any question