#include int minPulse1 = 0; // minimum servo position int maxPulse1 = 180; // maximum servo position int turnRate1 = 1; // servo turn rate increment (larger value, faster rate) int minPulse2 = 0; // minimum servo position int maxPulse2 = 130; // maximum servo position int turnRate2 = 1; // servo turn rate increment (larger value, faster rate) int buttonPin = 13; // pin that the trigger will be connected to int centerServo1; int centerServo2; int pulseWidth1; // servo pulse width int pulseWidth2; // servo pulse width Servo servo1; Servo servo2; void setup() { pinMode(buttonPin, OUTPUT); servo1.attach(9); servo2.attach(10); centerServo1 = maxPulse1 - ((maxPulse1 - minPulse1)/2); centerServo2 = maxPulse2 - ((maxPulse2 - minPulse2)/2); pulseWidth1 = centerServo1; pulseWidth2 = centerServo2; Serial.begin(9600); // opens serial port, sets data rate to 9600 bps Serial.println(" Arduino Serial Servo Control"); Serial.println("Press a, s, d, or w to move, spacebar to center, and f to fire"); Serial.println(); } void loop() { // check for serial input if (Serial.available() > 0) { int data = Serial.read(); // read the incoming byte: digitalWrite(buttonPin, LOW); // turn the pin off on any incoming data switch(data) { case 'd' : pulseWidth1 = pulseWidth1 - turnRate1; break; case 'q' : pulseWidth1 = pulseWidth1 + turnRate1; break ; case ' ' : pulseWidth1 = pulseWidth2 = centerServo1; break; case 's' : pulseWidth2 = pulseWidth2 - turnRate1; break; case 'z' : pulseWidth2 = pulseWidth2 + turnRate1; break ; case 'f' : digitalWrite(buttonPin, HIGH); break; } // stop servo pulse at min and max if (pulseWidth1 > maxPulse1) { pulseWidth1 = maxPulse1; } if (pulseWidth1 < minPulse1) { pulseWidth1 = minPulse1; } // stop servo pulse at min and max if (pulseWidth2 > maxPulse2) { pulseWidth2 = maxPulse2; } if (pulseWidth2 < minPulse2) { pulseWidth2 = minPulse2; } servo1.write(pulseWidth1); servo2.write(pulseWidth2); // print pulseWidth back to the Serial Monitor (uncomment to debug) Serial.print("Servo 1: "); Serial.print(pulseWidth1); Serial.print(" Servo 2: "); Serial.print(pulseWidth2); Serial.println("degrees"); } }