Geekzone: technology news, blogs, forums
Guest
Welcome Guest.
You haven't logged in yet. If you don't have an account you can register now.


BoredNerd

36 posts

Geek


#127239 3-Aug-2013 15:54
Send private message

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;
}

Create new topic
BoredNerd

36 posts

Geek


  #871273 3-Aug-2013 17:16
Send private message

OK sorted it turns out it was loose wires.

Create new topic





News and reviews »

Logitech Introduces New G522 Gaming Headset
Posted 21-May-2025 19:01


LG Announces New Ultragear OLED Range for 2025
Posted 20-May-2025 16:35


Sandisk Raises the Bar With WD_BLACK SN8100 NVME SSD
Posted 20-May-2025 16:29


Sony Introduces the Next Evolution of Noise Cancelling with the WH-1000XM6
Posted 20-May-2025 16:22


Samsung Reveals Its 2025 Line-up of Home Appliances and AV Solutions
Posted 20-May-2025 16:11


Hisense NZ Unveils Local 2025 ULED Range
Posted 20-May-2025 16:00


Synology Launches BeeStation Plus
Posted 20-May-2025 15:55


New Suunto Run Available in Australia and New Zealand
Posted 13-May-2025 21:00


Cricut Maker 4 Review
Posted 12-May-2025 15:18


Dynabook Launches Ultra-Light Portégé Z40L-N Copilot+PC with Self-Replaceable Battery
Posted 8-May-2025 14:08


Shopify Sidekick Gets a Major Reasoning Upgrade, Plus Free Image Generation
Posted 8-May-2025 14:03


Microsoft Introduces New Surface Copilot+ PCs
Posted 8-May-2025 13:56


D-Link A/NZ launches DWR-933M 4G+ LTE Cat6 Wi-Fi 6 Mobile Hotspot
Posted 8-May-2025 13:49


Synology Expands DiskStation Lineup with DS1825+ and DS1525+
Posted 8-May-2025 13:44


JBL Releases Next Generation Flip 7 and Charge 6
Posted 8-May-2025 13:41



Geekzone Live »

Try automatic live updates from Geekzone directly in your browser, without refreshing the page, with Geekzone Live now.



Are you subscribed to our RSS feed? You can download the latest headlines and summaries from our stories directly to your computer or smartphone by using a feed reader.