## Introduction: Remote Control Controlled Via Internet

This is a kinda complex tutorial, although I’ll try to make it as easy to understand for a wide amount of programmers.

Before proceeding, keep in mind that you’ll need at least one Altair.

## Step 1: ​Theory

First of all, we should know that the infrared light is simply light with an specific colour out of the bounds of the colours we can see with the naked eye.

In fact, the infrared wave length goes from 750 nanometers (ms), which is a little beyond of the wave length we can’t perceive. Bigger wave lengths are different things, suchs as microwaves, for instance.

As a small experiment in order to see infrared light from a remote control, you could point at a controller using your phone’s camera (or any other camera) while it’s on, then press a button. You should see something like on the picture number of this step.

The problem is that everything that surrounds us emit infrared light, with different wave length but still infrared (that’s why, when you see through a camera you don’t see everything shining; cameras only perceive certain wave length, which allow us to see the remote control IR light). Keeping in mind, a method in order to send information using IR light regardless of all the IR light “noise” is needed.

## Step 2: The Solution

Something similar to morse code was developed in order to solve this issue.

Modulation is in fact, the name that it has, and basically is making a IR LED blink @ 35kHz (although it may vary among manufacturers).

In fact sending a command, comes out as a blinks with spaces between them (measured in microseconds), in order to “attract the attention” of the receiver. The receiver, only measures the time the set of blinks lasted. Between block and block the space seems close to nonexistent to us, although on the IR context is more than enough.

To make it clearer, take a look to the gif on the page.

Keep in mind that the receiver is always returning “1”, while the sender is not sending anything.

## Step 3: ​Practice

The theory was explained, now it remains to put into practice all the new knowledge.

Before making a controller with Altair, the first task is learning how to read the information emitted by any Remote Control; from a TV, for instance.

For warming up, I’d like to you to make this little test. (Instead of batteries you could use directly the Altair, plugging the wires to 3.3v and to GND).

This experiment will blink every time the IR receiver receives something from a remote control. Cool, huh?

Alright, now.. with this code you’ll make the Altair to read whatever a controller transmits.

Check the embedded CODE (Had too many format troubles editing code here)

http://www.arduino.cc/en/pmwiki.php?n=Reference/P.... ( _BV(2) explanation)

Here I show you how to connect the Altair in order to work as a receiver.

ALWAYS check on the DATASHEET of your receiver (avoid burning it, like it happened to me while doing this tutorial).

## Step 5: The Emissor (and Receiver)

Now, the explanation in order to make that the receiver you just did also emits. The connection of the Altair, the components with the protoboard will be shown after the code. Likewise, the assistance links will be immediately after the code, although the code will be commented the best way possible. Any doubt with any part of the code, don’t hesitate to comment it on the section below, no one is born knowing, specially with all this which is pretty complex if you are not familiarized with low level programming.

The next code has one or more modification to the last code, it’s recommended to have it as a different file.

Check the embedded code!

http://www.fiz-ix.com/2012/01/how-to-configure-ard... (Inner timers)

## Step 6: Embedded Aquila Action

Having all this implemented, the received code could be simply copied form the Serial Monitor, and paste it as a new matrix in the code. Then, adding the Aquila action functions in order to call that (or those) matrix and have a Remote Control even better through the Aquila platform (the advantage of this is that you won’t need to buy more push buttons and that you’ll be able to automate as I will mention now).

See this as a “simple” application for the Altair, imagine configuring it so at N time of the day your TV would turn on and start recording your favorite show.

Now, we will add a few more lines to implement the Aquila platform. I’m including a matrix with a lot of numbers; this are the ones the remote control that I used sends to the TV I used (that in fact I extracted directly from the Serial Monitor). You’ll have to change those numbers for the ones your Serial Monitor returns

Although, I left them there so you could see a matrix structure.

//Because the timing is very important, we won’t be using digitalRead

//for it’s very slow compared with what we’ll be using.

//Check the reference link after the code; back on the tutorial.
//This (PIND) thing means that form pin 0 to 7 will be READ ONLY
#define IRpin_PIN PIND

