Friday, January 23, 2009

IR Update

Continuing on with the IR program we've been playing with, here's the code that we used
#include (Servo.h) *these are actually <>, for some reason, blogspot is deleting it when i upload

int ir_pin = 7; //Sensor pin 1 wired through a 220 ohm resistor
int led_pin = 11; //"Ready to Receive" flag, not needed but nice
int debug = 1; //Serial connection must be started to debug
int start_bit = 3800; //Start bit threshold (Microseconds)
int bin_1 = 1900; //Binary 1 threshold (Microseconds)
int bin_0 = 900; //Binary 0 threshold (Microseconds)

int rm1=3;
int rm2=2;

int lm1=5;
int lm2=4;

int start=0;
int motor_initial = 0;

int servo_angle=80;
int key;
//#define servo_initial = 80;
Servo servo1;


void setup() {
pinMode(led_pin, OUTPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
pinMode(8, INPUT);//This shows when we're ready to receive
pinMode(9, INPUT);
pinMode(rm2, OUTPUT);
pinMode(lm2, OUTPUT);

digitalWrite(rm2, LOW);
digitalWrite(lm2, LOW);
digitalWrite(led_pin, LOW); //not ready yet
Serial.begin(19200);
servo1.attach(10);

analogWrite(rm1,start);
analogWrite(lm1,start);
servo1.write(80);

}




int getIRKey()
{
int data[13];
digitalWrite(led_pin, HIGH); //Ok, i'm ready to recieve
while(pulseIn(ir_pin, HIGH) < start_bit) { //Wait for a start bit
}
data[0] = pulseIn(ir_pin, HIGH); //Start measuring bits, I only want low pulses
data[1] = pulseIn(ir_pin, HIGH);
data[2] = pulseIn(ir_pin, HIGH);
data[3] = pulseIn(ir_pin, HIGH);
data[4] = pulseIn(ir_pin, HIGH);
data[5] = pulseIn(ir_pin, HIGH);
data[6] = pulseIn(ir_pin, HIGH);
data[7] = pulseIn(ir_pin, HIGH);
data[8] = pulseIn(ir_pin, HIGH);
data[9] = pulseIn(ir_pin, HIGH);
data[10] = pulseIn(ir_pin, HIGH);
data[11] = pulseIn(ir_pin, HIGH);
data[12] = pulseIn(ir_pin, HIGH);
digitalWrite(led_pin, LOW);


if(debug == 1) {
Serial.println("-----");
}
for(int i=1;i<=12;i++) { //Parse them
if (debug == 1) {
//Serial.println(data[i]);
}
if(data[i] > bin_1) { //is it a 1?
data[i] = 1;
Serial.print("1");
}
else {
if(data[i] > bin_0) { //is it a 0?
data[i] = 0;
Serial.print("0");
}
else {
data[i] = 2;
Serial.print("Error"); //Flag the data as invalid; I don't know what it is!
}
}
}

for(int i=1;i<=12;i++)
{ //Pre-check data for errors
if(data[i] > 1)
{
return -1; //Return -1 on invalid data
}
}

int result = 0;
int seed = 1;
for(int i=12;i>=9;i--)
{ //Convert bits to integer
if(data[i] == 1)
{
result += seed;
}
seed = seed * 2;
}

return result; //Return key number
}

void decode_IR()
{
switch(key)
{
case 2:
servo_angle = servo_angle + 1;
servo1.write(servo_angle);
break;

case 8:
servo_angle = servo_angle -1;
servo1.write(servo_angle);
break;

case 4:
start = 0;
start = start +1;
analogWrite(rm1, start);
analogWrite(lm1, 0);

// delay(100);
//digitalWrite(lm2, HIGH);
break;

case 6:
if(start<255)
{
start = start+1;
}

analogWrite(lm1, start);
analogWrite(rm1, 0);
//delay(100);
//digitalWrite(rm2, HIGH);

break;

case 5:
servo_angle=80;
start=0;
analogWrite(rm1, 0);
analogWrite(lm1, 0);
digitalWrite(rm2, LOW);
digitalWrite(lm2, LOW);
servo1.write(80);
break;

case 12:
if(start>-255)
{
start--;
throttle();
}
break;

case 13:

if(start<255)
{
start++;
throttle();
}
break;

}


}

void throttle()
{
if(start>0 && start<255)
{
digitalWrite(lm2, LOW);
digitalWrite(rm2, LOW);
analogWrite(rm1, start);
analogWrite(lm1, start);
}
else if (start<0>-255)
{
digitalWrite(lm2, HIGH);
digitalWrite(rm2, HIGH);
analogWrite(rm1, 255+start);
analogWrite(lm1, 255+start);
}
}


void loop()
{
for(ir_pin=6;ir_pin<10; ir_pin++)
{
key = getIRKey(); //Fetch the key
if (key != -1)
{
Serial.print(" Key Recieved: ");
Serial.println(key);
}
decode_IR();
}
}

The biggest difference between this video and the last video is the smoothing out of the motors and servos once after we hit the kill/all stop switch. So, things to do for next week

1. Fix Motor

2. Program motor and test if it can recognize multiple beacons (the code is in place to tell us which button is pressed, so we'll just need to see if we can have it follow multiple beacons.

3. I want to try to program the remotes to give off different addresses and headers, and then program the board to only recognize a certain set of code

4. Once we get the circuit pins in (hint hint) we can install the camera and get working on programming that. More tutorials for me from RoboRealm and Prof Mason's site.

5. Optimize code for smoother transitions for acceleration.

2 comments:

  1. The code is not pasting correctly. Play with the formating options... Try "preformatted" to see if you can get the code to paste as a formatted bock. There is also a big chunk of the code missing from the middle.

    ReplyDelete
  2. dang...it did the same thing when i posted it the first time =\ it looked fine right before i published too. trying again.....

    ReplyDelete