Integer dropping for no apparent reason.

Hi there.

This sketch controls a two servo “turret” by using an old Xbox controller stick.
I didn’t want the turret to return to centre once the stick is released, therefore created integers for the positions, to be written to the servos. (they are large integers as a method of controlling the speed, as i want to add multiple speeds of movement based on the amount the stick is pushed)

I have tested it with a single servo and pot, and it worked fine (i got it working at multiple speeds too)
and in the code below, the “vertical” movement works perfectly.
but the horizontal movement is playing silly beggers.

The serial output for the horizontal pot value is a nice central 550. The Value
releasing the stick returns it between 530 and 570, so it isnt a mechanical issue, and the program seems to be correct from where i am sitting.

The integer to define the servo position, (ServoPosVar) starts at 5000, as defined, and then plummets, constantly, irrelevant of stick input. The constrains applied to the servo hold it within its mechanical limits. A few moments of this, and it flips to the maximum value, pressumably as the integer reaches it limit and loops back up.

any ideas as to what i causing the integer to drop, even though the pot position is correct?

// Controlling a servo position using a potentiometer (variable resistor) 
// objective is to control the servo as a "continuous rotation servo"
// Create integer to represent servo position, then apply modifier using pot, then write to servo
// modified from "Knob" example

#include <VarSpeedServo.h> 

VarSpeedServo HoriServo;  // create servo object to control a servo 
VarSpeedServo VertServo;  // create servo object to control a servo 

int HoriPotPin = 1;  // analog pin used to connect the potentiometer
int VertPotPin = 2;  // analog pin used to connect the potentiometer
int HoriPotVal;    // variable to read the value from the analog pin
int VertPotVal;    // variable to read the value from the analog pin
int HoriPosVar = 5000;    // variable to be used for calculating position
int VertPosVar = 5000;    // variable to be used for calculating position
int HoriServPos;      // HoriPosVar scaled and constrained for servo use
int VertServPos;      // HoriPosVar scaled and constrained for servo use

void setup() 
{ 
  Serial.begin(9600);   // serial port output for debugging.
  HoriServo.attach(9);  // attaches the servo on pin 9 to the servo object 
  VertServo.attach(10); // attaches the servo on pin 10 to the servo object 
} 

void loop() 
{ 
  HoriPotVal = analogRead(HoriPotPin);            // reads the value of the potentiometer (value between 0 and 1023) 
  VertPotVal = analogRead(VertPotPin);            // reads the value of the potentiometer (value between 0 and 1023) 

  // thumbstick centre is approx 480, but leeway required for mechanical slack in thumbstick.
  // insufficient slack results in ongoing servo twitch. excess slack requires increased movement to register
  // look into servo twitch being possibly due to noisy power supply over usb??


  //Horizontal movement

  // decreases in postion variable depenant on thumbstick push
  if (analogRead(HoriPotVal) < 500) {
    HoriPosVar-=10;
  }
  // increases in postion variable depenant on thumsbtick push
  if (analogRead(HoriPotVal) > 600) {
    HoriPosVar+=10;
  }
  HoriServPos = map(HoriPosVar, 0, 10000, 0, 180); // maps the position variable (large number) to servo position (180) to provide slow movement when adding 1 unit per cycle
  HoriServPos =  constrain(HoriServPos,30 ,130);    // servo protecton
  HoriServo.write(HoriServPos),10;                  // sets the servo position according to the scaled value 



  //Vertical movement
  // decreases in postion variable depenant on thumbstick push
  if (analogRead(VertPotVal) < 450) {
    VertPosVar-=10;
  }

  // increases in postion variable depenant on thumsbtick push
  if (analogRead(VertPotVal) > 560) {
    VertPosVar+=10;
  }
  VertServPos = map(VertPosVar, 0, 10000, 0, 180); // maps the position variable (large number) to servo position (180) to provide slow movement when adding 1 unit per cycle
  VertServPos =  constrain(VertServPos,1 ,178);    // servo protecton
  VertServo.write(VertServPos),10;                  // sets the servo position according to the scaled value 

  //movement Ends
  delay(1);


  Serial.println(HoriPotVal);                   // serial port output for debugging. Replace value as required.

}
  if (analogRead(HoriPotVal) < 500) {

HoriPotVal is NOT a pin number.

  if (analogRead(VertPotVal) < 450) {

Neither is VertPotVal.

Bingo.

Thanks a lot.

I cant see why it wouldnt have worked, as it was just referencing the one from the other was it not?? but then again, it was totally redundant in this situation, i agree. So stripping it out has sorted it.

Edit:

 HoriPotVal = analogRead(HoriPotPin);            // reads the value of the potentiometer (value between 0 and 1023) 
  VertPotVal = analogRead(VertPotPin);            // reads the value of the potentiometer (value between 0 and 1023)

this is the part i was referencing it with i think? i put that in because i was going to try an centralise the pot value: ie, make central 0, rather than 500. but that was before i took the controller apart and found that they were just straight up normal pots rather than something more complex