I have hacked up some code to use my universal remote with the arduino and the adafruit motor shield. However the way I have it now when I press the button I have designated for forward or other motion it just keeps going until I reset the board or press my designated stop button how could I change the code so it to only goes when the button is held down?
thanks Stump56
int ir_pin = 2;
int led_pin = 13; //"Ready to Recieve" flag, not needed but nice
int debug = 0; //Serial connection must be started to debug
int start_bit = 2000; //Start bit threshold (Microseconds)
int bin_1 = 1000; //Binary 1 threshold (Microseconds)
int bin_0 = 400; //Binary 0 threshold (Microseconds)
#include <AFMotor.h>
AF_DCMotor motor(2, MOTOR12_64KHZ);
AF_DCMotor motor2(1, MOTOR12_64KHZ);
void setup() {
pinMode(led_pin, OUTPUT); //This shows when we're ready to recieve
pinMode(ir_pin, INPUT);
digitalWrite(led_pin, LOW); //not ready yet
Serial.begin(9600);
motor.setSpeed(255);
motor2.setSpeed(255);
}
void Forward(){
motor.run(BACKWARD);
motor2.run(BACKWARD);
Serial.print("forwards");
}
void Backward(){
motor.run(FORWARD);
motor2.run(FORWARD);
Serial.print("backwards");
}
void SpinRight(){
motor.run(FORWARD);
motor2.run(BACKWARD);
Serial.print("right");
}
void SpinLeft(){
motor.run(BACKWARD);
motor2.run(FORWARD);
Serial.print("left");
}
void stop(){
motor.run(RELEASE);
motor2.run(RELEASE);
Serial.print("stop");
}
void loop() {
int key = getIRKey(); //Fetch the key
Serial.print("Key Recieved: ");
Serial.println(key);
if ((key==1474))
Forward();
delay (100);
if ((key==1475))
Backward();
delay (100);
if ((key==1506))
SpinLeft();
delay (100);
if ((key==1505))
SpinRight();
delay (100);
if ((key==1429)
stop();
delay (100);
}
int getIRKey() {
int data[12];
digitalWrite(led_pin, HIGH); //Ok, i'm ready to recieve
while(pulseIn(ir_pin, LOW) < 2200) { //Wait for a start bit
}
data[0] = pulseIn(ir_pin, LOW); //Start measuring bits, I only want low pulses
data[1] = pulseIn(ir_pin, LOW);
data[2] = pulseIn(ir_pin, LOW);
data[3] = pulseIn(ir_pin, LOW);
data[4] = pulseIn(ir_pin, LOW);
data[5] = pulseIn(ir_pin, LOW);
data[6] = pulseIn(ir_pin, LOW);
data[7] = pulseIn(ir_pin, LOW);
data[8] = pulseIn(ir_pin, LOW);
data[9] = pulseIn(ir_pin, LOW);
data[10] = pulseIn(ir_pin, LOW);
data[11] = pulseIn(ir_pin, LOW);
digitalWrite(led_pin, LOW);
if(debug == 1) {
Serial.println("-----");
}
for(int i=0;i<11;i++) { //Parse them
if (debug == 1) {
Serial.println(data[i]);
}
if(data[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<11;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=0;i<11;i++) { //Convert bits to integer
if(data[i] == 1) {
result += seed;
}
seed = seed * 2;
}
return result; //Return key number
}