Hi everyone, I'm not new to programming but I am new to programming hardware and new to Arduino, having only had it for a few days.
I have an application that I'm working on where I turn on or off LEDs with an IR remote. In one scenario the LEDs should blink on and off until another command is received from the IR remote.
I have the following code but it doesn't recognize when another IR signal is received.
#include <IRremote.h> // use the library for IR
// constants won't change. Used here to set pin numbers:
const int redLEDPin = 2; // the number of the LED pin
const int whiteLEDPin = 3; // the number of the LED pin
const int irRemotePin = 11; // pin 1 of IR receiver to Arduino digital pin 11
IRrecv irrecv(irRemotePin); // create instance of 'irrecv'
decode_results results;
// Variables will change:
int redLEDState = LOW; // ledState used to set the LED
int whiteLEDState = LOW; // ledState used to set the LED
int lightsLEDState = LOW; // ledState used to set the LED
//============================================================================
// Variables will change:
long previousMillis = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 500; // interval at which to blink (milliseconds)
//============================================================================
void translateIR() // takes action based on IR code received
{
long results_value = results.value;
switch(results_value)
{
case 0xFFC23D:
LightsOnOff();
break;
}
}
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
pinMode(redLEDPin, OUTPUT);
pinMode(whiteLEDPin, OUTPUT);
}
void loop()
{
// test for IR reception
irReceive();
}
int irReceive()
{
if (irrecv.decode(&results)) // have we received an IR signal?
{
translateIR();
for (int z=0; z<2; z++) // ignore 2nd and 3rd signal repeat
{
irrecv.resume(); // receive the next value
}
}
}
void LightsOnOff()
{
if (lightsLEDState == LOW)
{
lightsLEDState = HIGH;
}
else
{
lightsLEDState = LOW;
}
while (lightsLEDState == HIGH)
{
lightsLoop();
}
}
void lightsLoop()
{
Serial.println("running lights");
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval)
{
// save the last time you blinked the LED
previousMillis = currentMillis;
digitalWrite(redLEDPin, HIGH);
digitalWrite(whiteLEDPin, HIGH);
delay(500);
digitalWrite(redLEDPin, LOW);
digitalWrite(whiteLEDPin, LOW);
delay(500);
}
}
I know there are easier ways to simply blink a LED but I have more going on inside of the sections that are lighting the LEDs. I have only pasted in the relevant compilable portions of the code so that any responses could be about the actual problem.
What I need help with is how to call the irReceive() function again from inside the loop without stopping the loop. Everything I have tried has resulted in the loop stopping or never seeing the second button press.
Basically the functionality should be: press remote button once the loop starts and runs until the same button is pressed again.
Thanks in advance,
Jay