Trying to merge 2 programs into one (IR receiver and IR transmitter)

I'm playing around with recording and transmitting Infra Red signals from my tv remote. I'm using Arduino Mega. I found one program that records a received IR signal, and outputs the received array in the serial monitor. I then copy this array and paste it into a different program that transmits the IR signal. Both work, and are shown below.

IR receiver code:

// IR Receiver code. (WORKING ON MEGA)
//When you press a button on your remote control the code records it and prints the signal into the serial monitor, you copy this and paste it to be used in the transmitter code

#include <IRLibRecvPCI.h>

IRrecvPCI myReceiver(2);  //IR Receiver on pin 2   

void setup() {
  Serial.begin(9600);
  myReceiver.enableIRIn();
  Serial.println("Ready to receive IR signals");
  Serial.println("Point the remote controller to the IR receiver and press!");
}

void loop() {
  if (myReceiver.getResults()) {
    Serial.println("\n\n-------------------------");
    Serial.println("Received IR signal:");

    Serial.print(F("\n#define RAW_DATA_LEN "));
    Serial.println(recvGlobal.recvLength, DEC);

    Serial.print(F("uint16_t rawData[RAW_DATA_LEN]={\n"));
          for (bufIndex_t i = 1; i < recvGlobal.recvLength; i++) {
            Serial.print(recvGlobal.recvBuffer[i], DEC);
            Serial.print(F(", "));
             if ((i % 8) == 0) {
             Serial.print(F("\n"));
              }
          }
    Serial.println(F("1000};"));
    Serial.println("-------------------------");

    myReceiver.enableIRIn();
  }
}

//------FOR EXAMPLE, I PUSHED THE "POWER ON" BUTTON ON MY TV REMOTE, IT OUTPUTS THIS (below) IN THE SERIAL MONITOR, COPY AND PASTE THIS INTO THE TRANSMITTER CODE-----------------

/*
-------------------------
Received IR signal:

#define RAW_DATA_LEN 68
uint16_t rawData[RAW_DATA_LEN]={
9066, 4502, 586, 546, 590, 546, 590, 546, 
610, 526, 586, 546, 586, 550, 614, 522, 
610, 522, 614, 1630, 586, 1654, 614, 1630, 
586, 1654, 614, 1630, 610, 1630, 614, 1626, 
590, 1654, 614, 1626, 614, 522, 586, 1654, 
614, 522, 586, 550, 586, 546, 614, 1630, 
614, 522, 610, 522, 614, 1630, 614, 518, 
614, 1630, 614, 1626, 614, 1630, 614, 522, 
610, 1630, 614, 1000};
-------------------------
*/

IR transmitter code:


//IR transmitter code. (WORKING ON MEGA)
//You paste the signal you copied from receiver code, and then this code repeats the signal every 5 seconds


#define RAW_DATA_LEN 68
uint16_t rawDataTvPower[RAW_DATA_LEN]={            //I added "TvPower" to the array name
9066, 4502, 586, 546, 590, 546, 590, 546, 
610, 526, 586, 546, 586, 550, 614, 522, 
610, 522, 614, 1630, 586, 1654, 614, 1630, 
586, 1654, 614, 1630, 610, 1630, 614, 1626, 
590, 1654, 614, 1626, 614, 522, 586, 1654, 
614, 522, 586, 550, 586, 546, 614, 1630, 
614, 522, 610, 522, 614, 1630, 614, 518, 
614, 1630, 614, 1626, 614, 1630, 614, 522, 
610, 1630, 614, 1000};



#include <IRLibSendBase.h>
#include <IRLib_HashRaw.h>

IRsendRaw mySender;

void setup(){
  pinMode(9,OUTPUT);           // IR LED on D9
  digitalWrite(9,LOW);
  Serial.begin(9600);  
  }

  void loop(){  
    Serial.println("Sending ON");
    mySender.send(rawDataTvPower,RAW_DATA_LEN,38);
    Serial.println("Sent Turn ON TV");
    delay(5000);   
  }

  

