Basic servo project problem

Hi all i’m new so i’m at this so sorry if this ends up to be a dumb question.

I’m so confused !

I have some really basic code that moves a servo one way and then moves it back the other way.

I have a global variable to control how many times it performs this, the problem is that i cant seem to update my “cycles” variable after it has been declared.

The really weird bit is if i remove the servo deceleration then my variable gets updated just fine.

someone please put me out of my misery :slight_smile:

here is the code (don’t laugh) :slight_smile:

// Servo - Version: Latest 
#include <Servo.h>

Servo servo_1;
    
int pos = 0;
int cycles = 2;

void setup() {
    servo_1.attach(2);
    //servo_1.write(90);
}

void loop() {
  //go forward
  if(pos == 0 && cycles >0 ){
    for (pos = 1; pos<= 180; pos++){ 
    servo_1.write(pos);
    delay(10);
  }
  }
  //go backward
  if(pos = 180 && cycles >0){
    for (pos = 180; pos>= 1; pos--){ 
    servo_1.write(pos);
    delay(10);
  }
  }
  cycles = cycles -1;
}
  if (pos = 180 && cycles > 0)

Whoops !

Well....that's frustrating !

Thanks dude :slight_smile: