| This is the infra red receiver, with the pin connections and the remote control. |
|
I found an excellent infra red project description at
this webpage.
This project was especially interesting because it led me to the software 'Processing'.
This had appeared in my
software packages within the 'arduino' group (I'm on linux, remember), and I had installed it and looked it up on google. But I had never
used it before. See later on.
The code also uses the arduino pulseIn() function, which was a new one to me.
The infra red receiver can be very easy to get results from; simply plug in the 3 pins to your arduino,
and run the basic code below. The output from the infra red receiver appears in the serial window.
Other people have created libraries to interface with the IR receiver,
see here.
I followed the steps from that project, and here is my wiring and the code from his project below.
| Wired up - so simple yet so effective with the code. Open the serial monitor, and the codes appear that correspond to the button you pressed. |
|
//setup variables
int pinData= 12;
unsigned long lengthHeader;
unsigned long bit;
int byteValue;
void setup()
{
pinMode(pinData, INPUT);
Serial.begin(9600);
}
void loop()
{
//look for a header pulse from the IR Receiver
lengthHeader = pulseIn(pinData, LOW);
if(lengthHeader > 5000)
{
//step through each of the 32 bits that streams from the remote
byteValue = 0;
for(int i = 1; i <= 32; i++)
{
bit = pulseIn(pinData, HIGH);
//read the 8 bits that are specifically the key code
//use bitwise operations to convert binary to decimal
if (i > 16 && i <= 24)
if(bit > 1000)
byteValue = byteValue + (1 << (i - 17));
}
//send the key code to the processing.org program
Serial.print(byteValue);
Serial.flush();
}
delay(100);
}
|
And now the part that was new to me, the software 'processing'.
Assuming you've downloaded it and installed it: On my linux system, I downloaded it and ran it from my downloads directory.
Exit your arduino IDE, to release any locks on the USB port.
Run 'processing'; it brings up a window similar to the arduino IDE. Paste in the code below. (Full credit to the chap who
wrote this, on webpage
as above)
What the code does, is display a window with circles to simulate the remote control. Then, it listens on the serial port
(the USB port) for the codes that are output from the arduino. (It then decodes them, and makes one of the circles flash red).
The clever part, to me, was that the arduino outputs on the serial line in through the USB port. If the arduino IDE is running, and
you open your serial monitor, then the output is displayed there. But if you exit the arduino IDE, and set up 'processing'
to listen on the serial port, then a whole new set of possibilities opens up.
| Screenshot of me running 'processing', and the code below to interpret the Infra Red signals. |
|
Note the line of code 'String portName = "/dev/ttyACM0";'
This is my USB socket where my arduino is plugged in. You may need to change this; the easiest way to find your USB port
is to run this in a terminal :
ls -ltr /dev/tty*
import processing.serial.*;
//define variables used thourghout the program
Serial arduinoPort;
int rowPressed = 0;
int colPressed = 0;
int code = 0;
String codeText;
PFont font;
void setup()
{
//create the window
size(210, 325);
background(220,220,220);
//write the brand name at the bottom of the window
smooth();
font = loadFont("Courier-30.vlw");
textAlign(CENTER);
textFont(font);
fill(0,0,255);
text("I N S I G N I A", 105, 310);
//draw the silk screen colors on the remote
translate(-10, -15);
stroke(0,0,255);
ellipse(184,80,34,34);
ellipse(184,120,34,34);
ellipse(92,120,70,70);
ellipse(46,120,34,34);
ellipse(138,120,34,34);
ellipse(92,80,34,34);
ellipse(92,160,34,34);
rect(167,80,34,40);
rect(75,80,34,80);
rect(46,103,88,34);
//prepare for serial communication
//String portName = Serial.list()[1];
String portName = "/dev/ttyACM0";
arduinoPort = new Serial(this, portName, 9600);
}
void draw()
{
//initialize a few things each time.
rowPressed = 0;
colPressed = 0;
translate(-10, -15);
smooth();
//see if there is a serial message from the arduino
if (arduinoPort.available() > 0)
{
codeText = arduinoPort.readString();
code = int(codeText);
findPressed(code);
}
//use loops to draw the buttons on the remote
for(int col = 1; col <= 4; col++)
for(int row = 1; row <= 7; row++)
{
//draw the buttons different colors
if(row == rowPressed && col == colPressed) //red if pressed
fill(255, 0, 0);
else if((row == 4 && col == 1) || (row >= 5 && col <= 3)) //silk screened blue buttons
fill(0, 0, 255);
else //unpressed buttons
fill(240, 240, 240);
ellipse(col * 46, row * 40, 30, 30);
}
delay(100); //delay to leave the button lit a moment
}
//use the code id from the arduino to map to a button on the remote
void findPressed(int code)
{
switch(code)
{
case 28:
rowPressed = 1;
colPressed = 1;
break;
case 4:
rowPressed = 1;
colPressed = 2;
break;
case 17:
rowPressed = 1;
colPressed = 3;
break;
case 12:
rowPressed = 1;
colPressed = 4;
break;
case 80:
rowPressed = 2;
colPressed = 1;
break;
case 20:
rowPressed = 2;
colPressed = 2;
break;
case 81:
rowPressed = 2;
colPressed = 3;
break;
case 30:
rowPressed = 2;
colPressed = 4;
break;
case 84:
rowPressed = 3;
colPressed = 1;
break;
case 26:
rowPressed = 3;
colPressed = 2;
break;
case 85:
rowPressed = 3;
colPressed = 3;
break;
case 31:
rowPressed = 3;
colPressed = 4;
break;
case 18:
rowPressed = 4;
colPressed = 1;
break;
case 23:
rowPressed = 4;
colPressed = 2;
break;
case 73:
rowPressed = 4;
colPressed = 3;
break;
case 27:
rowPressed = 4;
colPressed = 4;
break;
case 5:
rowPressed = 5;
colPressed = 1;
break;
case 6:
rowPressed = 5;
colPressed = 2;
break;
case 7:
rowPressed = 5;
colPressed = 3;
break;
case 16:
rowPressed = 5;
colPressed = 4;
break;
case 9:
rowPressed = 6;
colPressed = 1;
break;
case 10:
rowPressed = 6;
colPressed = 2;
break;
case 11:
rowPressed = 6;
colPressed = 3;
break;
case 72:
rowPressed = 6;
colPressed = 4;
break;
case 13:
rowPressed = 7;
colPressed = 1;
break;
case 14:
rowPressed = 7;
colPressed = 2;
break;
case 15:
rowPressed = 7;
colPressed = 3;
break;
case 88:
rowPressed = 7;
colPressed = 4;
break;
default:
rowPressed = 1;
colPressed = 1;
}
}
|