/*
Leading Pulse 9000us
Space 4500us
Address + Inv. 27000us
Command + Inv. 27000us
Stop Bit 562us
-------------------------
TOTAL 68062us
*/
unsigned long powerOn = 0b00000000111111110011000011001111;
unsigned long volumeUp = 0b00000000111111110100001010111101;
unsigned long volumeDown = 0b00000000111111111100001000111101;
void setup() {
DDRB |= (1<<PINB5);
}
void loop() {
sendData(volumeUp);
delay(40);
repeatBurst(); // increment volume
repeatBurst(); // increment volume again
delay(5000);
sendData(volumeDown);
delay(40);
repeatBurst(); // decrement volume
repeatBurst(); // decrement volume again
delay(5000);
}
void repeatBurst() {
carrierFreq(9000); // leading pulse 9.0 ms
delayMicroseconds(2250); // space 2.25ms
carrierFreq(562); // pulse burst 562us
delay(108);
}
void carrierFreq(int usDuration) {
uint16_t numCycles = usDuration * 0.076;
for (int i=0; i<=numCycles; i++) {
PORTB ^= (1<<PINB5);
delayMicroseconds(13);
}
}
void sendData(unsigned long dataSequence) {
carrierFreq(9000); // leading pulse 9.0 ms
delayMicroseconds(4500); // space 4.5 ms
for (int i=0; i<32; i++) {
// turn on carrier for 1 bit time (562us)
carrierFreq(562);
// go silent, check for a 1 or 0
if (dataSequence & 0x80000000) {
delayMicroseconds(1675);
}
else {
delayMicroseconds(562);
}
dataSequence <<= 1; // bitshift 1
}
carrierFreq(562); // send stop bit
}