I will start off with what I am trying to do.. I thought it would be simple, but I was soooo wrong. I have a remote I'm not using, and I want to use it for my Arduino to turn LEDs on and off. I have used scripts to grab the inputs of HEX and raw code for each button. When I try to turn around and use that code to execute commands, it does not work. Serial shows its seeing the input, but it wont do anything.
This is what I used to decode the remote buttons:
/*
* IRhashdecode - decode an arbitrary IR code.
* Instead of decoding using a standard encoding scheme
* (e.g. Sony, NEC, RC5), the code is hashed to a 32-bit value.
*
* An IR detector/demodulator must be connected to the input RECV_PIN.
* This uses the IRremote library: http://arcfn.com/2009/08/multi-protocol-infrared-remote-library.html
*
* The algorithm: look at the sequence of MARK signals, and see if each one
* is shorter (0), the same length (1), or longer (2) than the previous.
* Do the same with the SPACE signals. Hszh the resulting sequence of 0's,
* 1's, and 2's to a 32-bit value. This will give a unique value for each
* different code (probably), for most code systems.
*
* You're better off using real decoding than this technique, but this is
* useful if you don't have a decoding algorithm.
*
* Copyright 2010 Ken Shirriff
* http://arcfn.com
*/
#include <IRremote.h>
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
irrecv.enableIRIn(); // Start the receiver
Serial.begin(9600);
}
// Compare two tick values, returning 0 if newval is shorter,
// 1 if newval is equal, and 2 if newval is longer
// Use a tolerance of 20%
int compare(unsigned int oldval, unsigned int newval) {
if (newval < oldval * .8) {
return 0;
}
else if (oldval < newval * .8) {
return 2;
}
else {
return 1;
}
}
// Use FNV hash algorithm: http://isthe.com/chongo/tech/comp/fnv/#FNV-param
#define FNV_PRIME_32 16777619
#define FNV_BASIS_32 2166136261
/* Converts the raw code values into a 32-bit hash code.
* Hopefully this code is unique for each button.
*/
unsigned long decodeHash(decode_results *results) {
unsigned long hash = FNV_BASIS_32;
for (int i = 1; i+2 < results->rawlen; i++) {
int value = compare(results->rawbuf[i], results->rawbuf[i+2]);
// Add value into the hash
hash = (hash * FNV_PRIME_32) ^ value;
}
return hash;
}
void loop() {
if (irrecv.decode(&results)) {
Serial.print("'real' decode: ");
Serial.print(results.value, HEX);
Serial.print(", hash decode: ");
Serial.println(decodeHash(&results), HEX); // Do something interesting with this value
irrecv.resume(); // Resume decoding (necessary!)
}
}
#define LEDPIN 13
void blink() {
digitalWrite(LEDPIN, HIGH);
delay(200);
digitalWrite(LEDPIN, LOW);
delay(200);
}
// Blink the LED the number of times indicated by the Philips remote control
// Replace loop() with this for the blinking LED example.
void blink_example_loop() {
if (irrecv.decode(&results)) {
unsigned long hash = decodeHash(&results);
switch (hash) {
case 0x322ddc47: // 0 (10)
blink(); // fallthrough
case 0xdb78c103: // 9
blink();
case 0xab57dd3b: // 8
blink();
case 0x715cc13f: // 7
blink();
case 0xdc685a5f: // 6
blink();
case 0x85b33f1b: // 5
blink();
case 0x4ff51b3f: // 4
blink();
case 0x15f9ff43: // 3
blink();
case 0x2e81ea9b: // 2
blink();
case 0x260a8662: // 1
blink();
break;
default:
Serial.print("Unknown ");
Serial.println(hash, HEX);
}
irrecv.resume(); // Resume decoding (necessary!)
}
}
This is what I was trying to adapt to actually do something with the remote:
// 0.1 by pmalmsten http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1176098434
// 0.2 by farkinga
// 0.3 by farkinga - adds cool behaviors
#define IR_BIT_LENGTH 12 // number of bits sent by IR remote
#define BIT_1 1000 // Binary 1 threshold (Microseconds)
#define BIT_0 400 // Binary 0 threshold (Microseconds)
#define BIT_START 2000 // Start bit threshold (Microseconds)
#define IR_PIN 11 // Sensor pin 1 wired through a 220 ohm resistor
#define LED_PIN 8 // first LED output
#define POWER_PIN 7 // second LED output, corresponds to power button
#define DEBUG 1 // Serial connection must be started to debug
int runtime_debug = 0; // flag to output raw IR pulse data
int output_key = 0; // flag to print decoded key integers
int power_button = 0; // flag to indicate if power LED is on
int power_level = 128; // value (0-255) for power LED intensity
void setup() {
pinMode(LED_PIN, OUTPUT); //This shows when we're ready to recieve
pinMode(POWER_PIN, OUTPUT); //This is the "power on" indicator
pinMode(IR_PIN, INPUT);
digitalWrite(LED_PIN, LOW);
Serial.begin(9600);
}
void loop() {
digitalWrite(LED_PIN, HIGH);
int key = get_ir_key();
digitalWrite(LED_PIN, LOW); // turn LED off while processing response
do_response(key);
delay(100); // short delay to cancel duplicate keypresses
}
/*
wait for a keypress from the IR remote, and return the
integer mapping of that key (e.g. power button on remote returns
the integer 1429)
*/
int get_ir_key()
{
int pulse[IR_BIT_LENGTH];
int bits[IR_BIT_LENGTH];
do {} //Wait for a start bit
while(pulseIn(IR_PIN, LOW) < BIT_START);
read_pulse(pulse, IR_BIT_LENGTH);
pulse_to_bits(pulse, bits, IR_BIT_LENGTH);
return bits_to_int(bits, IR_BIT_LENGTH);
}
/*
respond to specific remote-control keys with different behaviors
*/
void do_response(int key)
{
switch (key)
{
case 1437: // record button
Serial.println("toggle debug pulse");
runtime_debug = 1 - runtime_debug;
break;
case 1498: // display button
Serial.println("Toggle key output");
output_key = 1 - output_key;
break;
case 1429: // power button
Serial.println("Power");
power_button = 1 - power_button;
set_power();
break;
case 1424: // channel up button
Serial.println("Channel Up");
break;
case 1425: // channel down button
Serial.println("Channel Down");
break;
case 3342: // up rocker/pause
power_level+=1;
set_power();
break;
case 3343: // down rocker/stop
power_level-=1;
set_power();
break;
case 3344: // left rocker/rewind
if (power_level < 50)
{
power_level-=3;
}
else
{
power_level-=10;
}
set_power();
break;
case 3345: // right rocker/fast forward
if (power_level < 50)
{
power_level+=3;
}
else
{
power_level+=10;
}
set_power();
break;
case 3352: // play button
blip_power();
break;
default:
if (output_key)
{
Serial.print("Key ");
Serial.print(key);
Serial.println(" not programmed");
}
break;
}
}
/*
use pulseIn to receive IR pulses from the remote.
Record the length of these pulses (in ms) in an array
*/
void read_pulse(int pulse[], int num_bits)
{
for (int i = 0; i < num_bits; i++)
{
pulse[i] = pulseIn(IR_PIN, LOW);
}
}
/*
IR pulses encode binary "0" as a short pulse, and binary "1"
as a long pulse. Given an array containing pulse lengths,
convert this to an array containing binary values
*/
void pulse_to_bits(int pulse[], int bits[], int num_bits)
{
if (DEBUG || runtime_debug) { Serial.println("-----"); }
for(int i = 0; i < num_bits ; i++)
{
if (DEBUG || runtime_debug) { Serial.println(pulse[i]); }
if(pulse[i] > BIT_1) //is it a 1?
{
bits[i] = 1;
}
else if(pulse[i] > BIT_0) //is it a 0?
{
bits[i] = 0;
}
else //data is invalid...
{
Serial.println("Error");
}
}
}
/*
convert an array of binary values to a single base-10 integer
*/
int bits_to_int(int bits[], int num_bits)
{
int result = 0;
int seed = 1;
//Convert bits to integer
for(int i = 0 ; i < num_bits ; i++)
{
if(bits[i] == 1)
{
result += seed;
}
seed *= 2;
}
return result;
}
/*
set the brightness of the "power LED" depending on the power_level
variable.
*/
void set_power()
{
power_level = constrain(power_level, 0, 200);
analogWrite(POWER_PIN, power_level * power_button);
// if the power level is above the max or below the min...
if ((power_level == 200) || (power_level == 0))
{
blink_led();
}
}
/*
make LED blink rapidly
*/
void blink_led()
{
for (int i = 0; i < 5; i++)
{
analogWrite(LED_PIN, 0);
delay(50);
analogWrite(LED_PIN, 1);
delay(50);
}
}
/*
neat little routine to fade both LEDs in a sequence. Currently,
this is called by do_response() when the "play" button is pressed.
*/
void blip_power()
{
int max_val = 100;
for (int i = 0; i < max_val; i++)
{
analogWrite(POWER_PIN, max_val-i);
analogWrite(LED_PIN, i);
delay(15);
}
for (int i = max_val; i >= 0; i--)
{
analogWrite(POWER_PIN, max_val-i);
analogWrite(LED_PIN, i);
delay(15);
}
set_power();
}
I appreciate ANY advice you have or any code I should try. My goal is to simply flash a few pins depending on the button i press. I was wondering if anyone knew about some simple code I can use to decode my remote and use it. Keep in mind, I am quite new to this, and the code I am using, I did not write.
Thanks in advance!