#include <Wire.h>
#include <Mesh.h>
#include <AquilaProtocol.h></p><p>#define MASK 2 //This will be taken as a binary number (00000010)
#define Boton 33 //Built-in Altair Button (It has inverted logif! LOW = Pressed, HIGH = NOT pressed)
#define Boton2 10 //Extern button, normal logic! HIGH = Pressed, LOW = Not pressed!
#define IR 9
//The longest pulse that will be read is going to be 65ms  (when the code applies, it’ll be a microseconds function
//So it will NOT be taken as 65000ms (being this 65 seconds); in fact 65 ms is A LOT in this environment.
#define MAXPULSE 65000

//Nuestra resolución de tiempo, entre más grande es el valor mejor,
//Our time resolution, the bigger the value the better, for it’s more precise
//but it takes longer to process it
//And we would be losing the precision we won not using digitalRead
#define RESOLUTION 20

//El pulso que se recibirá será en pares, 100 pares en éste ámbito es muchísimo
//The received pulse will be in pairs, 100 pairs is actually a LOT.
uint16_t pulses[100][2]; // Remember that a “value” consist in an ON and OFF from the LED, so in the matrix is stored in pairs.
uint8_t currentpulse = 0; // It’ll be used to know how many pairs from ON and OFF have being received.
bool full = false;</p><p>uint16_t turnON [120][2]={{46264,1100},{540,1160},{480,1960},{540,1080},{520,1120},{520,1120},{520,1100},{540,1080},{560,1060},{1420,1040},{600,1800},{720,920},{20324,980},{640,980},{660,1780},{740,880},{760,860},{760,880},{760,880},{760,880},{760,860},{1600,900},{760,1700},{760,880},{20384,940},{700,920},{700,1740},{760,880},{760,860},{780,860},{760,880},{760,900},{720,900},{1600,880},{740,1740},{760,860}};
//This is an example of how a code for turning ON a TV looks like, It may be different to yours, so you’ll have to modify the matrix in the next functions (also this one) :)</p><p> uint16_t vUP[120][2]={0};
uint16_t vDWN[120][2]={0};
uint16_t cUP[120][2]={0};
uint16_t cDWN[120][2]={0};
uint16_t inp[120][2]={0};     </p><p>bool encender (uint8_t param, bool gotParam)
{
Enviar (turnON);
delay(500);
}</p><p>bool volumenUP (uint8_t param, bool gotParam)
{
Enviar(vUP);
delay(500);
}</p><p>bool volumenDWN (uint8_t param, bool gotParam)
{
Enviar(vDWN);
delay(500);
}</p><p>bool canalUP (uint8_t param, bool gotParam)
{
Enviar(cUP);
delay(500);
}</p><p>bool canalDWN (uint8_t param, bool gotParam)
{
Enviar(cDWN);
delay(500);
}</p><p>bool input (uint8_t param, bool gotParam)
{
Enviar(inp);
delay(500);
}

