StateChangeDetection button Problem

I had prwviously made an Ir Relay circuit for my room appliances and it worked great. Now I want to modify it by attaching a push button(normally open) so that if button is pressed once, one of the light(relay 1) in the room turns on and no need to find remote control and then turn appliances on. I have connected the push button according to :-

The problem is that when i power my circuit, the relay goes mad. It keep triggering itself, due to some kind of interference (i think). Which shuts up as i touch any part of circuit. Kindly help me out this problem. The code i used is:-

#include <IRremote.h>

int RECV_PIN = 11;
int button = 7;
int r3 = 9;
int r2 = 12;
int r1 = 10;
    
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(button,INPUT);
}

void loop() 
{
  if(digitalRead(button)==1)
  {
    if(digitalRead(r3)==LOW)
      {
        digitalWrite(r3,HIGH);
        Serial.println("r1 on");
        
      }
      else{
    digitalWrite(r3,LOW);
    Serial.println("r1 off");
    }
  }
  
  if (irrecv.decode(&results))               
  {
    
    if(results.value==1930688319)//<--Relay1
  {
      if(digitalRead(r3)==LOW)
      {
        digitalWrite(r3,HIGH);
        Serial.println("r1 on");
        
      }
      else{
    digitalWrite(r3,LOW);
    Serial.println("r1 off");
    }
  }
    if(results.value==1440415647)//<--Relay2
    {
      if(digitalRead(r2)==LOW)
      {
        digitalWrite(r2,HIGH);
        Serial.println("r2 on");
      }
      else{
    digitalWrite(r2,LOW);
    Serial.println("r2 off");
    }
    }
    if(results.value==130972539)//<--Relay3
    {
      if(digitalRead(r1)==LOW)
      {
        digitalWrite(r1,HIGH);
        Serial.println("r3 on");
      }
      else{
    digitalWrite(r1,LOW);
    Serial.println("r3 off");
    }
    }
    if(results.value==3173222979)//<--power button
    {
       if(digitalRead(r1)==HIGH)
      {
        digitalWrite(r1,LOW);
        Serial.println("r3 off");
      }
       if(digitalRead(r2)==HIGH)
      {
        digitalWrite(r2,LOW);
        Serial.println("r2 off");
      }
     
     if(digitalRead(r3)==HIGH)
      {
        digitalWrite(r3,LOW);
        Serial.println("r1 off");
      }
    }
     irrecv.resume();
  }                              
        
}

Kindly let me know if there is any problem with tha code too.

(deleted)

you may also need a button debouncer to filter multiple presses to only one press with a button press. There are several ways to debounce a button.

good oscilloscope view at 1:50

Thanks for reply. Now with a change in code, the button works perfectly i.e. Relay 1(r3) can now be triggered with button correctly. But relay 1 now stops triggering by remote control, although relay 2 and 3 are triggering alright with remote. Its a problem in code. Here’s my new 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




// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
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);




  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH),  and you've waited
  // long enough since the last press to ignore any noise:




  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }




  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:




    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;




      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        r3State = !r3State;
      }
    }
  }




  // set the LED:
  digitalWrite(r3, r3State);




  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = reading;





 if (irrecv.decode(&results))                
  {
    
    if(results.value==1930688319)//<--Relay1
  {
      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)//<--Relay3
    {
      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();
  }                            
        
}

When you extend the delay (lastDebounceTime) as long as the button state has changed, the interval will never elapse.

You better start with lastDebounceTime=0, then set it when the button reading changes, and leave it unchanged until the interval has elapsed. When the debounce interval has elapsed, update the button state and reset lastDebounceTime to zero again, to indicate “stable state reached”.

Doing this will make relay controllable from remote control too? Because relay is presently controlled from button only(i want both).

DrDiettrich:
When you extend the delay (lastDebounceTime) as long as the button state has changed, the interval will never elapse.

You better start with lastDebounceTime=0, then set it when the button reading changes, and leave it unchanged until the interval has elapsed. When the debounce interval has elapsed, update the button state and reset lastDebounceTime to zero again, to indicate “stable state reached”.

The delay can interfere with IRremote. Use capacitors to debounce.

Only the delay() function can do harm, but is not present in the OP's code.