Hello, I am trying to control a Hifi receiver via IR and am running into some problems. As the hardware part is trivial (I'm using just a a TSSP4400 LED on my LED pin), I am asing here, though I cannot rule out hardware issues (switching speed?). I am using an Arduino Ethernet Board.
The code below uses the same parameters as lirc config files. (However, I am not completely sure that I interpret them right, so that might be an issue.)
The generated signal is generally recognized by my IR Toy v2 (i.e. the LED is blinking on the toy, so it is basically 38kHz), but the output from IRGraph (from WinLIRC) is shit and it does not work on the receiver (of course). For real commands, no consistent signal is recognized by IRGraph.
Even a sendElement(HEADER) does not output
-------________ as I expected, but somthing like
-------__---____ or even
----_--__---____
I am quite at the beginning of Arduino and stuff, so any advice is appreciated.
#define BIT_IS_SET(i, bits) (1 << i & bits)
// LED connected to LED pin (13 for Uno, 9 for Arduino Ethernet)
const int LED_PIN = 9;
// Width of a pulse, in microseconds
const unsigned int FREQUENCY = 38;
// {pulse time, space time}, checked this via IRGraph on the remote.
const int HEADER[] = {8500,4250};
const int ONE[] = {500,1500};
const int ZERO[] = {500,500};
const int TRAIL[] = {500,0}; //{ptrail,0}
// # of bytes per command
const int BITS = 16;
const int PRE_DATA_BITS = 16;
//PRE_DATA is prepended to every command, no delay between
const unsigned int PRE_DATA = 0xA55A;
//Command
const unsigned int ON = 0x38C7;
const unsigned int OFF = ON;
const unsigned int VOLUME_UP = 0x50AF;
const unsigned int VOLUME_DOWN = 0xD02F;
const int TESTPULSE[] = {1000,0};
void setup()
{
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
}
//Send a single pulse/space combination modulated with FREQUENCY
void sendElement(const int times[])
{
static const int PERIOD = 1000/FREQUENCY;
for (int periods = times[0]/PERIOD; periods > 0; periods--) {
digitalWrite(LED_PIN, HIGH);
delayMicroseconds(PERIOD/2);
digitalWrite(LED_PIN, LOW);
delayMicroseconds(PERIOD/2);
}
delayMicroseconds(times[2]);
}
//Sends a signal
void sendSignal(const unsigned int signal, const int bits) {
for (int i = bits; i >= 0; i--)
{
if (BIT_IS_SET(i, signal)) {
sendElement(ONE);
} else {
sendElement(ZERO);
}
}
}
//Sends a full command with HEADER, PRE_DATA, command and TRAIL
void command(const unsigned int command) {
sendElement(HEADER);
sendSignal(PRE_DATA,PRE_DATA_BITS);
sendSignal(command,BITS);
sendElement(TRAIL);
}
void loop()
{
command(VOLUME_UP);
delay(1000);
command(VOLUME_DOWN);
delay(1000);
//sendElement(TESTPULSE);
}