WADS combined with Servo

I am trying to combine the WADS sketch with another one which will allow me to control the motors using the ‘w’, ‘s’, ‘a’, and ‘d’ keys and then also control a servo using the ‘q’ and ‘e’ keys. I want to mount a camera on the servo eventually. here is where I’m at

#include <Servo.h>

Servo servo1;
Servo servo2;

/** Adjust these values for your servo and setup, if necessary **/
int servo1Pin = 14; // control pin for servo motor
int servo2Pin = 11; // control pin for servo motor
int minPulse = 900; // minimum servo position
int maxPulse = 2100; // maximum servo position
int turnRate = 100; // servo turn rate increment (larger value, faster rate)
int refreshTime = 20; // time (ms) between pulses (50Hz)

/** The Arduino will calculate these values for you **/
int centerServo; // center servo position
int pulseWidth; // servo pulse width
int moveServo1; // raw user input
int moveServo2; // raw user input
long lastPulse = 0; // recorded time (ms) of the last pulse

int E1 = 6; //M1 Speed Control
int E2 = 5; //M2 Speed Control
int M1 = 8; //M1 Direction Control
int M2 = 7; //M2 Direction Control

void setup(void) {
pinMode(servo1Pin, OUTPUT); // Set servo pin as an output pin
pinMode(servo2Pin, OUTPUT); // Set servo pin as an output pin
centerServo = maxPulse - ((maxPulse - minPulse)/2);
pulseWidth = centerServo; // Give the servo a starting point (or it floats)
Serial.begin(9600);
Serial.println(" Arduino Serial Servo Control");
Serial.println(“Press q or e to move, spacebar to center”);
Serial.println();
int i;
for(i=5;i<=8;i++)
pinMode(i, OUTPUT);
Serial.begin(9600);
}

void loop(void) {

while (Serial.available() < 1) {} // Wait until a character is received
char val = Serial.read();
int leftspeed = 255; //255 is maximum speed
int rightspeed = 255;
int slowspeed = 0;
int nospeed = 0;
switch(val) // Perform an action depending on the command
{
case ‘s’://Move Forward
forward (leftspeed,rightspeed);
break;
case ‘w’://Move Backwards
reverse (leftspeed,rightspeed);
break;
case ‘d’://Turn Left
left (leftspeed,rightspeed);
break;
case ‘a’://Turn Right
right (leftspeed,rightspeed);
break;
case ‘f’: //Stop
halt (slowspeed,nospeed);
break;
default:
stop();
break;
// wait for serial input
if (Serial.available() > 0) {
// read the incoming byte:
moveServo1 = Serial.read();
moveServo2 = Serial.read();

// ASCII 'a' is 44, ASCII 'd' is 46
if (moveServo1 == 113) { pulseWidth = pulseWidth - turnRate; }
if (moveServo1 == 101) { pulseWidth = pulseWidth + turnRate; }
if (moveServo1 == 32) { pulseWidth = centerServo; }

// ASCII 's' is 44, ASCII 'w' is 46
if (moveServo2 == 115) { pulseWidth = pulseWidth - turnRate; }
if (moveServo2 == 119) { pulseWidth = pulseWidth + turnRate; }
if (moveServo2 == 32) { pulseWidth = centerServo; }

// stop servo pulse at min and max
if (pulseWidth > maxPulse) { pulseWidth = maxPulse; }
if (pulseWidth < minPulse) { pulseWidth = minPulse; }

// print pulseWidth back to the Serial Monitor (uncomment to debug)
// Serial.print("Pulse Width: ");
// Serial.print(pulseWidth);
// Serial.println("us");   // microseconds

}

// pulse the servo every 20 ms (refreshTime) with current pulseWidth
// this will hold the servo’s position if unchanged, or move it if changed
if (millis() - lastPulse >= refreshTime) {
digitalWrite(servo1Pin, HIGH); // start the pulse
delayMicroseconds(pulseWidth); // pulse width
digitalWrite(servo1Pin, LOW); // stop the pulse
lastPulse = millis(); // save the time of the last pulse
}
if (millis() - lastPulse >= refreshTime) {
digitalWrite(servo2Pin, HIGH); // start the pulse
delayMicroseconds(pulseWidth); // pulse width
digitalWrite(servo2Pin, LOW); // stop the pulse
lastPulse = millis(); // save the time of the last pulse
}
}

void stop(void) //Stop
{
digitalWrite(E1,LOW);
digitalWrite(E2,LOW);
}
void forward(char a,char b)
{
analogWrite (E1,a);
digitalWrite(M1,LOW);
analogWrite (E2,b);
digitalWrite(M2,LOW);}
void reverse (char a,char b)
{
analogWrite (E1,a);
digitalWrite(M1,HIGH);
analogWrite (E2,b);
digitalWrite(M2,HIGH);
}
void left (char a,char b)
{
analogWrite (E1,a);
digitalWrite(M1,HIGH);
analogWrite (E2,b);
digitalWrite(M2,LOW);
}
void right (char a,char b)
{
analogWrite (E1,a);
digitalWrite(M1,LOW);
analogWrite (E2,b);
digitalWrite(M2,HIGH);
}
void halt (char a, char b)
{
analogWrite (E1,a);
digitalWrite(M1, LOW);
analogWrite (E2,b);
digitalWrite(M2, LOW);
}

I tried to verify the sketch but i get these errors;

servo_with_keyboard_control.ino: In function ‘void loop()’:
servo_with_keyboard_control:114: error: a function-definition is not allowed here before ‘{’ token
servo_with_keyboard_control:119: error: a function-definition is not allowed here before ‘{’ token
servo_with_keyboard_control:125: error: a function-definition is not allowed here before ‘{’ token
servo_with_keyboard_control:132: error: a function-definition is not allowed here before ‘{’ token
servo_with_keyboard_control:139: error: a function-definition is not allowed here before ‘{’ token
servo_with_keyboard_control:146: error: a function-definition is not allowed here before ‘{’ token
servo_with_keyboard_control:151: error: expected `}’ at end of input

Any ideas? Thank you for your time. I am new to the programming and to using an arduino.

Ok, after finding where i was missing the brackets i uploaded the sketch onto my Arduino Uno which has a H-bridge with 2 motors hooked up and the servo. If I upload the corrected sketch i can control the motors but the Servo will not turn. It has power and everything is correct in my wiring. I uploaded a sketch that is specific for servo control with this setup and the servo works perfectly. Any ideas would be helpful, Thank you!

You are either missing brackets, have too many or they are in the wrong place. Before you start combining code, it’s important to take some time to learn Arduino - it’s one of the easier programming languages, but is still not “plug and play”.

It’s best to use the servo library - in order for a servo to maintain its position, it needs to receive a position signal about every 20-30ms. You can see in the code that if there are any delays which are longer than this, the servo will not get the position command and therefore not work properly (which often happens when combining two different code). Next, check to see if the servo pin is already used by one of the H-bridge pins. Last, check to see that the servo is connected to V, GND and a digital pin. If in doubt, include some images of your setup (double check) and if we can’t find any issues there, the code itself.

Thank you so much. I found where i was missing them. Right before void stop (void).