void setup(void)
{
Serial.begin(9600);

Mesh.begin();
Aquila.begin();
Aquila.setClass("mx.makerlab.test");
Aquila.setName("Control");

Mesh.announce(HUB);

Serial.begin(9600);
pinMode(Boton, INPUT);
pinMode(Boton2, INPUT);
//    pinMode(Boton2, INPUT_PULLUP);
pinMode(IR, OUTPUT);
pinMode(15, OUTPUT);
pinMode(14, OUTPUT);</p><p>//           7        6       5        4       3         2       1      0
//TCCR2A - [COM2A1, COM2A0, COM2B1, COM2B0, reserved, reserved, WGM21, WGM20]
//           7        6       5        4       3       2    1     0
//TCCR2B - [FOC2A, FOC2B, reserved, reserved, WGM22, CS22, CS21, CS20]</p><p>  TCCR2A = _BV(COM2A0) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20); // A ‘or’ is applies to all of them and it looks like this -> TCCR2A = 0110 0011  (99)
TCCR2B = _BV(WGM22) | _BV(CS21); // A ‘or’ is applies and it looks like this -> TCCR2B= 0000 1010 = 10
OCR2A = 25; //Inner time gets to 25 (splitted between the frequency of the micro controller (16MHz) times 2 (because the split will only show the half of the wave, we want to know the duration of a complete cycle.
TCCR2A ^= _BV(COM2A1); // Prepares the IR LED</p><p>  Serial.println("Ready to decode IR!"); //After this message we’ll know that it has just finished the SETUP part and stats reading pulses.
}</p><p>void IRcarrier (unsigned int matrix)
{
if (matrix != 0)
{
TCCR2A ^= _BV(COM2A1); //Changes the 8’th bit. Turns the LED ON.
delayMicroseconds(matrix); //Waits  matrix-microseconds with the LED turned ON.
TCCR2A ^= _BV(COM2A1); //Turns the LED off.
}
}</p><p>void Enviar(uint16_t pulse[120][2])
{
digitalWrite(15, LOW);
for (int i = 0; i < 120; i++)
{
if (pulse[i][0] == 0) //If a matrix slot is empty, it leaves the loop.
{
break;
}
delayMicroseconds(pulse[i][0]); //Waits n microseconds wi the IR LED off.
IRcarrier(pulse[i][1]); //Enters the función sending the delay that takes the ON from the IR
}
digitalWrite(15, HIGH);
}</p><p>//The only thing this función does is printing everything received after the pulse is over.
void printpulses()
{
Serial.print("{");
for (uint8_t i = 0; i < currentpulse; i++)
{
Serial.print("{");
Serial.print(pulses[i][0], DEC); //DEC <- En caso de que no esté en decimales, esto lo obliga a pasarlo a decimales.
Serial.print(",");
Serial.print(pulses[i][1], DEC);
Serial.print("}");
}
Serial.print("};\n");
full = true;
}

void loop(void)
{</p><p>Mesh.loop();
Aquila.loop();  </p><p>  digitalWrite(15, HIGH); //Built-in LEDs, inverted logic.
digitalWrite(14, HIGH); //Both begin off.</p><p>  if (digitalRead(Boton2) == HIGH || (currentpulse != 0))
{
{
Serial.println("Leyendo!");
digitalWrite(14, LOW); //GREEN LED ON!
full = false;
}

uint16_t highpulse, lowpulse; // The pulse will be temporary stored on this variables.
highpulse = lowpulse = 0; //This process will be splitter in to steps, when the LED is ON; HighPulse and when it’s off; LowPulse.

//All this ‘while’ function is for when the pulse is on High (the LED ON)

//  --- Complex Explanation --- //
//We will apply what is on the pin 0-7 a “mask” and we take whatever is on the position 00000100
// If we order the pins this way -> 76543210 we an see in a clearer way that the 00000100 is on the antepenultimate position, same as (xxxxx2xx)
//The '&' operator generates a truth table between 00000100 and what is connected to the pins (the micro controller will ignore whatever is not on the pin 2, basically).
//So, if there’s anything connected to the PIN 2, it will work.
while (IRpin_PIN & (1 << MASK) && full == false)
{
highpulse++;
delayMicroseconds(RESOLUTION);

// If the pulse is to long, the scanning ceases.
// Writes down everything received and resets counters.
if ((highpulse >= MAXPULSE) && (currentpulse != 0))
{
printpulses();
currentpulse=0;
return;
}
}
// if it never got to the ‘if loop’, then it stores the new high pulse to the matrix.
pulses[currentpulse][0] = highpulse;

//---------------------------------------------

// The process repeats for the second part of the pulse (remember that it consists on a HIGH and a LOW together)
// Is the same as on the other WHILE, only with another notation, where _BV is BITVALUE. Check the assistance links.
// And this one will count every time the receiver receives nothing.
while (! ( IRpin_PIN & _BV(MASK) ) && full == false)
{
lowpulse++;
delayMicroseconds(RESOLUTION);

// If the pulse is too long, the scanning ceases.
// Writes down everything received and resets counters.
if ((lowpulse >= MAXPULSE) && (currentpulse != 0))
{
printpulses();
currentpulse=0;
return;
}
}
pulses[currentpulse][1] = lowpulse * RESOLUTION;
// A ON-OFF pulse has been read
// the whole process will be repeated until some command exceeds the length of a ON or a OFF (and enters to the ‘if loop’).
currentpulse++;
}</p><p>if (digitalRead(Boton) == LOW && full == true)
{
Enviar(pulses);
delay(500);
}
digitalWrite(14,HIGH);
}</p>