I've run into a problem...
I'm driving two servos based on the input from one joystick. While I'm working on building the maths I need to calculate the bearing indicated on the joystick to translate to where to send the servos, I've settled on the eight basic compass points, and fuzzy logic to determine where the joystick is pointing to. Code at the bottom. The issue I'm having is that after my servos initialize, respond to commands, and the loop ends, the servos (ServoA more than ServoB) twitch as the next loop starts.
The servos are not powered by the Arduino Uno r3. While it might be a power based problem I believe I have ruled that out as if I change the value of the delay at the end of the loop, the delay between twitches changes accordingly.
Note: ServoB is rotated 180 in its housing, so that it's 180-degree rotational range allows the pair of servos to cover a circular area.
#include <SoftwareSerial.h>
#include <Servo.h>
SoftwareSerial lcd(2,6);
Servo Alpha;
Servo Bravo;
int ledPin = 13;
int joyPin1 = 0;
int joyPin2 = 5;
int value1 = 0;
int value2 = 0;
int value3 = 0;
int value4 = 0;
int AlphaLast = 0;
int BravoLast = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
pinMode(11, OUTPUT);
Serial.begin(9600);
Alpha.attach(9);
Bravo.attach(11);
lcd.begin(9600);
clearDisplay();
setLCDCursor(2);
lcd.print("Hello, world");
setLCDCursor(16);
lcd.print(" Running Monitor");
}
void loop() {
value1 = analogRead(joyPin1);
delay(100);
value2 = analogRead(joyPin2);
Serial.print("J: X = ");
Serial.print(value1);
Serial.print(" Y = ");
Serial.print(value2);
clearDisplay();
lcd.print("X: ");
lcd.print(value1);
setLCDCursor(7);
lcd.print("SvA: ");
lcd.print(AlphaLast);
setLCDCursor(16);
lcd.print("Y: ");
lcd.print(value2);
setLCDCursor(23);
lcd.print("SvB: ");
lcd.print(BravoLast);
value3 = 0;
if (value1 < 350 && (value2 > 350 && value2 < 650)) {
Serial.println(" W");
value4 = 7;
value3++;
}
if ((value3 == 0) && (value1 < 350 && value2 < 650)) {
Serial.println(" NW");
value4 = 8;
}
if ((value3 == 0) && (value1 < 350 && value2 > 350)) {
Serial.println(" SW");
value4 = 6;
}
if ((value1 > 650) && (value2 > 350 && value2 < 650)) {
Serial.println(" E");
value4 = 3;
value3++;
}
if ((value3 == 0) && (value1 > 650 && value2 < 650)) {
Serial.println(" NE");
value4 = 2;
}
if ((value3 == 0) && (value1 > 650 && value2 > 350)) {
Serial.println(" SE");
value4 = 4;
}
if (value2 > 650 && (value1 > 350 && value1 < 650)) {
Serial.println(" S");
value4 = 5;
}
if (value2 < 350 && (value1 > 350 && value1 < 650)) {
Serial.println(" N");
value4 = 1;
}
if ((value1 > 350 && value1 < 650) && (value2 > 350 && value2 < 650)) {
Serial.println(" Center");
value4 = 0;
}
switch (value4) {
case 0:
Serial.println("Neither Servo Moves");
break;
case 1:
Serial.println("Both Servos moves toward North");
Alpha.write(0);
AlphaLast = 0;
delay(15);
Bravo.write(180);
BravoLast = 180;
delay(15);
break;
case 2:
Serial.println("Only Right Servo moves towards NorthEast");
Bravo.write(135);
BravoLast = 135;
delay(15);
break;
case 3:
Serial.println("Only Right Servo moves towards East");
Bravo.write(90);
BravoLast = 90;
delay(15);
break;
case 4:
Serial.println("Only Right Servo moves towards SouthEast");
Bravo.write(45);
BravoLast = 45;
delay(15);
break;
case 5:
Serial.println("Both Servos move toward South");
Alpha.write(180);
AlphaLast = 180;
delay(15);
Bravo.write(0);
BravoLast = 0;
delay(15);
break;
case 6:
Serial.println("Only Left Servo moves towards SouthWest");
Alpha.write(45);
AlphaLast = 45;
delay(15);
break;
case 7:
Serial.println("Only Left Servo moves towards West");
Alpha.write(90);
AlphaLast = 90;
delay(15);
break;
case 8:
Serial.println("Only Left Servo moves towards NorthWest");
Alpha.write(135);
AlphaLast = 135;
delay(15);
break;
default:
Serial.println("No Instruction Received");
}
delay(500);
}
void clearDisplay()
{
lcd.write(0xFE); // send the special command
lcd.write(0x01); // send the clear screen command
}
void setLCDCursor(byte cursor_position)
{
lcd.write(0xFE); // send the special command
lcd.write(0x80); // send the set cursor command
lcd.write(cursor_position); // send the cursor position
}
Moderator edit: code tags added