Sending RAW IR signal by bit

Everything related to protocols and IR codes
Post Reply
manda3
Posts: 3
Joined: Wed Jun 12, 2024 2:15 pm

Sending RAW IR signal by bit

Post by manda3 »

Hello, everyone. I'm trying to send IR signals to a Gree AC. My raw data is very long and arduino couldn't support all the data of temperatures, therefore I had to figure out how the arrays were changing depending on the temperature. I did that and I created a code that changes the binary version of turn OFF command to the binary array of any temperature I want. So if I choose 16, the function will change the binary array to the binary array of temperature 16. Then there comes the problem I'm facing right now.

After I have the corret binary array to the temperature I want, I send that to a function that I created called SendRawByBit, which was supposed to work similarly to the sendRaw function, but it's not working and I don't know why. I'm reading the documentation and I don't quite understand everything... I think the problem might be related to the irsend.mark and irsend.space methods. I'm probrably missing something...

Code: Select all

#define HEADER_MARK 9040
#define HEADER_SPACE 4450

#define MARK_DURATION 700

#define SHORT_PAUSE 700
#define LONG_PAUSE 1650

#define SHORT_DELAY 20000
#define LONG_DELAY 40000

#define TOTAL_BYTES 25
#define MESSAGE_BITS (TOTAL_BYTES*8)+1


void sendRawByBit(byte byte_array[], int khz, int temp) {
   

    IrSender.enableIROut(khz);

    // Sending Header
    IrSender.mark(HEADER_MARK); 
    IrSender.space(HEADER_SPACE);

    int idx = 2; 

    for (int byteIndex = 0; byteIndex < TOTAL_BYTES; byteIndex++) {

        for (int bitIndex = 0; bitIndex < 8; bitIndex++) {  
          
          //700 20000
          if(idx== 72 || idx == 212|| idx == 352){
              IrSender.mark(MARK_DURATION);
              IrSender.space(SHORT_DELAY);
            
              idx++;
              idx++;
  
          }
          //700 40000
          if(idx == 138 || idx == 278){
             
              IrSender.mark(MARK_DURATION);
              IrSender.space(LONG_DELAY);
             
              idx++;
              idx++;
                     
          }
          //9040 4450
          if(idx == 140 || idx == 280){ 
              IrSender.mark(HEADER_MARK); 
              IrSender.space(HEADER_SPACE);
           
               idx++;
               idx++;
           
          }
                   
            // Getting current bit 
            int bit = (byte_array[byteIndex] >> (7 - bitIndex)) & 1;
            if (bit == 1) {
                IrSender.mark(MARK_DURATION);
                IrSender.space(LONG_PAUSE);
                idx++;
                idx++;
    
            } else {
                IrSender.mark(MARK_DURATION);
                IrSender.space(SHORT_PAUSE);
                idx++;
                idx++;
    
            }
        }
    }

    // Sending final bit
    if(temp == 28){
          IrSender.mark(MARK_DURATION);
          IrSender.space(SHORT_PAUSE);
        
    }else{
          IrSender.mark(MARK_DURATION);
          IrSender.space(LONG_PAUSE);
        
    }
    //sending final 700 
    IrSender.space(SHORT_PAUSE);


}
I know that is not the ideal to send the temperature to my sendRawByBit function, but in this case, I have very long arrays and when I convert to bytes, I get 25 bytes + 1 final bit. And this final bit changes if the temperature is 28. But anyway, I know that this code sends the right arrays because I already printed everything and compared with my original raw arrays and they are the same. So that's why I believe the problem is with mark and space methods. I read on the documentation that mark depends on the correct implementation of delayMicroseconds() and micros() for time pause... But I don't know what I need to change for it to work.

A normalized raw array looks like this:
Raw: 9000, 4500, 700, 1650, 700, 700, 700, 700, 700, 1650, 700, 1650, 700, 700, 700, 1650, 700, 700, 700, 1650, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 1650, 700, 1650, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 1650, 700, 700, 700, 1650, 700, 700, 700, 700, 700, 1650, 700, 700, 700, 20000, 700, 1650, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 1650, 700, 1650, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700,40000, 9000, 4500, 700, 1650, 700, 700, 700, 700, 700, 1650, 700, 1650, 700, 700, 700, 1650, 700, 700, 700, 1650, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 1650, 700, 1650, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 1650, 700, 1650, 700, 1650, 700, 700, 700, 700, 700, 1650, 700, 700, 700, 20000, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 1650, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 1650, 700, 700, 700, 1650, 700, 700, 700, 40000, 9000, 4500, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 1650, 700, 700, 700, 1650, 700, 700, 700, 1650, 700, 700, 700, 20000, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 700, 1650, 700, 700, 700, 1650, 700

All the raw arrays when converted to binary follow this sequence:

700 1650 = 1
700 700 = 0

Hh - header (9000,4000)
S = short (20000)
L = long (40000)
p - 700

Hh (35bits) pS (32bits) pLHh(35bits) pS (32bits) pLHh (35bits) pS (32bits).


The idx on the previous code is to keep track of where I need to put all those headers, short, long, and p values...

Thanks a lot.
manda3
Posts: 3
Joined: Wed Jun 12, 2024 2:15 pm

Re: Sending RAW IR signal by bit

Post by manda3 »

Hey, thanks for the reply. I tried to apply some changes to my code but still wasn't working. Could it be related with delays generated by the if statments I use?

I used a second arduino to record the signals that I was sending to the AC, but it wasn't the right array, which, of course, explains why the AC didn't react to the comand.

This is the array I received for 16°C:

Raw: (417) 9080, -4852, 732, -9208, 732, -9664, 736, -9668, 732, -10708, 732, -10708, 760, -9640, 756, -10684, 760, -9644, 708, -9692, 732, -9668, 736, -9652, 752, -9644, 752, -9668, 736, -9664, 736, -9644, 776, -9628, 752, -9668, 760, -9620, 752, -9648, 756, -9664, 760, -9644, 732, -10708, 756, -10684, 756, -9628, 752, -9644, 756, -9668, 760, -9640, 736, -9644, 752, -10692, 752, -9668, 732, -10688, 776, -9648, 732, -9644, 756, -10708, 736, -9640, 752, -20388, 752, -2060, 752, -9620, 732, -9672, 752, -9668, 756, -9628, 752, -9668, 732, -9668, 736, -9664, 760, -9624, 752, -9644, 756, -9668, 760, -9620, 752, -9672, 732, -9668, 756, -10684, 760, -10680, 736, -9668, 756, -9620, 756, -9644, 780, -9644, 732, -9648, 728, -9696, 704, -9696, 732, -9668, 736, -9640, 756, -9668, 736, -9664, 760, -9644, 728, -10692, 756, -10684, 756, -10708, 732, -10704, 760, -40360, 9100, -4856, 732, -2008, 756, -988, 732, -1056, 704, -6600, 728, -10712, 756, -9644, 736, -10704, 760, -9644, 732, -9648, 752, -9668, 732, -9668, 760, -9620, 776, -9648, 756, -9644, 736, -9644, 752, -9668, 732, -9668, 736, -9668, 732, -9668, 732, -9668, 732, -9652, 776, -10684, 732, -10708, 736, -9664, 736, -9664, 732, -9672, 732, -9668, 732, -9648, 724, -10716, 780, -10660, 756, -10688, 752, -9644, 732, -9692, 736, -10704, 736, -9664, 756, -20388, 732, -1056, 728, -9608, 752, -9668, 732, -9668, 732, -9668, 736, -9644, 756, -9668, 760, -9640, 756, -9624, 748, -9652, 780, -9644, 732, -9648, 752, -9648, 756, -9664, 736, -9664, 736, -9668, 704, -9696, 732, -9652, 776, -9624, 752, -9648, 752, -10708, 736, -9664, 736, -9668, 732, -9664, 736, -9668, 732, -9652, 752, -9644, 728, -9672, 756, -9668, 732, -9648, 752, -10708, 736, -9640, 756, -40376, 9104, -4860, 728, -1040, 700, -1044, 728, -1036, 700, -5524, 732, -9664, 736, -9648, 752, -9668, 732, -9668, 760, -9620, 752, -9672, 732, -9668, 756, -9624, 752, -9648, 752, -9668, 736, -9668, 732, -9644, 756, -9668, 704, -9680, 752, -9648, 752, -9648, 780, -9648, 756, -9616, 752, -9668, 736, -9664, 756, -9648, 728, -9676, 732, -9668, 732, -9668, 732, -9668, 732, -10688, 756, -9644, 756, -10684, 756, -9664, 732, -10716, 756, -9640, 756, -20368, 752, -1056, 732, -9600, 752, -9648, 756, -9664, 736, -9668, 732, -9648, 748, -9676, 732, -9668, 732, -9644, 756, -9672, 728, -9668, 760, -9620, 756, -9664, 736, -9668, 728, -9676, 728, -9652, 752, -9668, 732, -9648, 776, -9624, 752, -9648, 756, -9664, 736, -9648, 748, -9672, 732, -9668, 760, -9644, 732, -9648, 752, -9668, 732, -9648, 756, -9644, 756, -10684, 756, -9664, 732,

The correct not standardized raw array for 16 is:

(419) 9040, 4452, 656, 1652, 680, 524, 708, 500, 680, 1624, 684, 1624, 704, 500, 684, 1624, 680, 524, 708, 496, 684, 524, 708, 496, 684, 524, 704, 500, 708, 500, 680, 524, 680, 524, 684, 520, 684, 524, 680, 524, 680, 524, 684, 524, 704, 1600, 684, 1624, 680, 524, 684, 524, 680, 524, 680, 524, 684, 524, 680, 1624, 684, 524, 704, 1600, 684, 524, 680, 524, 680, 1628, 704, 500, 708, 19916, 680, 1628, 680, 524, 708, 500, 704, 500, 680, 524, 708, 500, 708, 496, 708, 500, 708, 496, 708, 496, 684, 524, 708, 496, 708, 500, 708, 496, 708, 1600, 708, 1600, 680, 524, 680, 524, 684, 524, 680, 524, 708, 500, 680, 524, 708, 496, 708, 500, 708, 496, 708, 500, 680, 524, 708, 500, 708, 1596, 708, 1600, 680, 1624, 684, 1624, 704, 39896, 9064, 4432, 704, 1604, 704, 500, 704, 500, 704, 1604, 704, 1600, 708, 496, 708, 1600, 708, 496, 708, 500, 704, 500, 708, 500, 704, 500, 704, 504, 704, 500, 704, 504, 704, 500, 704, 504, 676, 552, 652, 532, 700, 504, 704, 500, 704, 1600, 704, 1600, 708, 500, 708, 496, 708, 500, 708, 496, 708, 500, 704, 1600, 708, 1600, 704, 1600, 708, 500, 708, 496, 708, 1600, 684, 520, 708, 19896, 732, 500, 704, 500, 708, 496, 708, 500, 704, 500, 708, 496, 708, 500, 704, 500, 708, 500, 704, 500, 708, 500, 704, 500, 708, 496, 708, 496, 708, 500, 704, 500, 708, 496, 708, 500, 708, 496, 708, 500, 708, 1600, 704, 500, 708, 496, 708, 500, 704, 500, 708, 496, 708, 500, 704, 500, 708, 496, 708, 500, 680, 1624, 708, 500, 708, 39888, 9064, 4432, 656, 548, 708, 500, 708, 500, 680, 524, 704, 500, 708, 500, 704, 500, 708, 500, 704, 500, 704, 504, 680, 524, 704, 500, 708, 500, 704, 500, 680, 528, 704, 500, 708, 496, 708, 500, 704, 500, 708, 496, 708, 500, 704, 500, 708, 500, 704, 500, 708, 500, 704, 500, 680, 524, 704, 500, 708, 500, 704, 1604, 704, 500, 704, 1600, 708, 500, 704, 1600, 708, 504, 700, 19920, 708, 500, 704, 500, 708, 500, 704, 500, 708, 500, 704, 500, 708, 496, 708, 496, 708, 500, 704, 500, 708, 500, 680, 524, 708, 496, 708, 500, 704, 500, 708, 500, 704, 500, 680, 524, 708, 500, 704, 500, 704, 500, 704, 504, 704, 500, 704, 504, 704, 500, 704, 500, 708, 500, 704, 500, 704, 504, 704, 1600, 708, 500, 704, 1600, 704,

I don't understand where this -9000 ~ -10000 space is coming from.


I tried to do as it was shown in the example you gave, I used the mark, space and initSoftPWM, with both codes the array I got was still not right...

Thank you!
User avatar
AnalysIR
Site Admin
Posts: 792
Joined: Sat Aug 31, 2013 3:51 pm
Location: Dublin, Ireland
Contact:

Re: Sending RAW IR signal by bit

Post by AnalysIR »

Some tips:

- to debug replace the

Code: Select all

IrSender.mark(MARK_DURATION);   
IrSender.space(SHORT_DELAY);
with serial print statements to see/verify if your logic is correct.
- make use of else if statements to avoid multiple checks for every bit
- then I would either use an index into a buffer to get the timings to send (eg use the sendraw example from the library).
- alternatively use the send from flash example I linked if you are short on SRAM.

Hope that helps
manda3
Posts: 3
Joined: Wed Jun 12, 2024 2:15 pm

Re: Sending RAW IR signal by bit

Post by manda3 »

The logic is correct, I printed them before to check and in theory I'm sending the right arrays. I even used them in sendRaw example to check if they were the same, and they worked fine.

I think I have to track the delay that happens when I use the statements. From what I could see, the spaces are the ones getting messed up.
I guess there's some delay in the code that adds up to the time the IR Pin is low.. maybe that's what happening.
I'm just not sure how to track that time.


About the buffer and flash memory option: for this project, I can't use the flash memory (ideally - but I guess if nothing else works, I might end up using it. But it would be a shame that all the work I had to find the binary patterns would go to waste :( ).

And as for the buffer, I have a code that works fine and changes the temperature through a buffer. I use the binary array and transform it back to the standardized raw array. The problem is that my arduino is not just changing temperatures. I also have some sensor modules and a bluetooth module that enables me to send the temperature value I want through my phone. So when I put together these both parts, there's not enough space for everything, which is why I have to use this SendRawByBit function that I'm trying to create, since with the buffer the program crashes after a little time...


Thanks again for your ideas, I appreciate :)
User avatar
AnalysIR
Site Admin
Posts: 792
Joined: Sat Aug 31, 2013 3:51 pm
Location: Dublin, Ireland
Contact:

Re: Sending RAW IR signal by bit

Post by AnalysIR »

As I suggested earlier your code needs a major redo.

You are running thru every if statement for each bit(so 200 times 8 if compares). Use the else if approach so that only one comparison is made for each bit
The most common occurrence should be the first if statement and so on.

This should be your first step.
Post Reply