Infra Red PC control
December 18, 2008 Leave a comment
- IR receiver (from Radioshack – REALLY IMPORTANT : this actually automatically demodulates the 38khz IR signals to nice 1/0 signals)
- Arduino
- LCD panel – not really used yet, it only displays the code received from the remote

From Left to Right: USB cable, Arduino, LCD Panel, IR receiver and Led

Back of boards. The LCD Panel has a serial adapter on its back
Arduino
main.pde
#define DEBUG 0 //Serial connection must be started to debug
void setup() {
Serial.begin(115200);
ir_setup();
lcd_setup();
}
void loop() {
int key = getIRKey(); //Fetch the key
if(DEBUG){
Serial.print("Key: ");
Serial.println(key);
}
if(key >= 0){
lcd_write(key);
sendPC(key);
}
delay(50);
}
void sendPC(int cmd){
Serial.print(getByte(cmd));
}
byte getByte(int key){
if(key == 752) return 1;
if(key == 2800) return 2;
if(key == 720) return 3;
if(key == 3280) return 4;
if(key == 2672) return 5;
if(key == 144) return 6;
if(key == 2192) return 7;
if(key == 3536) return 8;
// 1..5 on the remote command DIGI PINS 8-12
if(key == 16) return 12;
if(key == 1168) return 15;
if(key == 3216) return 16;
if(key == 656) return 17;
return 249;
}
ir.pde
#define IR_PIN 7 //Sensor pin 1 wired through a 220 ohm resistor
#define LED_PIN 6 //"Ready to Recieve" flag, not needed but nice
#define START_MIN 2200 //Start bit threshold (Microseconds)
#define BIN_1 1000 //Binary 1 threshold (Microseconds)
#define BIN_0 400 //Binary 0 threshold (Microseconds)
#define SIZE 12
#define PULSE_TIMEOUT 10000 // in microseconds
void ir_setup() {
pinMode(LED_PIN, OUTPUT); //This shows when we're ready to recieve
pinMode(IR_PIN, INPUT);
digitalWrite(LED_PIN, LOW); //not ready yet
}
int getIRKey() {
int data[SIZE];
int time = 0;
while( pulseIn(IR_PIN, LOW, PULSE_TIMEOUT) 10){
return -2;
}
time++;
}
digitalWrite(LED_PIN, HIGH); //Ok, i'm ready to recieve
for(int i=0; i<SIZE; i++){
data[i] = pulseIn(IR_PIN, LOW, PULSE_TIMEOUT); //Start measuring bits, I only want low pulses
}
digitalWrite(LED_PIN, LOW);
for(int i=0;i BIN_1) { //is it a 1?
data[i] = 1;
}else{
if(data[i] > BIN_0) { //is it a 0?
data[i] = 0;
} else {
data[i] = 2; //Flag the data as invalid; I don't know what it is!
}
}
}
for(int i=0;i 1) {
return -1; //Return -1 on invalid data
}
}
int result = 0;
int seed = 1;
for(int i=SIZE-1; i>=0; i--) { //Convert bits to integer
if(data[i] == 1) {
result += seed;
}
seed = seed * 2;
}
return result; //Return key number
}
LCD.pde
#include #define rxPin 4 // rxPin is immaterial - not used - just make this an unused Arduino pin number
#define txPin 8 // pin 14 is analog pin 0, on a BBB just use a servo cable :), see Reference pinMode
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
void lcd_setup() {
pinMode(txPin, OUTPUT);
mySerial.begin(9600); // 9600 baud is chip comm speed
//mySerial.print("?G216"); // set display geometry, 2 x 16 characters in this case
//delay(100); // pause to allow LCD EEPROM to program
mySerial.print("?B88"); // set backlight to ff hex, maximum brightness
delay(100); // pause to allow LCD EEPROM to program
mySerial.print("?s6"); // set tabs to six spaces
delay(1000); // pause to allow LCD EEPROM to program
}
void lcd_write(int x) {
/*
mySerial.print("?B22"); // set backlight to ff hex, maximum brightness
delay(100);
mySerial.print("?B88"); // set backlight to ff hex, maximum brightness
delay(100);
*/
mySerial.print("?f");
mySerial.print("?x06");
mySerial.print(x);
}
Processing
IRcontrol.pde
import processing.serial.*;public static int MOUSE_INCR = 10;
Serial myPort; // The serial port
Robot robot;
int x=0;
int y=0;
float lastPress = 0;
void setup() {
size(400, 300);
// List all the available serial ports:
println(Serial.list());
String portName = Serial.list()[3];
myPort = new Serial(this, portName, 115200);
try {
robot = new Robot();
}
catch (AWTException e) {
e.printStackTrace();
}
}
void draw() {
background(0);
}
void serialEvent(Serial myPort) {
int inByte = myPort.read();
println(inByte);
if(inByte <= 4){
moveMouse(inByte);
}else if(inByte == 5){
pressMouse();
}
}
void moveMouse(int key){
if(key == 1){
y -= MOUSE_INCR;
}else if(key == 2){
y += MOUSE_INCR;
}else if(key == 3){
x -= MOUSE_INCR;
}else if(key == 4){
x += MOUSE_INCR;
}
if(x < 0) x = 0;
if(y 500){
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
lastPress = millis();
}
}
Recent Comments