What I want to do is join the 2 codes together. So I can record and store an IR signal (multiple in the future) and then transmit it later on. I attempted some code (below), it stores the received signal on a temporary array, and then attempts to transmit it but it doesn't turn my tv on or off. I don't know where it goes wrong, or if it is possible, or if i should try a different approach. My thoughts are commented in the code below.

IR Transceiver code:

//IR Transceiver code, (NOT WORKING - Not sure if signal is being transmitted)
// Trying to add both reciever and transmitter code into one piece of code.
// I added 2 push button to allow for receiver mode, and transmitter mode.
// Note, the signal/array length (RAW_DATA_LEN) is differnt for each remote manufacturer you are using (from 20 to 68 in my case)
// My plan is to record the IR signal, and place it in a Temp Array of length 100 (to act as a "catch all")
// I will then be able to determine the length of the signal, and store that value at the end of the array, Temp Array [99]
// When i want to transmit the signal, I will create a New Array with length TempArray[99]
// Then i will transmit this signal


#include <IRLibRecvPCI.h>
IRrecvPCI myReceiver(2); //IR Receiver on pin 2


#include <IRLibSendBase.h>
#include <IRLib_HashRaw.h>

IRsendRaw mySender;


#define tempArrayLength  100  //100 in length
uint16_t tempArray[tempArrayLength]={0};  // Starts will all zero's

int NewArrayLength;  // New Array varies in length

int ButtonRec = 8;    // When pushed it will be in Receiver mode 
int ButtonTrans = A3;  // When pushed it will transmit the signal in the New Array (made using data from the tempArray)

int ListenFlag=1;   // not sure if i need this, it just stops the serial monitor from repeating ("Listening....")


void setup() {
 pinMode(9,OUTPUT);
  pinMode(ButtonRec,INPUT); // pulled UP, LOW when pushed
  pinMode(ButtonTrans,INPUT);// pulled UP, LOW when pushed
 digitalWrite(9,LOW); 
 Serial.begin(9600);

Serial.println("\n\n Record a signal first..., nothing in temp array to transmit  \n Hold down rec button while sending the signal");
}

