Anyone familiar with arduino I am trying to remote control my robot using a remote I got from mindkits but I can't seem to get it to work at all. It doesn't seem to be picking up the button presses. Here is a link to the remote webpage http://www.mindkits.co.nz/store/kits/ir-kit-for-arduino and this is my source code.
#include <Servo.h>
#define sensorIR 0 //Must be an analog pin
float sensorValue, cm; //Must be of type float for pow()
Servo leftServo;
Servo rightServo;
Servo sensorServo;
Servo servo0;
Servo servo1;
Servo servo2;
Servo servo3;
//int sensorPosition = 90;
const int buttonPin = 2;
int intPosition = 0;
int intSecondCounter = 0;
int intWaypoint = 0;
int ch1; // Here's where we'll keep our channel values
int ch2;
String content = "";
char character;
Servo servo = servo0;
int intServoPosition = 0;
#define IR_BIT_LENGTH 32 // number of bits sent by IR remote
#define FirstLastBit 15 // divide 32 bits into two 15 bit chunks for integer variables. Ignore center two bits. they are all the same.
#define BIT_1 1500 // Binary 1 threshold (Microseconds)
#define BIT_0 450 // Binary 0 threshold (Microseconds)
#define BIT_START 4000 // Start bit threshold (Microseconds)
#define IR_PIN 2 // IR Sensor pin
#define LED_PIN 13 // LED goes off when signal is received
int debug = 0; // flag as 1 to output raw IR pulse data stream length in microseconds
int output_verify = 1; // flag as 1 to print decoded verification integers. same number for all buttons
int output_key = 1; // flag as 1 to print decoded key integers
int remote_verify = 16128; // verifies first bits are 11111100000000 different remotes may have different start codes
void setup() {
Serial.begin(9600);
//leftServo.attach(9);
//rightServo.attach(8);
//sensorServo.attach(7);
//servo0.attach(2);
//servo1.attach(3);
//servo1.write(0);
//servo2.attach(4);
//servo2.write(40);
servo3.attach(4);
servo3.write(130);
pinMode(buttonPin, INPUT);
pinMode(5, INPUT); // Set our input pins as such
pinMode(6, INPUT);
pinMode(IR_PIN, INPUT);
}
void loop() {
int key = get_ir_key();
do_response(key);
delay(130);
}
void do_response(int key)
{
if (output_key)
{
Serial.print("Key ");
Serial.println(key);
}
switch (key)
{
case 32640: // turns on UUT power
Serial.println("POWER");
break;
case 32385: // FUNC/STOP turns off UUT power
Serial.println("FUNC/STOP");
break;
case 32130: // |<< ReTest failed Test
Serial.println("|<<");
break;
case 32002: // >|| Test
Serial.println(">||");
break;
case 31875: // >>| perform selected test number
Serial.println(">>|");
break;
case 32512: // VOL+ turns on individual test beeper
Serial.println("VOL+");
break;
case 31492: // VOL- turns off individual test beeper
Serial.println("VOL-");
break;
case 31620: // v scroll down tests
Serial.println("v");
break;
case 31365: // ^ scroll up tests
Serial.println("^");
break;
case 30982: // EQ negative tests internal setup
Serial.println("EQ");
break;
case 30855: // ST/REPT Positive tests Select Test and Repeat Test
Serial.println("ST/REPT");
break;
case 31110: // 0
Serial.println("0");
break;
case 30600: // 1
Serial.println("1");
break;
case 30472: // 2
Serial.println("2");
break;
case 30345: // 3
Serial.println("3");
if (intServoPosition > 180)
{
intServoPosition = 130;
}
else
{
intServoPosition+=10;
}
servo3.write(intServoPosition);
break;
case 30090: // 4
Serial.println("4");
break;
case 29962: // 5
Serial.println("5");
break;
case 29835: // 6
Serial.println("6");
break;
case 29580: // 7
Serial.println("7");
break;
case 29452: // 8
Serial.println("8");
break;
case 29325: // 9
Serial.println("9");
break;
default:
{
Serial.print("Key ");
Serial.print(key);
Serial.println(" not programmed");
}
break;
}
}
/*
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, HIGH) < BIT_START);
read_pulse(pulse);
pulse_to_bits(pulse, bits);
RemoteVerify(bits);
return bits_to_int(bits);
}
/*
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[])
{
for (int i = 0; i < IR_BIT_LENGTH; i++)
{
pulse[i] = pulseIn(IR_PIN, HIGH);
}
}
/*
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[])
{
if (debug) { Serial.println("-----"); }
for(int i = 0; i < IR_BIT_LENGTH; i++)
{
if (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");
}
}
}
/*
check returns proper first 14 check bits
*/
void RemoteVerify(int bits[])
{
int result = 0;
int seed = 1;
//Convert bits to integer
for(int i = 0 ; i < (FirstLastBit) ; i++)
{
if(bits[i] == 1)
{
result += seed;
}
seed *= 2;
}
if (output_verify)
{
Serial.print("Remote ");
Serial.print(result);
Serial.println(" verification code");
}
if (remote_verify != result) {delay (60); get_ir_key();} //verify first group of bits. delay for data stream to end, then try again.
}
/*
convert an array of binary values to a single base-10 integer
*/
int bits_to_int(int bits[])
{
int result = 0;
int seed = 1;
//Convert bits to integer
for(int i = (IR_BIT_LENGTH-FirstLastBit) ; i < IR_BIT_LENGTH ; i++)
{
if(bits[i] == 1)
{
result += seed;
}
seed *= 2;
}
return result;
}