Doubt in Programming (not repeating code)

my intention is when I press button 1 on the remote control, (on / off the relay), same thing for button 2, now when button 3 is pressed, it sends the infrared, for example to increase the TV volume

When I press 1 and 2, everything works normally (on / off the relay), but when I press button 3, it only sends the infrared code once, and when I press button 3 again nothing happens, and even when I press the button again button 1 and 2, they do not work (relay on / off)

#include <IRremote.h>

int RECV_PIN = 11;
const int Rele1 = 10;
const int Rele2 = 7;
int flag0, flag1, flag3;

IRsend irsend;
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{
Serial.begin(9600);
pinMode(Rele1,OUTPUT);
pinMode(Rele2,OUTPUT);
irrecv.enableIRIn(); // Start the receiver
}

void loop() {
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);

if(results.value == 0xFF6897){ //botao 1
if(flag0 == 0) {
flag0 = 1;
digitalWrite(Rele1, HIGH);
}else{
flag0 = 0;
digitalWrite(Rele1, LOW);
}
}
if(results.value == 0xFF9867){ //botao 2
if(flag1 == 0) {
flag1 = 1;
digitalWrite(Rele2, HIGH);
}else{
flag1 = 0;
digitalWrite(Rele2, LOW);
}
}
if(results.value == 0xFFB04F){ //botao 3
irsend.sendNEC (0x20DF40BF,32);
}
irrecv.resume(); // Receive the next value
}
delay(100);

}

Try here

Please remember to use code tags when posting code.

my intention is when I press button 1 on the remote control, (on / off the relay), same thing for button 2, now when button 3 is pressed, it sends the infrared, for example to increase the TV volume

When I press 1 and 2, everything works normally (on / off the relay), but when I press button 3, it only sends the infrared code once, and when I press button 3 again nothing happens, and even when I press the button again button 1 and 2, they do not work (relay on / off)

JARBASR02.ino (1.13 KB)

/*
 * IRremote: IRrecvDemo - demonstrates receiving IR codes with IRrecv
 * An IR detector/demodulator must be connected to the input RECV_PIN.
 * Version 0.1 July, 2009
 * Copyright 2009 Ken Shirriff
 * http://arcfn.com
 */

#include <IRremote.h>

int RECV_PIN = 11;
const int Rele1 = 10;
const int Rele2 = 7;
int flag1, flag2;

IRsend irsend;
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{
  Serial.begin(9600);
  pinMode(Rele1,OUTPUT);
  pinMode(Rele2,OUTPUT);
  irrecv.enableIRIn(); // Start the receiver
}

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);

  if(results.value == 0xFF6897){  //botao 1
    if(flag1 == 0) {
      flag1 = 1;
      digitalWrite(Rele1, HIGH);
    }else{
      flag1 = 0;
      digitalWrite(Rele1, LOW);
    }
   }
     if(results.value == 0xFF6897){  //botao 2
    if(flag2 == 0) {
      flag2 = 1;
      digitalWrite(Rele2, HIGH);
    }else{
      flag2 = 0;
      digitalWrite(Rele2, LOW);
    }
   }
      if(results.value == 0xFFB04F){  //botao 3
        irsend.sendNEC (0x20DF40BF,32);
   }
      irrecv.resume(); // Receive the next value
  }
delay(100);

}

Ken Shirriff said in an old post (#5), and it probably still holds true:

You can't use sending and receiving at the same time. When you do a send, the receiving is disabled. To re-enable it after sending you need to call:
irrecv.enableIRIn(); // Start the receiver

This solved my problem, thank you very much. :smiley: