infrared remote control controlling 433mhz wall sockets

Hi, Im using a infrared remote control to send signals to a arduino which then sends 433mhz signals out to control remote control mains sockets around the house. Iv got it all working but I wish to set a timer on certain sockets so when they turn on they will turn off after say 10 minutes but only on select sockets. Could anyone show me how this could be done when you push button 1 on the remote. So it would send the 433mhz turn on signal then wait 10 minutes then send the turn off signal. Thanks. Here is the code.

#include <RCSwitch.h>
#include <IRremote.h>

int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
RCSwitch mySwitch = RCSwitch();

byte state0 = 0;
byte state1 = 0;
byte state2 = 0;
byte state3 = 0;
byte state4 = 0;
byte state5 = 0;
byte state6 = 0;
byte state7 = 0;
byte state8 = 0;
byte state9 = 0;
byte state10 = 0;
byte state11 = 0;
byte state12 = 0;
byte state13 = 0;

void setup(){
  
  Serial.begin(9600);  //starts serial communication
  irrecv.enableIRIn(); // Start the receiver
  
  // Transmitter is connected to Arduino Pin #10  
  mySwitch.enableTransmit(10);
  mySwitch.setPulseLength(189);
}

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

    //switch case to use the selected remote control button
    switch (results.value){
     
     case 0xFF6897: //when you press the 1 button
     if (state1 == 0) {
        mySwitch.send(349491, 24); //(349491, 24);
        }
      else {
        mySwitch.send(349500, 24); //(349500, 24);
      }
      state1 = !state1; //Swap the state: if it is 0, make it 1, if it is 1, make it 0 
      break;
      
      case 0xFF9867: //when you press the 2 button
      if (state2 == 0) {
        mySwitch.send(349635, 24); //(349635, 24);
      }            
      else {
        mySwitch.send(349644, 24); //(349644, 24);
      }        
      state2 = !state2; //Swap the state: if it is 0, make it 1, if it is 1, make it 0
      break;
      
      case 0xFFB04F: //when you press the 3 button
      if (state3 == 0) {
        mySwitch.send(349955, 24); //(349955, 24);
      }
      else {
        mySwitch.send(349964, 24); //(349964, 24);
      }
      state3 = !state3; //Swap the state: if it is 0, make it 1, if it is 1, make it 0 
      break;
      
      case 0xFF30CF: //when you press the 4 button
      if (state4 == 0) {
        mySwitch.send(351491, 24); //(351491, 24);
      }
      else {
        mySwitch.send(351500, 24); //(351500, 24);
      }
      state4 = !state4; //Swap the state: if it is 0, make it 1, if it is 1, make it 0 
      break;
      
      case 0xFF18E7: //when you press the 5 button
      if (state5 == 0) {
        mySwitch.send(357635, 24); //(357635, 24);
      }
      else {
        mySwitch.send(357644, 24); //(357644, 24);
      }
      state5 = !state5; //Swap the state: if it is 0, make it 1, if it is 1, make it 0
      break;
      
      case 0xFF7A85: //when you press the 6 button
      if (state6 == 0) {
       mySwitch.send(4543795, 24); //(4543795, 24);
      }
      else {
       mySwitch.send(4543804, 24); //(4543804, 24);
      }
      state6 = !state6; //Swap the state: if it is 0, make it 1, if it is 1, make it 0
      break;
      
      case 0xFF10EF: //when you press the 7 button
      if (state7 == 0) {
        mySwitch.send(4543939, 24); //(4543939, 24);
      }
      else {
        mySwitch.send(4543948, 24); //(4543948, 24);
      }
      state7 = !state7; //Swap the state: if it is 0, make it 1, if it is 1, make it 0;
      break;
      
      case 0xFF38C7: //when you press the 8 button
      if (state8 == 0) {
        mySwitch.send(4544259, 24); //(4544259, 24);
      }
      else {
        mySwitch.send(4544268, 24); //(4544268, 24);
      }
      state8 = !state8; //Swap the state: if it is 0, make it 1, if it is 1, make it 0W);
      break;
      
      case 0xFF5AA5: //when you press the 9 button
      if (state9 == 0) {
        mySwitch.send(4545795, 24); //(4545795, 24);
      }
      else {
        mySwitch.send(4545804, 24); //(4545804, 24);
      }
      state9 = !state9; //Swap the state: if it is 0, make it 1, if it is 1, make it 0
      break;
      
      case 0xFF4AB5: //when you press the 0 button
      if (state0 == 0) {
        mySwitch.send(4551939, 24); //(4551939, 24);
      }
      else {
        mySwitch.send(4551948, 24); //(4551948, 24)
      }
      state0 = !state0; //Swap the state: if it is 0, make it 1, if it is 1, make it 0
      break;

      case 0xFF629D: //When you press the up button
      if (state10 == 0) {
        mySwitch.send(1398067, 24); //(4543795, 24);
      }
      else {
        mySwitch.send(1398076, 24); //(4543804, 24);
      }
      state10 = !state10; //Swap the state: if it is 0, make it 1, if it is 1, make it 0;
      break;

      case 0xFFA857: //When you push the down button
      if (state11 == 0) {
        mySwitch.send(1398211, 24); //(4543939, 24);
      }
      else {
        mySwitch.send(1398220, 24); //(4543948, 24);
      }
      state11 = !state11; //Swap the state: if it is 0, make it 1, if it is 1, make it 0
      break;

      case 0xFF22DD: //When you push the left button
      if (state12 == 0) {
        mySwitch.send(1398531, 24); //(4544259, 24);
      }
      else {
        mySwitch.send(1398540, 24); //(4544268, 24);
      }
      state12 = !state12; //Swap the state: if it is 0, make it 1, if it is 1, make it 0;
      break;

      case 0xFFC23D: //When you push the right button
      if (state13 == 0) {
        mySwitch.send(1400067, 24); //(4545795, 24);
      }
      else {
        mySwitch.send(1400076, 24); //(4545804, 24);
      }
      state13 = !state13; //Swap the state: if it is 0, make it 1, if it is 1, make it 0
      }
      if (irrecv.decode(&results)) {
      Serial.println(results.value, HEX);
      irrecv.resume(); // Receive the next value
  }
}
}
byte state0 = 0;
byte state1 = 0;
byte state2 = 0;
byte state3 = 0;
byte state4 = 0;
byte state5 = 0;
byte state6 = 0;
byte state7 = 0;
byte state8 = 0;
byte state9 = 0;
byte state10 = 0;
byte state11 = 0;
byte state12 = 0;
byte state13 = 0;

Have you heard of arrays?
Have you heard of arrays?
Have you heard of arrays?
Have you heard of arrays?
Have you heard of arrays?

You determine which pin to toggle, based on the remote switch that was pressed. The key there is toggle. If you want to turn a pin on, when a remote switch is pressed, and then turn the pin off a fixed time later, you first need to stop toggling the pin.

You should have an array of pin states. You should have an array of times that pins were turned on. You should have an array of times that the pins should be on (or -1 which would mean toggle the pin).

Then, independent of the remote handling, you would iterate through the arrays. If the pin is on, and has a interval that has elapsed, turn the pin off and clear the on time.

I would do something like this (totally NOT tested) - this is just to give you an idea

#include <RCSwitch.h>
#include <IRremote.h>

const int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;

RCSwitch mySwitch = RCSwitch();

struct RCStructure
{
  boolean state;
  unsigned long codeID;
  unsigned long onCode;
  unsigned long offCode;
  unsigned int codeLength;
  char letter;
  boolean autoOff;
  unsigned long autoOffTime;
};

RCStructure remoteKey[] = {
  {false, 0xFF4AB5ul, 4551939ul, 4551948ul, 24, '0', false, 0ul}, //when you press the 0 button
  {false, 0xFF6897ul, 349491ul,  349500ul,  24, '1', false, 0ul}, //when you press the 1 button
  {false, 0xFF9867ul, 349635ul,  349644ul,  24, '2', false, 0ul}, //when you press the 2 button
  {false, 0xFFB04Ful, 349955ul,  349964ul,  24, '3', false, 0ul}, //when you press the 3 button
  {false, 0xFF30CFul, 351491ul,  351500ul,  24, '4', false, 0ul}, //when you press the 4 button
  {false, 0xFF18E7ul, 357635ul,  357644ul,  24, '5', false, 0ul}, //when you press the 5 button
  {false, 0xFF7A85ul, 4543795ul, 4543804ul, 24, '6', false, 0ul}, //when you press the 6 button
  {false, 0xFF10EFul, 4543939ul, 4543948ul, 24, '7', false, 0ul}, //when you press the 7 button
  {false, 0xFF38C7ul, 4544259ul, 4544268ul, 24, '8', false, 0ul}, //when you press the 8 button
  {false, 0xFF5AA5ul, 4545795ul, 4545804ul, 24, '9', false, 0ul}, //when you press the 9 button
  {false, 0xFF629Dul, 1398067ul, 1398076ul, 24, 'U', false, 0ul}, //When you press the up button
  {false, 0xFFA857ul, 1398211ul, 1398220ul, 24, 'D', false, 0ul}, //When you push the down button
  {false, 0xFF22DDul, 1398531ul, 1398540ul, 24, 'L', false, 0ul}  //When you push the left button
};

const byte remoteKeySize = sizeof(remoteKey) / sizeof(remoteKey[0]);
const unsigned long AUTO_OFF_DELAY = 600000ul; // 10 minutes x 60 seconds per minute x 1000 millisecond per second

// -----------------------------------------------

void setup() {

  Serial.begin(9600);  //starts serial communication
  irrecv.enableIRIn(); // Start the receiver

  // Transmitter is connected to Arduino Pin #10
  mySwitch.enableTransmit(10);
  mySwitch.setPulseLength(189);
}

void loop() {

  // -----------------------------
  // check the remote control
  // -----------------------------
  if (irrecv.decode(&results)) {
    for (byte i = 0; i < remoteKeySize; i++) { // check which key is pressed
      if (results.value == remoteKey[i].codeID) {
        // we found the key, do what's needed
        Serial.println(remoteKey[i].letter); // debug only, print the letter on the key
        if (remoteKey[i].state) {
          remoteKey[i].state = false;
          mySwitch.send(remoteKey[i].offCode, remoteKey[i].codeLength);
          remoteKey[i].autoOff = false; // stop it if needed
        }
        else {
          remoteKey[i].state = true;
          mySwitch.send(remoteKey[i].onCode, remoteKey[i].codeLength);
          if ((remoteKey[i].letter >= '0') && (remoteKey[i].letter <= '9')) { // sets auto off only for 0 to 9
            remoteKey[i].autoOff = true;
            remoteKey[i].autoOffTime = millis();
          }
        }
        break;
      }
    } // for each key
    irrecv.resume(); // Receive the next value
  }

  // -----------------------------
  // check auto off features
  // -----------------------------
  for (byte i = 0; i < remoteKeySize; i++) {
    if (remoteKey[i].state && remoteKey[i].autoOff && (millis() - remoteKey[i].autoOffTime >= AUTO_OFF_DELAY)) {
      remoteKey[i].state = false;
      remoteKey[i].autoOff = false;
      mySwitch.send(remoteKey[i].offCode, remoteKey[i].codeLength);
    }
  }


  // -----------------------------
  // do other stuff
  // -----------------------------


}

J-M-L:
I would do something like this (totally NOT tested) - this is just to give you an idea

#include <RCSwitch.h>

#include <IRremote.h>

const int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;

RCSwitch mySwitch = RCSwitch();

struct RCStructure
{
 boolean state;
 unsigned long codeID;
 unsigned long onCode;
 unsigned long offCode;
 unsigned int codeLength;
 char letter;
 boolean autoOff;
 unsigned long autoOffTime;
};

