- Tue Nov 29, 2011 7:46 pm
#136352
Hi everyone I am curretnly working on a Ardubot (http://www.sparkfun.com/tutorials/129) the idea is to get the basic movement down and then attach a Bluetooth adapter and a camera to pan and tilt. All the controls are from the keyboard through a terminal. the first problem I have run into is the wheels. I get them to move but when I open the terminal and press let say w to move forward and release the button it stays going forward. the idea is to use w,a,s and d for the movement of the robot but I would only like to have the robot move if the button is held down. can anyone think of how to fix this or point me in the right direction.
Code: Select all
ps I understand it has something to do with the data already being sent to the buffer on the arduino. maybe a way to clear the buffer or set "stop" to be the default?
/*
auther Sam
*/
unsigned char PIN_HBRIDGE_1A = 9;
unsigned char PIN_HBRIDGE_2A = 6;
unsigned char PIN_HBRIDGE_3A = 5;
unsigned char PIN_HBRIDGE_4A = 3;
void stopMotor(){
digitalWrite(PIN_HBRIDGE_1A, LOW);
digitalWrite(PIN_HBRIDGE_2A, LOW);
digitalWrite(PIN_HBRIDGE_3A, LOW);
digitalWrite(PIN_HBRIDGE_4A, LOW);
}
void farward(){
digitalWrite(PIN_HBRIDGE_1A, LOW);
digitalWrite(PIN_HBRIDGE_2A, HIGH);
digitalWrite(PIN_HBRIDGE_3A, HIGH);
digitalWrite(PIN_HBRIDGE_4A, LOW);
}
void reverse(){
digitalWrite(PIN_HBRIDGE_1A, HIGH);
digitalWrite(PIN_HBRIDGE_2A, LOW);
digitalWrite(PIN_HBRIDGE_3A, LOW);
digitalWrite(PIN_HBRIDGE_4A, HIGH);
}
void turnLeft(){
digitalWrite(PIN_HBRIDGE_1A, LOW);
digitalWrite(PIN_HBRIDGE_2A, HIGH);
digitalWrite(PIN_HBRIDGE_3A, LOW);
digitalWrite(PIN_HBRIDGE_4A, LOW);
}
void turnRight(){
digitalWrite(PIN_HBRIDGE_1A, LOW);
digitalWrite(PIN_HBRIDGE_2A, LOW);
digitalWrite(PIN_HBRIDGE_3A, HIGH);
digitalWrite(PIN_HBRIDGE_4A, LOW);
}
void setup(){
Serial.begin(9600);
pinMode(PIN_HBRIDGE_1A, OUTPUT);
pinMode(PIN_HBRIDGE_2A, OUTPUT);
pinMode(PIN_HBRIDGE_3A, OUTPUT);
pinMode(PIN_HBRIDGE_4A, OUTPUT);
Serial.println("Setup Complete");
}
void loop(){
if (Serial.available()){
char ch = Serial.read();
switch(ch){
case 'w':
farward();
Serial.flush();
break;
case 'd':
turnRight();
break;
case 'a':
turnLeft();
break;
case 's':
reverse();
break;
default:
stopMotor;
}
}
}