I want to turn my air conditioner on/off with an arduino as the title says. So far I have gotten the raw signals, but I don't know how to properly output them. I have followed this guide from a friend on apknite group and captured this:
ON
Raw: (73) 9044, -4440, 688, -492, 672, -520, 668, -1628, 700, -1628, 696, -1604, 696, -1600, 700, -492, 672, -516, 672, -516, 672, -520, 672, -516, 672, -1628, 700, -488, 672, -520, 672, -516, 672, -516, 672, -520, 672, -516, 672, -516, 672, -520, 672, -516, 672, -520, 668, -520, 672, -516, 672, -520, 668, -1632, 696, -492, 672, -516, 672, -1656, 672, -516, 644, -1656, 696, -492, 672, -516, 672, -1628, 700, -492, 668,
OFF
Raw: (73) 9024, -4460, 640, -548, 640, -540, 652, -1676, 672, -516, 624, -1676, 700, -1600, 700, -488, 652, -540, 648, -540, 648, -540, 652, -540, 648, -1652, 696, -492, 648, -540, 648, -544, 648, -540, 648, -540, 652, -540, 648, -540, 648, -540, 652, -540, 648, -540, 648, -544, 648, -540, 648, -540, 648, -1652, 696, -492, 652, -540, 648, -1652, 696, -492, 648, -1676, 700, -492, 620, -572, 640, -1656, 696, -496, 648,
Now I used this code to send the signal:
#define txPinIR 2 //IR carrier output
unsigned char carrierFreq = 0; //default
unsigned char period = 0; //calculated once for each signal sent in initSoftPWM
unsigned char periodHigh = 0; //calculated once for each signal sent in initSoftPWM
unsigned char periodLow = 0; //calculated once for each signal sent in initSoftPWM
unsigned long sigTime = 0; //used in mark & space functions to keep track of time
unsigned long sigStart = 0; //used to calculate correct length of existing signal, to handle some repeats
//RAW NEC signal -32 bit with 1 repeat - make sure buffer starts with a Mark
unsigned int ON_RAW[] = {9044, -4440, 688, -492, 672, -520, 668, -1628, 700, -1628, 696, -1604, 696, -1600, 700, -492, 672, -516, 672, -516, 672, -520, 672, -516, 672, -1628, 700, -488, 672, -520, 672, -516, 672, -516, 672, -520, 672, -516, 672, -516, 672, -520, 672, -516, 672, -520, 668, -520, 672, -516, 672, -520, 668, -1632, 696, -492, 672, -516, 672, -1656, 672, -516, 644, -1656, 696, -492, 672, -516, 672, -1628, 700, -492, 668};
unsigned int OFF_RAW[] = {9024, -4460, 640, -548, 640, -540, 652, -1676, 672, -516, 624, -1676, 700, -1600, 700, -488, 652, -540, 648, -540, 648, -540, 652, -540, 648, -1652, 696, -492, 648, -540, 648, -544, 648, -540, 648, -540, 652, -540, 648, -540, 648, -540, 652, -540, 648, -540, 648, -544, 648, -540, 648, -540, 648, -1652, 696, -492, 652, -540, 648, -1652, 696, -492, 648, -1676, 700, -492, 620, -572, 640, -1656, 696, -496, 648};
#define NEC_HEX_VALUE 0x20DF22DDUL //UL makes this an unsigned long
#define NEC_BIT_COUNT 32
void setup() {
pinMode(txPinIR, OUTPUT);
pinMode(3, INPUT);
digitalWrite(3, 1);
pinMode(5, INPUT);
digitalWrite(5, 1);
}
void loop() {
//First send the NEC RAW signal defined above
if(digitalRead(3) == 0){
sendRawBuf(ON_RAW, sizeof(ON_RAW) / sizeof(ON_RAW[0]), 38);
delay(1000);
}
if(digitalRead(5) == 0){
sendRawBuf(OFF_RAW, sizeof(OFF_RAW) / sizeof(OFF_RAW[0]), 38);
delay(1000);
}
}
void sendRawBuf(unsigned int *sigArray, unsigned int sizeArray, unsigned char kHz) {
if (carrierFreq != kHz) initSoftPWM(kHz); //we only need to re-initialise if it has changed from last signal sent
sigTime = micros(); //keeps rolling track of signal time to avoid impact of loop & code execution delays
for (int i = 0; i < sizeArray; i++) {
mark(sigArray[i++]); //also move pointer to next position
if (i < sizeArray) { //check we have a space remaining before sending it
space(sigArray[i]); //pointer will be moved by for loop
}
}
}
void initSoftPWM(unsigned char carrierFreq) { // Assumes standard 8-bit Arduino, running at 16Mhz
//supported values are 30, 33, 36, 38, 40, 56 kHz, any other value defaults to 38kHz
//we will aim for a duty cycle of circa 33%
period = (1000 + carrierFreq / 2) / carrierFreq;
periodHigh = (period + 1) / 3;
periodLow = period - periodHigh;
// Serial.println (period);
// Serial.println (periodHigh);
// Serial.println (periodLow);
switch (carrierFreq) {
case 30 : //delivers a carrier frequency of 29.8kHz & duty cycle of 34.52%
periodHigh -= 6; //Trim it based on measurementt from Oscilloscope
periodLow -= 10; //Trim it based on measurementt from Oscilloscope
break;
case 33 : //delivers a carrier frequency of 32.7kHz & duty cycle of 34.64%
periodHigh -= 6; //Trim it based on measurementt from Oscilloscope
periodLow -= 10; //Trim it based on measurementt from Oscilloscope
break;
case 36 : //delivers a carrier frequency of 36.2kHz & duty cycle of 35.14%
periodHigh -= 6; //Trim it based on measurementt from Oscilloscope
periodLow -= 11; //Trim it based on measurementt from Oscilloscope
break;
case 40 : //delivers a carrier frequency of 40.6kHz & duty cycle of 34.96%
periodHigh -= 6; //Trim it based on measurementt from Oscilloscope
periodLow -= 11; //Trim it based on measurementt from Oscilloscope
break;
case 56 : //delivers a carrier frequency of 53.8kHz & duty cycle of 40.86%
periodHigh -= 6; //Trim it based on measurementt from Oscilloscope
periodLow -= 12; //Trim it based on measurementt from Oscilloscope
break;
case 38 : //delivers a carrier frequency of 37.6kHz & duty cycle of 36.47%
default :
periodHigh -= 6; //Trim it based on measurementt from Oscilloscope
periodLow -= 11; //Trim it based on measurementt from Oscilloscope
break;
}
}
void mark(unsigned int mLen) { //uses sigTime as end parameter
sigTime += mLen; //mark ends at new sigTime
unsigned long now = micros();
unsigned long dur = sigTime - now; //allows for rolling time adjustment due to code execution delays
if (dur == 0) return;
while ((micros() - now) < dur) { //just wait here until time is up
digitalWrite(txPinIR, HIGH);
if (periodHigh) delayMicroseconds(periodHigh);
digitalWrite(txPinIR, LOW);
if (periodLow) delayMicroseconds(periodLow);
}
}
void space(unsigned int sLen) { //uses sigTime as end parameter
sigTime += sLen; //space ends at new sigTime
unsigned long now = micros();
unsigned long dur = sigTime - now; //allows for rolling time adjustment due to code execution delays
if (dur == 0) return;
while ((micros() - now) < dur) ; //just wait here until time is up
}
The problem is that the signal lasts 2.5 seconds for some reason, while on the actual remote it lasts around 0.2 seconds and I am not sure what should I put in third thing in sendRawBuf.
I am using arduino nano.