Hello! I am new to programming in Arduino and I don't know how to switch between functions, while they are still running when I use my IR remote. My code lets me switch between many functions using my IR remote only after one function is finished, which is what I wanted to do. Now I want my functions to run continuously (as if I am using an infinite loop) and let me switch with another function when I press a button.
For example, I select to press button #1 from my IR remote, my code is running function #1 and it never stops. Now I press button #2 from my IR remote, my code switches from function #1 to function #2 and this function never stops running until I press another button.
This is my code:
#include "IRremote.h"
int receiver = 9; // Signal Pin of IR receiver to Arduino Digital Pin 11
int tDelay = 200;
int latchPin = 11; // (11) ST_CP [RCK] on 74HC595
int clockPin = 10; // (10) SH_CP [SCK] on 74HC595
int dataPin = 12; // (12) DS [S1] on 74HC595
int index = 0;
byte leds = 0;
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results; // create instance of 'decode_results'
void translateIR() // takes action based on IR code received
{
switch(results.value)
{
case 0xFF30CF: Serial.println("1"); One_After_Another(); break;
case 0xFF18E7: Serial.println("2"); One_At_a_Time(); break;
case 0xFF7A85: Serial.println("3"); Ping_Pong(); break;
case 0xFF10EF: Serial.println("4"); Random_LED_ON(); break;
case 0xFF38C7: Serial.println("5"); Marquee(); break;
case 0xFF5AA5: Serial.println("6"); Binary_Count(); break;
default:
Serial.println(" other button ");
}// End Case
delay(500); // Do not get immediate repeat
}
void setup()
{
Serial.begin(9600);
Serial.println("IR Receiver Button Decode");
irrecv.enableIRIn(); // Start the receiver
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}
void shiftWrite(int Pin, boolean State){
digitalWrite(latchPin, HIGH);
bitWrite(leds, Pin, State);
shiftOut(dataPin, clockPin, MSBFIRST, leds);
digitalWrite(latchPin, LOW);
}
void loop()
{
if (irrecv.decode(&results)) // have we received an IR signal?
{
translateIR();
irrecv.resume(); // receive the next value
}
}
void One_After_Another(){
for(int i=0;i<=4;i++){
for(index=0; index<=7; index++){
shiftWrite(index, HIGH);
delay(tDelay);
}
for(index=7; index>=0; index--){
shiftWrite(index, LOW);
delay(tDelay);
}
}
}
void One_At_a_Time(){
for(int i=0;i<=7;i++){
for(index=0; index<=7; index ++){
shiftWrite(index, HIGH);
delay(tDelay);
shiftWrite(index, LOW);
}
}
}
void Ping_Pong(){
for(int i=0; i<=4; i++){
for(index=0; index<=7; index++){
shiftWrite(index, HIGH);
delay(tDelay);
shiftWrite(index, LOW);
}
for(index=7; index>=0; index--){
shiftWrite(index, HIGH);
delay(tDelay);
shiftWrite(index, LOW);
}
}
}
void Random_LED_ON(){
for(int i=0; i<=69; i++){
index=random(8);
shiftWrite(index, HIGH);
delay(tDelay);
shiftWrite(index, LOW);
}
}
void Marquee(){
for(int i=0; i<=4;i++){
for(index=0; index<=3; index++){
shiftWrite(index, HIGH);
shiftWrite(index+4, HIGH);
delay(tDelay);
shiftWrite(index, LOW);
shiftWrite(index+4, LOW);
}
}
}
void Binary_Count(){
int delayCount = 600;
shiftOut(dataPin, clockPin, MSBFIRST, leds);
digitalWrite(latchPin, HIGH);
digitalWrite(latchPin, LOW);
leds++;
delay(delayCount);
}
I would apreciate your help.