IR remote with two leds

I just want to verify that this code should turn one led on at a time. The green led works fine when I’m using it but the red led blinks on then off instead of staying on like the green one does. Also is there a more elegant way of writing this code?

#include <IRremote.h>

int recvPin = 2;
int redled = 4;
int greenled = 8;

IRrecv irReceiver(recvPin);
decode_results results;


void setup(){
  pinMode(redled, OUTPUT);
  pinMode(greenled, OUTPUT);
  
  
  Serial.begin(9600); //Initialize the serial port with a baud rate of 9600
  
  irReceiver.enableIRIn(); // Start the receiver
  irReceiver.blink13(true);
}


void loop() {
  long int recv_value;
  
  if (irReceiver.decode(&results)) {
    recv_value = results.value;
    Serial.println(recv_value);
       
    if (recv_value == 1347255 && digitalRead(greenled) == LOW && digitalRead(redled) == LOW){
      digitalWrite(greenled, HIGH);
      
      irReceiver.resume();
        }
        
      else if (recv_value == 1363635 && digitalRead(greenled) == LOW && digitalRead(redled) == LOW){
        digitalWrite(redled, HIGH);
        
        irReceiver.resume();
        }
        
      else if (recv_value == 1371825 && digitalRead(greenled) == HIGH || digitalRead(redled) == HIGH){
        digitalWrite(greenled, LOW);
        digitalWrite(redled, LOW);
        
        
        irReceiver.resume();
        }

      else{
        irReceiver.resume();
      }
  }
}

I would suggest placing some Serial.println() statements in various places to see if you are getting to where you think you're getting.

For starters, there is no way to turn on the red (assuming HIGH is ON)) if the green is ON, and there is no way to turn on the green if red is on. This is because both have to be OFF in order to turn one on (&& is a logical AND).

In your last if, either one can be on, in order to turn them both off.

You should use parentheses to define which conditions go with the other conditions. Exemple:

    else if ( recv_value == 1371825 && (digitalRead(greenled) == HIGH || digitalRead(redled) == HIGH) ) {

Which means something completely different than:

    else if ( (recv_value == 1371825 && digitalRead(greenled) == HIGH ) || digitalRead(redled) == HIGH ) {
void loop() {
  long int recv_value;
  
  
  
  if (irReceiver.decode(&results)) {
    recv_value = results.value;
    Serial.println(recv_value);
       
    if (recv_value == 1347255 && digitalRead(greenled) == LOW && digitalRead(redled) == LOW){
      digitalWrite(greenled, HIGH);
      Serial.println(digitalRead(greenled));
      Serial.println(digitalRead(redled));
      irReceiver.resume();
        }
        
      else if (recv_value == 1363635 && digitalRead(greenled) == LOW && digitalRead(redled) == LOW){
        digitalWrite(redled, HIGH);
        Serial.println(digitalRead(greenled));
        Serial.println(digitalRead(redled));
        irReceiver.resume();
        }
        
      else if (recv_value == 1371825 && ( digitalRead(greenled) == HIGH || digitalRead(redled) == HIGH)){
        digitalWrite(greenled, LOW);
        digitalWrite(redled, LOW);
        Serial.println(digitalRead(greenled));
        Serial.println(digitalRead(redled));
        
        irReceiver.resume();
        }

      else{
        irReceiver.resume();
      }
  }
}

this is my code now but when I look in the serial monitor the green turns on and I get a 1 for green on then a 0 for red off and if I keep pushing buttons on my remote other then the off button nothing happens to the light (it stays on until I press the off button). With the red led it turns on giving me a 0 green then 1 red but thats only if I press it quickly and dont hold for any amount of time. Otherwise I get 0 green 1 red -1(assuming thats the code for button holding) then 0 green 0 red. I don't quite understand why the red one won't stay on when the green one does.

watatsumi:
this is my code now but when I look in the serial monitor the green turns on and I get a 1 for green on then a 0 for red off and if I keep pushing buttons on my remote other then the off button nothing happens to the light (it stays on until I press the off button). With the red led it turns on giving me a 0 green then 1 red but thats only if I press it quickly and dont hold for any amount of time. Otherwise I get 0 green 1 red -1(assuming thats the code for button holding) then 0 green 0 red. I don’t quite understand why the red one won’t stay on when the green one does.

Well, your decision code is fine. Nothing wrong with it at all. I suspect the IR operation is either not as you think it is, or there is something wrong with the decode, or ???

Here’s your code, modified to test and report on the operation, but without the IR stuff (I don’t have any IR phototransistors to test with).
Your code is still in this one, but commented out.
recv_value starts with 0, then changes at count = 10, 20, 30, and 40, in the order

The output shows that the if conditions are working as written.

int recvPin = 2;
int redled = 4;
int greenled = 8;
byte count = 0;
//IRrecv irReceiver(recvPin);
//decode_results results;
long recv_value = 0;

void setup(){
  pinMode(redled, OUTPUT);
  pinMode(greenled, OUTPUT);
  digitalWrite (redled,LOW);
  digitalWrite (greenled,LOW);
  Serial.begin(115200); //Initialize the serial port with a baud rate of 9600

  Serial.print(digitalRead(greenled));
  Serial.println(digitalRead(redled));
 
//    irReceiver.enableIRIn(); // Start the receiver
//  irReceiver.blink13(true);
}

void loop() {

//  if (irReceiver.decode(&results)) {
//    recv_value = results.value;
    Serial.println(recv_value);

    if (recv_value == 1347255 && digitalRead(greenled) == LOW && digitalRead(redled) == LOW){
      digitalWrite(greenled, HIGH);
      Serial.print("In if #1: ");
      Serial.print(digitalRead(greenled));
      Serial.print(", ");
      Serial.println(digitalRead(redled));
//      irReceiver.resume();
    }

    else if (recv_value == 1363635 && digitalRead(greenled) == LOW && digitalRead(redled) == LOW){
      digitalWrite(redled, HIGH);
      Serial.print("In if #2: ");
      Serial.print(digitalRead(greenled));
      Serial.print(", ");
      Serial.println(digitalRead(redled));
//      irReceiver.resume();
    }

    else if (recv_value == 1371825 && ( digitalRead(greenled) == HIGH || digitalRead(redled) == HIGH)){
      digitalWrite(greenled, LOW);
      digitalWrite(redled, LOW);
      Serial.print("In if #3: ");
      Serial.print(digitalRead(greenled));
      Serial.print(", ");
      Serial.println(digitalRead(redled));
//      irReceiver.resume();
    }

//    else{
//      irReceiver.resume();
//    }
//  }

  count++;
  Serial.print(count);
  Serial.print("  ");
  
  if (count == 10) {
    recv_value = 1347255;
  }
  if (count == 20) {
    recv_value = 1371825;
  }
  if (count == 30) {
    recv_value = 1363635;
  }
  if (count == 40) {
    recv_value = 1371825;
  }
  if (count == 50) while(1) {};
}