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.