servo loop issue

Hi fellow Programmers,

I just ran into a problem, I cannot solve. Spent hours to figure it out, but cannot see my mistake. (it must be a noob mistake…)
The situation:

  • I have 2 servos. When i run the program, i want to move one of the servos every 1 second, and in the meantime, i want to be able to re-calibrate both servos. Then it should continue to move every 1 seconds, from the angle I set manually.

It runs okay, but when I move the HOR (horizontal) servo, it moves 10 degrees (as wished), but then it moves back.

example:

90 degrees
91 degrees
92 degrees
93 degrees
i push the remote, it jumps to 83 degrees
then it jumps back to 94
95
96

instead of continuing from 83, 84, 85…
I dont know why.

Here is my full code:

#include <IRremote.h>
#include <IRremoteInt.h>
#include <Servo.h>

Servo myservoHOR;
Servo myservoVER;

// int posHOR = 90;
int posVER = 90;
int posHORstart = 90;
int posVERstart = 0;
int receiver = 2;
String newIR = "";


IRrecv irrecv(receiver);
decode_results results;

void setup()   {
  Serial.begin(9600);
  Serial.println("serial monitor works");
  irrecv.enableIRIn();
  myservoHOR.attach(13);
  myservoVER.attach(9);
  myservoHOR.write(posHORstart);
  myservoVER.write(posVERstart);
}

void loop()
{

int posHOR = myservoHOR.read();
Serial.println("loop kezdo poshor");
Serial.println(posHOR);
  
  if (irrecv.decode(&results))
  {
    newIR = String(results.value);

Serial.println("varom a parancsot");

    if (newIR == "16720605")
    {

      Serial.println("jobbra-balra  BAL");
      myservoHOR.write(posHOR + 10);
      int posHOR = posHOR  + 10;
      Serial.println("uj HOR ertek:");
      Serial.println(posHOR);
    }
    if (newIR == "16761405")
    {

      Serial.println("jobbra-balra JOBB");
      myservoHOR.write(posHOR - 10);
         int posHOR = posHOR  - 10;
      Serial.println("uj HOR ertek:");
      Serial.println(posHOR);
    }

    if (newIR == "16736925")
    {

      Serial.println("fel-le FEL");
      myservoVER.write(posVER + 10);
         int posVER = posVER  + 10;
      Serial.println("uj VER ertek:");
      Serial.println(posVER);
    }
    if (newIR == "16754775")
    {

      Serial.println("fel-le LE");
      myservoVER.write(posVER - 10);
          int posVER = posVER  - 10;
      Serial.println("uj VER ertek:");
      Serial.println(posVER);
    }

    Serial.println(results.value, HEX);
    Serial.println("this button was pressed");
    Serial.println(newIR);

    delay(1500);
    irrecv.resume();
    


  }
// int posHOR = myservoHOR.read();
//mozgassuk kicsit a motort magatol
// 240000 ms alatt fokot kell mozognia
      Serial.println("kovessuk a napot");
      myservoHOR.write(posHOR + 1);
      int posHORuj = myservoHOR.read();
      Serial.println("1 fokkal kovetem. Az uj HOR pozi:");
      Serial.println(posHORuj);
      Serial.println("---");
      Serial.println("---");
  
      delay(1000);


  
}

Any help would be greatly appreciated. Thanks!

  posHOR = posHOR  + 10;  // instead int posHOR = posHOR  + 10;
  posHOR = posHOR  - 10;  // instead int posHOR = posHOR  - 10;
  posVER = posVER  - 10;  // instead int posVER = posVER  - 10;
  posVER = posVER  + 10;  // instead int posVER = posVER  + 10;

You were hiding the variable with a new definition.

Whandall:   posHOR = posHOR  + 10;  // instead int posHOR = posHOR  + 10;   posHOR = posHOR  - 10;  // instead int posHOR = posHOR  - 10;   posVER = posVER  - 10;  // instead int posVER = posVER  - 10;   posVER = posVER  + 10;  // instead int posVER = posVER  + 10;

You were hiding the variable with a new definition.

Thanks a lot Whandall! i told you its a noob mistake :) it works now perfectly.