Non latching switch controlled by IR Remote

Hi, I am very new to the Arduino game and the programming side of things is proving to be a real challenge. Loving what Im learning though. This might sound silly but I am trying to use an Arduino Uno to control a Tv via an IR remote. My outdoor TV got some moisture in the internals and now the remote receiver no longer works. The push button controls however still work. I would like to use the IR receiver and Arduino to switch relays that will piggyback the push buttons on the tv. I have found and modified a program that receives the signal from the tv remote and drives the relays but unfortunately it causes latching conditions of the switches. I require only instantaneous operation of the relays. As in, I need the relay to change state for the duration of the button press and then release as soon as the button is released. I have been unable to find the changes needed to alter the switch operation. Thanks for any assistance.

[code]
#include <IRremote.h>
#include <ir_Lego_PF_BitStreamEncoder.h>
const int switch_1 = 7;
const int switch_2 = 6;
const int switch_3 = 5;
const int switch_4 = 4;
int RECV_PIN = 11;
int toggleState_1 = 0;
int toggleState_2 = 0;
int toggleState_3 = 0;
int toggleState_4 = 0;

IRrecv irrecv(RECV_PIN);
decode_results results;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  irrecv.enableIRIn();
  pinMode(switch_1, OUTPUT);
  pinMode(switch_2, OUTPUT);
  pinMode(switch_3, OUTPUT);
  pinMode(switch_4, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (irrecv.decode(&results)) {
    switch (results.value) {
      case 0x4EBB708F:
        if (toggleState_1 == 0) {
          digitalWrite(switch_1, HIGH);
          toggleState_1 = 1;
        }
        else {
          digitalWrite(switch_1, LOW);
          toggleState_1 = 0;
        }
        delay(100);
        break;
      case 0x4EBB926D:
        if (toggleState_2 == 0) {
          digitalWrite(switch_2, HIGH);
          toggleState_2 = 1;
        }
        else {
          digitalWrite(switch_2, LOW);
          toggleState_2 = 0;
        }
        delay(100);
        break;
      case 0x4EBBC23D:
        if (toggleState_3 == 0) {
          digitalWrite(switch_3, HIGH);
          toggleState_3 = 1;
        }
        else {
          digitalWrite(switch_3, LOW);
          toggleState_3 = 0;
        }
        delay(100);
        break;
      case 0x4EBB8A75:
        if (toggleState_4 == 0) {
          digitalWrite(switch_4, HIGH);
          toggleState_4 = 1;
        }
        else {
          digitalWrite(switch_4, LOW);
          toggleState_4 = 0;
        }
        delay(100);
        break;
      default : break;
    }
    irrecv.resume();
  }
}

[/code]

Case-switch statements only runs on true condition. So if we logically step through this, we can definitely find the problem.

Let's say you press button 1, that correspond to case 1.
Program runs case 1, sets Switch 1 to HIGH.
Program ran into "break" statement, "breaks" itself out of the switch statement, effectively skipping everything else.
You released button 1.
Program runs case default, immediately breaks.
You pressed button 2.
Program runs case 2, sets Switch 2 to HIGH.
Program ran breaks out of switch statement.
and so on...

You should be able to spot the problem from here. You need a way to turn off the pins, the way you implemented will not work now. Easiest solution would be to have it run a "clear every output pin" routine at the start of the loop. The only problem that might arise from this solution is that you might have relays clicking multiple times when holding a button, because it's being set LOW, then HIGH, then LOW again at start of loop.

don’t your outputs constantly toggle as long as you hold the button down? don’t you want to recognize that a button was released before toggle the output?

dealing with an actively driven output only as long as a button is held down requires recognizing when the button was released – a non-active event. in the code below, handled in the default, no input case

consider the code below tested using leds and buttons attempting to simulate you IR input

// demonstrate active on and latching control

enum { Off = HIGH, On = LOW };

byte outPins [] = { 12, 13 };
byte butPins [] = { A1, A2 };
byte butSt   [] = { Off, Off };

#define Npins sizeof(outPins)

// -----------------------------------------------------------------------------
void setup() {
    Serial.begin(115200);

    for (unsigned n = 0; n < Npins; n++)  {
        pinMode      (butPins [n], INPUT_PULLUP);
        digitalWrite (outPins [n], Off);
        pinMode      (outPins [n], OUTPUT);
    }
}

// -----------------------------------------------------------------------------
void check (int n) {
    static byte state = Off;

    switch (n) {
    case 0:     // but-1 -- toggle output
        if (Off == state)
            digitalWrite (outPins [n], ! digitalRead (outPins [n]));
        break;

    case 1:     // but-2 -- active output
        digitalWrite (outPins [n], On);
        break;

    default:
        digitalWrite (outPins [1], Off);
        state = Off;
        break;
    }
}

// -----------------------------------------------------------------------------
void loop() {
    unsigned n;
    for (n = 0; n < Npins; n++)  {
        if (On == digitalRead (butPins [n]))
            break;
    }

    check (n);
    delay (10);     // debounce
}