void loop() {

 
if(digitalRead(ButtonRec)==LOW){ 
if(ListenFlag==1){Serial.println("Listetning... "); ListenFlag=0;myReceiver.enableIRIn();} //enables IR Receiver

 if (myReceiver.getResults()) {  //a signal has been stored in the buffer array

   for (int i = 1; i < tempArrayLength; i++) {
    
     tempArray[i]=recvGlobal.recvBuffer[i], DEC;  //start copying the buffer array into the temp array

           if(tempArray[i] >0){  //If i'th value is still above zero then carry on the for loop      
           }
     
          else {          //If i'th value is zero, then we have reached the end of the recorded signal      
          tempArray[i]=1000;  // note all signals must end with a value of 1000, this is for the "sender" program to know??
          NewArrayLength=i;  // we now know the length of the signal     
          tempArray[99]=NewArrayLength;//store "length of signal" value in the 99th/last cell of the tempArray 
          i=tempArrayLength; // stop the for loop
           }      
   }

   myReceiver.disableIRIn(); // disable IR receiver ,may get confused when transmitting the signal?    
   ListenFlag=1; // reset flag
   Serial.println("\n\nSignal Recorded into Temp Array, RELEASE BUTTON NOW");
   delay(3000); // give yourself 3 seconds to release the rec button
   Serial.println(" \n\n Press Transmit buton to transmit the signal");   
 }
}


if(analogRead(ButtonTrans)==LOW){//Transmit signal stored in temp array
Serial.println("\nTransmitting the received signal currently stored in temp array...");

uint16_t rawDataNewArray[tempArray[99]];    //Create a New Array of length tempArray[99]
  for (int i = 1; i < tempArray[99]+1; i++){   //copy values from temp array into New Array
rawDataNewArray[i] = {tempArray[i]};         
  }

mySender.send(rawDataNewArray,tempArray[99],38);  // Transmit the signal in New Array

delay(500);
Serial.println("\nTemp Array Signal Sent");




//-------------- Debugging stuff after transmitting attempt - Print all 3 arrays (intial buffer, temp, and New) and their lengths------------------

Serial.println("\n\n-------------------------");
Serial.println("        DEBUGGING        ");
Serial.println("-------------------------");
Serial.print("\n Original array from the programs buffer, Length  =  ");    //buffer array
Serial.println(recvGlobal.recvLength, DEC);

   for (bufIndex_t i = 1; i < recvGlobal.recvLength; i++) {
     Serial.print(recvGlobal.recvBuffer[i], DEC);     
     Serial.print(F(", "));
         if ((i % 8) == 0) {
             Serial.print(F("\n"));
          }
   }
Serial.println(F("1000;"));



Serial.println("\n\n-------------------------");
Serial.print("\n Temp Array copied from the programs buffer, Length  =  ");    //tempArray
Serial.println(tempArrayLength);
Serial.print("\n double check length  =  ");
Serial.println(sizeof(tempArray)/sizeof(tempArray[0]));  // original size is double what expected, apparently i need to divided by  "sizeof(tempArray[0])" to get correct answer
Serial.println("");

   for (bufIndex_t i = 1; i < tempArrayLength + 1; i++) {
     Serial.print(tempArray[i]);     
     Serial.print(F(", "));
         if ((i % 8) == 0) {
             Serial.print(F("\n"));
          }
   }


Serial.println("\n\n-------------------------");
Serial.print("\n New Array copied from the Temp Array, Length  =  ");    //NewArray  or  rawDateNewArray
Serial.println(NewArrayLength);
Serial.print("\n double check length  =  ");
Serial.println(sizeof(rawDataNewArray)/sizeof(rawDataNewArray[0]));  // again, magic of some kind
Serial.println("");

  for (int i = 1; i < NewArrayLength+1; i++){   // Prints out the signal in New Array
   
     Serial.print(rawDataNewArray[i]);     
     Serial.print(F(", "));
      if ((i % 8) == 0) {
      Serial.print(F("\n"));
      }
  }






}//END Transmit

}//END VOID MAIN LOOP






// --------------------------------- DEBUGGING RESULTS FROM SERIAL MONITOR ------------------------------------
/*



Record a signal first..., nothing in temp array to transmit  
Hold down rec button while sending the signal
Listetning... 


Signal Recorded into Temp Array, RELEASE BUTTON NOW


Press Transmit buton to transmit the signal

Transmitting the received signal currently stored in temp array...

Temp Array Signal Sent


-------------------------
       DEBUGGING        
-------------------------

Original array from the programs buffer, Length  =  68
9102, 4474, 614, 522, 614, 522, 614, 522, 
614, 522, 614, 522, 614, 522, 610, 522, 
614, 522, 614, 1630, 614, 1630, 638, 1602, 
614, 1630, 614, 1630, 590, 1654, 614, 1630, 
614, 1630, 614, 1630, 614, 518, 614, 1630, 
590, 546, 614, 522, 614, 522, 614, 1630, 
610, 522, 618, 518, 614, 1630, 614, 522, 
614, 1630, 614, 1626, 614, 1630, 614, 522, 
614, 1630, 614, 1000;


-------------------------

Temp Array copied from the programs buffer, Length  =  100

double check length  =  100

9102, 4474, 614, 522, 614, 522, 614, 522, 
614, 522, 614, 522, 614, 522, 610, 522, 
614, 522, 614, 1630, 614, 1630, 638, 1602, 
614, 1630, 614, 1630, 590, 1654, 614, 1630, 
614, 1630, 614, 1630, 614, 518, 614, 1630, 
590, 546, 614, 522, 614, 522, 614, 1630, 
610, 522, 618, 518, 614, 1630, 614, 522, 
614, 1630, 614, 1626, 614, 1630, 614, 522, 
614, 1630, 614, 1000, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 68, 534, 

-------------------------

New Array copied from the Temp Array, Length  =  68

double check length  =  68

9102, 4474, 614, 522, 614, 522, 614, 522, 
614, 522, 614, 522, 614, 522, 610, 522, 
614, 522, 614, 1630, 614, 1630, 638, 1602, 
614, 1630, 614, 1630, 590, 1654, 614, 1630, 
614, 1630, 614, 1630, 614, 518, 614, 1630, 
590, 546, 614, 522, 614, 522, 614, 1630, 
610, 522, 618, 518, 614, 1630, 614, 522, 
614, 1630, 614, 1626, 614, 1630, 614, 522, 
614, 1630, 614, 1000, 


*/








