My IR relay circuit was working perfectly until I decided to modify it with a push button. I want one of the three relays to be toggled by a pushbutton so that "relay3 (r3)" can be toggled by both i.e. pushbutton as well as IRremote. I made a code for that and "relay3" now can be toggled successfully with pushbutton. But the problem is that that relay is no more toggling with IRremote. Although remaining two relays can be toggled with remote. I am sure that the problem is in the code only and I tried many times but cant find the problem with the code.
Here is the code:-
#include <IRremote.h>
const int RECV_PIN = 11;
const int r3 = 9;
const int r2 = 12;
const int r1 = 10;
const int buttonPin = 2; // the number of the pushbutton pin
// Variables will change:
int r3State = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
pinMode(r3,OUTPUT);//Pin For relay3
pinMode(r2,OUTPUT);//Pin For relay2
pinMode(r1,OUTPUT);//Pin For relay1
pinMode(buttonPin, INPUT);
digitalWrite(r3, r3State);
}
void loop()
{
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay)
{
if (reading != buttonState)
{
buttonState = reading;
if (buttonState == HIGH)
{
r3State = !r3State;
}
}
}
digitalWrite(r3, r3State);
lastButtonState = reading;
if (irrecv.decode(&results))
{
if(results.value==1930688319)//<--Relay3
{
if(digitalRead(r3)==LOW)
{
digitalWrite(r3,HIGH);
}
else{
digitalWrite(r3,LOW);
}
}
if(results.value==1440415647)//<--Relay2
{
if(digitalRead(r2)==LOW)
{
digitalWrite(r2,HIGH);
}
else{
digitalWrite(r2,LOW);
}
}
if(results.value==130972539)//<--Relay1
{
if(digitalRead(r1)==LOW)
{
digitalWrite(r1,HIGH);
}
else{
digitalWrite(r1,LOW);
}
}
if(results.value==3173222979)//<--power button
{
if(digitalRead(r1)==HIGH)
{
digitalWrite(r1,LOW);
}
if(digitalRead(r2)==HIGH)
{
digitalWrite(r2,LOW);
}
if(digitalRead(r3)==HIGH)
{
digitalWrite(r3,LOW);
}
}
irrecv.resume();
}
}