THX for the additional information.
I meant that the code sections handling the two axes are similar, but the placement of the delay() calls that make them work is different one to the other.
I would have thought that the two axes are identical, that is to say that the code to handle them would be identical but for the variables used and the axis being handled.
That you arrived at two different and (so far) odd fixes interested me. Is there, in fact, any real difference between the two axes that would require different handling?
I ran your code and it does seem to work, aside from issuing multiple STOP commands, STOP when it is already. Stopped. No large deal.
I do not argue with success, this odd line
else if ( (restX - deadZoneX) < currentJoystickX < (restX + deadZoneX) ) {
somehow seems to be working - whatever it does do is passing for what you think it is doing. I haven't just now what it takes to see why that is. Perhaps I will figure that out.
The code below would replace your loop() function. It only handles one axis. It uses hysteresis and a simple finite state machine to track the joystick movement and issue commands to the pan/tilt as necessary. Your code does the same things! I only offer this as a simple demonstration of a more explicit FSM approach:
void loop() {
doXJoyStick();
}
void doXJoyStick()
{
enum {IDLE, LEFT, RIGHT, STOP};
static unsigned char state = IDLE;
int joyX = analogRead(A0) - 512; // normalize to -511..511 range
switch (state) {
case IDLE :
if (joyX < -200) {
Serial.println("PAN Right");
Serial.println(joyX);
mySerial.print("#AMMF0000W\r\n"); // PAN Up
mySerial.flush();
state = LEFT;
}
else if (joyX > 200) {
Serial.println("PAN Left");
Serial.println(joyX);
mySerial.print("#AMMB0000W\r\n"); // PAN Down
mySerial.flush();
state = RIGHT;
}
break;
case LEFT :
if (joyX > -100)
state = STOP;
break;
case RIGHT :
if (joyX < 100)
state = STOP;
break;
case STOP :
Serial.println("PAN Stop");
Serial.println(Xval);
mySerial.print("#AMST0000W\r\n"); // PAN Stop
mySerial.flush();
state = IDLE;
break;
}
}
The other axis could be handled with a cut/paste/edit copy of the same code, or a real programmer could figure out a way for the two code sections, nearly identical, could be generalized to serve both axes.
Try it here (X-axis only).
a7