RCStructure remoteKey = {
 {false, 0xFF4AB5ul, 4551939ul, 4551948ul, 24, ‘0’, false, 0ul}, //when you press the 0 button
 {false, 0xFF6897ul, 349491ul,  349500ul,  24, ‘1’, false, 0ul}, //when you press the 1 button
 {false, 0xFF9867ul, 349635ul,  349644ul,  24, ‘2’, false, 0ul}, //when you press the 2 button
 {false, 0xFFB04Ful, 349955ul,  349964ul,  24, ‘3’, false, 0ul}, //when you press the 3 button
 {false, 0xFF30CFul, 351491ul,  351500ul,  24, ‘4’, false, 0ul}, //when you press the 4 button
 {false, 0xFF18E7ul, 357635ul,  357644ul,  24, ‘5’, false, 0ul}, //when you press the 5 button
 {false, 0xFF7A85ul, 4543795ul, 4543804ul, 24, ‘6’, false, 0ul}, //when you press the 6 button
 {false, 0xFF10EFul, 4543939ul, 4543948ul, 24, ‘7’, false, 0ul}, //when you press the 7 button
 {false, 0xFF38C7ul, 4544259ul, 4544268ul, 24, ‘8’, false, 0ul}, //when you press the 8 button
 {false, 0xFF5AA5ul, 4545795ul, 4545804ul, 24, ‘9’, false, 0ul}, //when you press the 9 button
 {false, 0xFF629Dul, 1398067ul, 1398076ul, 24, ‘U’, false, 0ul}, //When you press the up button
 {false, 0xFFA857ul, 1398211ul, 1398220ul, 24, ‘D’, false, 0ul}, //When you push the down button
 {false, 0xFF22DDul, 1398531ul, 1398540ul, 24, ‘L’, false, 0ul}  //When you push the left button
};

const byte remoteKeySize = sizeof(remoteKey) / sizeof(remoteKey[0]);
const unsigned long AUTO_OFF_DELAY = 600000ul; // 10 minutes x 60 seconds per minute x 1000 millisecond per second

// -----------------------------------------------

void setup() {

Serial.begin(9600);  //starts serial communication
 irrecv.enableIRIn(); // Start the receiver

// Transmitter is connected to Arduino Pin #10
 mySwitch.enableTransmit(10);
 mySwitch.setPulseLength(189);
}

void loop() {

// -----------------------------
 // check the remote control
 // -----------------------------
 if (irrecv.decode(&results)) {
   for (byte i = 0; i < remoteKeySize; i++) { // check which key is pressed
     if (results.value == remoteKey[i].codeID) {
       // we found the key, do what’s needed
       Serial.println(remoteKey[i].letter); // debug only, print the letter on the key
       if (remoteKey[i].state) {
         remoteKey[i].state = false;
         mySwitch.send(remoteKey[i].offCode, remoteKey[i].codeLength);
         remoteKey[i].autoOff = false; // stop it if needed
       }
       else {
         remoteKey[i].state = true;
         mySwitch.send(remoteKey[i].onCode, remoteKey[i].codeLength);
         if ((remoteKey[i].letter >= ‘0’) && (remoteKey[i].letter <= ‘9’)) { // sets auto off only for 0 to 9
           remoteKey[i].autoOff = true;
           remoteKey[i].autoOffTime = millis();
         }
       }
       break;
     }
   } // for each key
   irrecv.resume(); // Receive the next value
 }

// -----------------------------
 // check auto off features
 // -----------------------------
 for (byte i = 0; i < remoteKeySize; i++) {
   if (remoteKey[i].state && remoteKey[i].autoOff && (millis() - remoteKey[i].autoOffTime >= AUTO_OFF_DELAY)) {
     remoteKey[i].state = false;
     remoteKey[i].autoOff = false;
     mySwitch.send(remoteKey[i].offCode, remoteKey[i].codeLength);
   }
 }

// -----------------------------
 // do other stuff
 // -----------------------------

}

Thank you J-M-L. That code is working great. I am new to arduino so I really appreciate the help. I think I understatnd it now a lot better.

Great news!