Two servo car wasd

Hey, I need help for a college project. I’m doing a two-wheeled car with two servo, like this: ‘two servo car arduino’

I need to drive the car to walk forward, backward, turn left and right with the keypad ‘wasd’.

I tried the following code:

#include <Servo.h>
// Create two servo instances
Servo back_left_servo;
Servo back_right_servo;
// Sending signals to servos:
// 90 is no servo movement (mid-point)
// 0 is full speed forward
// 180 is full speed backward
int back_left_speed = 90;
int back_right_speed = 90;
#define MOVE_FORWARD 'w'
#define MOVE_BACK 's'
#define MOVE_LEFT 'a'
#define MOVE_RIGHT 'd'
#define FULL_STOP 'e'
// not all servos stop at exactly 90, so adjust default stop here
#define LEFT_DEFAULT_STOP 94  
#define RIGHT_DEFAULT_STOP 89
void setup() {
  Serial.begin(9600); 
  // Attach the servos on Arduino PWM pin 3 and PWM pin 5
  back_left_servo.attach(3);
  back_right_servo.attach(5);
  // Set servos to mid-range, ie, default stop
  back_left_servo.write(LEFT_DEFAULT_STOP);
  back_right_servo.write(RIGHT_DEFAULT_STOP);
}

void loop(){
  char byte = 0;
  // press q to cancel and exit
  while (byte != 'q') {
    Serial.readBytes(&byte, 1);
    // press w to move forward
    if (byte == MOVE_FORWARD) {
      back_left_speed = back_left_servo.read();
      back_left_speed -= 5;  // Increase speed by 5
      back_left_servo.write(back_left_speed);

      back_right_speed = back_right_servo.read();
      back_right_speed += 5;  // Increase speed by 5
      back_right_servo.write(back_right_speed);
      Serial.print("move forward \n");
      byte = 0;
    }
    // press s to move backward
    if (byte == MOVE_BACK) {
      back_left_speed = back_left_servo.read();
      back_left_speed += 5;  // Decrease speed by 5
      back_left_servo.write(back_left_speed);
      
      back_right_speed = back_right_servo.read();
      back_right_speed -= 5;  // Decrease speed by 5
      back_right_servo.write(back_right_speed);
      Serial.print("move back \n");
      byte = 0;
    }
    // press a to turn left
    if (byte == MOVE_LEFT) {
      back_left_speed = back_left_servo.read();
      back_left_speed += 5;  // Decrease speed by 5
      back_left_servo.write(back_left_speed);

      back_right_speed = back_right_servo.read();
      back_right_speed += 5;  // Increase speed by 5
      back_right_servo.write(back_right_speed);
      Serial.print("move left \n");
      byte = 0;
    }
    // press d to turn right
    if (byte == MOVE_RIGHT) {
      back_left_speed = back_left_servo.read();
      back_left_speed -= 5;  // Increase speed by 5
      back_left_servo.write(back_left_speed);

      back_right_speed = back_right_servo.read();
      back_right_speed -= 5;  // Decrease speed by 5
      back_right_servo.write(back_right_speed);
      Serial.print("move right \n");
      byte = 0;
    }
    // press e to default stop
    if (byte == FULL_STOP) {
      back_left_servo.write(LEFT_DEFAULT_STOP);
      back_right_servo.write(RIGHT_DEFAULT_STOP);
      Serial.print("full stop \n");
      byte = 0;
    }
  }
  
  Serial.print("Done \n");
  Serial.end();
}

But it does not work correctly, because a wheel rotates in the opposite direction to the other when pressing ‘w’, and both servos rotate in different speed.

Is this correct Arduino code?

I just need to drive the car with the two servo motors

help me pls :wink:

Please put your code in its own window as seen in other posts. This can be done by placing     [code] and [/code]  around the code. This makes it easier for others to read.

You will not get a lot of help unless you make it easy for us. Make sure the code is formatted for easy reading.

Weedpharma

That code has the servos going in the same direction, but one servo is "backwards" and needs to be given the opposite signal to go forward. To go forward, one will need to be at 180 and the other at 0.

weedpharma: Please put your code in its own window as seen in other posts. This can be done by placing     [code] and [/code]  around the code. This makes it easier for others to read.

You will not get a lot of help unless you make it easy for us. Make sure the code is formatted for easy reading.

Weedpharma

Ok, this post has been modified.

Typical setup for using continuous rotation servos for bot wheels to get same direction rotation.

//zoomkat 10-09-14 serial servo test
//type servo position 0 to 180 in serial monitor
//opposite side servos rotate together

String readString;
#include <Servo.h> 
Servo myservo1;  // create servo object to control a servo 
Servo myservo2;

void setup() {
  Serial.begin(9600);
  myservo1.attach(8);
  myservo2.attach(9);
  Serial.println("servo-test"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the String readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured String 
    int n = readString.toInt();  //convert readString into a number
    myservo1.write(n);
    myservo2.write(180-n); //turns opposide servo in desired direction
    readString="";
  } 
}