This is a frequent question. Search for "Merging codes". Try the "Search Forum" function up to the right in this window.

1 Like

analogRead() gives a value from 0..1023. You are using that pin as a digital input so use digitalRead() which returns HIGH/LOW.

C arrays begin with index 0.

What are you seeing on your Serial monitor? You have lots of print() messages in your code. Which are happening?

1 Like

i'll check that out thanks!

I changed it to digitalRead thanks!
I think i got confused with the arrays, in the example receiver code, the buffer index starts at 1 also, so i thought i may have to replicated that. I made a debugging script at the end where i output all arrays and lengths to compare, they all seem legit though when i start at i=1. At the very end of the code i copied and pasted the serial monitor output which shows everything

You were right! :smiley:
the buffer array starts at i=1, so i had to copy it to tempArray[i-1]
it works now, thanks!

If anyone is interessted the working code is below, note that the values at tempArray[0] and rawDataNewArray[0] are not used by the transmitting routine, so don't worry about/ignore them.

//IR Transceiver code, (NOT WORKING - Not sure if signal is being transmitted)
// Trying to add both reciever and transmitter code into one piece of code.
// I added 2 push button to allow for receiver mode, and transmitter mode.
// Note, the signal/array length (RAW_DATA_LEN) is differnt for each remote manufacturer you are using (from 20 to 68 in my case)
// My plan is to record the IR signal, and place it in a Temp Array of length 100 (to act as a "catch all")
// I will then be able to determine the length of the signal, and store that value at the end of the array, Temp Array [99]
// When i want to transmit the signal, I will create a New Array with length TempArray[99]
// Then i will transmit this signal


#include <IRLibRecvPCI.h>
IRrecvPCI myReceiver(2); //IR Receiver on pin 2


#include <IRLibSendBase.h>
#include <IRLib_HashRaw.h>

//IRsendRaw mySender;


#define tempArrayLength  100  //100 in length
uint16_t tempArray[tempArrayLength]={0};  // Starts will all zero's

int NewArrayLength;  // New Array varies in length

int ButtonRec = 8;    // When pushed it will be in Receiver mode 
int ButtonTrans = A3;  // When pushed it will transmit the signal in the New Array (made using data from the tempArray)

int ListenFlag=1;   // not sure if i need this, it just stops the serial monitor from repeating ("Listening....")


void setup() {
  pinMode(9,OUTPUT);
   pinMode(ButtonRec,INPUT); // pulled UP, LOW when pushed
   pinMode(ButtonTrans,INPUT);// pulled UP, LOW when pushed
  digitalWrite(9,LOW); 
  Serial.begin(9600);

Serial.println("\n\n Record a signal first..., nothing in temp array to transmit  \n Hold down rec button while sending the signal");
}

