SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By samman946
#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


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

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?
By esklar81
#136500
samman946,
It does not look to me like an issue with a buffer. Each of your subroutines turns on the H-bridge for its intended configuration, but nothing but the "stopmotor" subroutine turns the H-bridge off. Therefore, unless you send a subsequent command, the H-bridge stays where you put it and the wheels keep doing what you told them to do.

If you add a time delay and a call of the "stopmotor" subroutine to each of the directional subroutines, you should get the robot to drive in the direction you commanded, for the length of time of the delay, then stop.

Detecting expliticly whether a key is still being held might be a bit of a challenge, as it would probably require poking around inside the computer's keyboard driver. However, there is an approach you might want to try:
  1. Confirm that, if you hold a key, the computer keeps typing that character.
  2. Measure the amount of time between character entries if you hold a key down. (It might be easier to do this by holding a key for a known amount of time, counting the characters, and dividing.)
  3. Set the delay in each of the directional subroutines to half of the time between character entries.
  4. Test this. If my idea is workable, holding a key should result in a jerky motion, as the motors should run briefly, then stop and wait for the repeated character to cause the motors to run briefly again.
  5. Increase the delay in small increments, until the motion is not jerky, but the robot stops promptly when you release the key.


Good Luck,
Eric
By samman946
#136581
Thank you so much for the help I did get it to work. I am using putty as my terminal program. Here is my code to move it back forward left and right.
Code: Select all
int time = 33;

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);
  delay(time);
}
void farward(){
  digitalWrite(PIN_HBRIDGE_1A, LOW);
  digitalWrite(PIN_HBRIDGE_2A, HIGH);
  digitalWrite(PIN_HBRIDGE_3A, HIGH);
  digitalWrite(PIN_HBRIDGE_4A, LOW);
  delay(time);
}
void reverse(){
  digitalWrite(PIN_HBRIDGE_1A, HIGH);
  digitalWrite(PIN_HBRIDGE_2A, LOW);
  digitalWrite(PIN_HBRIDGE_3A, LOW);
  digitalWrite(PIN_HBRIDGE_4A, HIGH);
  delay(time);
}
void turnLeft(){
  digitalWrite(PIN_HBRIDGE_1A, LOW);
  digitalWrite(PIN_HBRIDGE_2A, HIGH);
  digitalWrite(PIN_HBRIDGE_3A, LOW);
  digitalWrite(PIN_HBRIDGE_4A, HIGH);
  delay(time);
}
void turnRight(){
  digitalWrite(PIN_HBRIDGE_1A, HIGH);
  digitalWrite(PIN_HBRIDGE_2A, LOW);
  digitalWrite(PIN_HBRIDGE_3A, HIGH);
  digitalWrite(PIN_HBRIDGE_4A, LOW);
  delay(time);
}
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(){
    char ch = Serial.read();
    if(ch == '0'){
      stopMotor();
    }
    else if(ch == 'w'){
      farward();
    }
    else if(ch == 's'){
      reverse();
    }
    else if(ch == 'a'){
      turnLeft();
    }
    else if(ch == 'd'){
      turnRight();
    }
    else{
      stopMotor();
    }
}    
            
  

it seems to work well so the next step is to add wireless Bluetooth and get this thing chasing the cat. It will be a while before I can buy anything giving me time to do some research if anyone has any suggestions for beginners at Bluetooth let me know. thank you again.