void loop() {

  
if(digitalRead(ButtonRec)==LOW){ 
if(ListenFlag==1){Serial.println("Listetning... "); ListenFlag=0;myReceiver.enableIRIn();} //enables IR Receiver

  if (myReceiver.getResults()) {  //a signal has been stored in the buffer array

    for (int i = 1; i < tempArrayLength; i++) {                                                               ////  change to i = 0
     
      tempArray[i-1]=recvGlobal.recvBuffer[i], DEC;  //start copying the buffer array into the temp array

            if(tempArray[i-1] >0){  //If i'th value is still above zero then carry on the for loop      
            }
      
           else {          //If i'th value is zero, then we have reached the end of the recorded signal      
           tempArray[i-1]=1000;  // note all signals must end with a value of 1000, this is for the "sender" program to know??
           NewArrayLength=i;  // we now know the length of the signal     
           tempArray[99]=NewArrayLength;//store "length of signal" value in the 99th/last cell of the tempArray 
           i=tempArrayLength; // stop the for loop
            }      
    }

    myReceiver.disableIRIn(); // disable IR receiver ,may get confused when transmitting the signal?    
    ListenFlag=1; // reset flag
    Serial.println("\n\nSignal Recorded into Temp Array, RELEASE BUTTON NOW");
    delay(3000); // give yourself 3 seconds to release the rec button
    Serial.println(" \n\n Press Transmit buton to transmit the signal");   
  }
}


if(digitalRead(ButtonTrans)==LOW){//Transmit signal stored in temp array
Serial.println("\nTransmitting the received signal currently stored in temp array...");

uint16_t rawDataNewArray[tempArray[99]];    //Create a New Array of length tempArray[99]
   for (int i = 0; i < tempArray[99]; i++){   //copy values from temp array into New Array
 rawDataNewArray[i] = {tempArray[i]};         
   }
IRsendRaw mySender;
mySender.send(rawDataNewArray,tempArray[99],38);  // Transmit the signal in New Array

delay(500);
Serial.println("\nTemp Array Signal Sent");



/*
//-------------- Debugging stuff after transmitting attempt - Print all 3 arrays (intial buffer, temp, and New) and their lengths------------------

Serial.println("\n\n-------------------------");
Serial.println("        DEBUGGING        ");
Serial.println("-------------------------");
Serial.print("\n Original array from the programs buffer, Length  =  ");    //buffer array
Serial.println(recvGlobal.recvLength, DEC);

    for (bufIndex_t i = 1; i < recvGlobal.recvLength; i++) {
      Serial.print(recvGlobal.recvBuffer[i], DEC);     
      Serial.print(F(", "));
          if ((i % 8) == 0) {
              Serial.print(F("\n"));
           }
    }
Serial.println(F("1000;"));



Serial.println("\n\n-------------------------");
Serial.print("\n Temp Array copied from the programs buffer, Length  =  ");    //tempArray
Serial.println(tempArrayLength);
Serial.print("\n double check length  =  ");
Serial.println(sizeof(tempArray)/sizeof(tempArray[0]));  // original size is double what expected, apparently i need to divided by  "sizeof(tempArray[0])" to get correct answer
Serial.println("");

    for (bufIndex_t i = 0; i < tempArrayLength; i++) {
      Serial.print(tempArray[i]);     
      Serial.print(F(", "));
          if ((i % 8) == 0) {
              Serial.print(F("\n"));
           }
    }


Serial.println("\n\n-------------------------");
Serial.print("\n New Array copied from the Temp Array, Length  =  ");    //NewArray  or  rawDateNewArray
Serial.println(NewArrayLength);
Serial.print("\n double check length  =  ");
Serial.println(sizeof(rawDataNewArray)/sizeof(rawDataNewArray[0]));  // again, magic of some kind
Serial.println("");

   for (int i = 0; i < NewArrayLength; i++){   // Prints out the signal in New Array
    
      Serial.print(rawDataNewArray[i]);     
      Serial.print(F(", "));
       if ((i % 8) == 0) {
       Serial.print(F("\n"));
       }
   }

*/




}//END Transmit

}//END VOID MAIN LOOP


This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.