Servo starts at wrong position

Lanyard machine.pdf (7.1 KB)

Why does my servo go to 110+ degrees instead of the int pos = 10? It woks fine after it gets going but will not go to pos 10 at the beginning.

#include <Stepper.h>
#include <Servo.h>

const byte  buttonPin = 5; // the pin to which the pushbutton is attached
bool runStepper = false;
bool endCycle = false;
bool betweenCycleTiming = false;
unsigned long betweenCycleStartTime;
const unsigned long betweenCycleDelay = 3000;

const int stepsPerRevolution = 575;  // steps to make 13 inch paracord lanyard

Servo myservo;  // create servo object to control a servo

int pos = 10;    // starting position is not top dead center

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {

  Serial.begin(115200);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(13, OUTPUT);
  // set the speed at 30 rpm:
  myStepper.setSpeed(30);

  myservo.attach(7);  // attaches the servo on pin 7 to the servo object
  
}


void loop() {

  checkButton();
  
  if (runStepper == true and betweenCycleTiming == false)
  {
    Serial.println("running");
    myStepper.step(-stepsPerRevolution);
    delay(100);

    for (pos = 50; pos <= 110; pos += 1) { // goes from 50 degrees to 110 degrees
      // in steps of 1 degree
      myservo.write(pos);              // tell servo to go to position in variable 'pos'
      delay(100);                       // waits 10ms for the servo to reach the position
    }
    for (pos = 110; pos >= 50; pos -= 1) { // goes from 110 degrees to 50 degrees
      myservo.write(pos);              // tell servo to go to position in variable 'pos'
      delay(5);                       // waits 5ms for the servo to reach the position
    }
    endCycle = true;
    Serial.println("end cycle");
  }
  if (endCycle == true)//pick up betweenCycleDelay start and enable timing
  {
    betweenCycleTiming = true;
    betweenCycleStartTime = millis();
    endCycle = false;
    digitalWrite(13, HIGH);//indicate start of 3 second period
  }

  if (betweenCycleTiming == true)//allow for button read between cycles
  {
    checkButton();

    //restart aftercooling delay if button not pressed to stop cycle
    if (millis() - betweenCycleStartTime >= betweenCycleDelay)
    {
      betweenCycleTiming = false;
      digitalWrite(13, LOW);
    }
  }
}

void checkButton()
{
  const byte debouncePeriod = 50;
  static byte oldButtonState = HIGH;

  byte buttonState = digitalRead(buttonPin); //read push button state

  if ((buttonState == LOW) && (oldButtonState == HIGH))  // check is button pressed and is changed using logic for input pullup
  {
    delay(debouncePeriod); //blocking debounce routine using delay
    buttonState = digitalRead(buttonPin); //read button again
    if ( buttonState == LOW) //still LOW
    {
      if (runStepper == false)
      {
        Serial.println("run");
        runStepper = true;
      }
      else
      {
        Serial.println("stop");
        runStepper = false;
      }
    }
  }
  oldButtonState = buttonState;
}

Where is the code to make it go to that position at the beginning? I can't see it ...

Is this not it? If not, where do I put it?

This is just setting a value to a variable and nothing more. Nowhere do you have a command to position the servo in that desired position. If they want only at the beginning when starting the program in setup after myservo.attach(7) add myservo.write(pos).

You need to understand the difference between the global space outside functions, setup and loop functions. Also the steps you take to move your stepper and the order they must be in. When something unexpected happens you look at the bit you think should do x and realise it is not doing x. Then you work down through your code and see it is doing Y. Obviously your code for Y works and x does not. If you want x to work replicate Y with x.

As above
Int pos=10;
Just declares a variable of type int, names it pos and initialises it to 10
You don’t do anything with this variable

The default position for a Servo is 90. If you want it to go to a position other than 90 when you take control, set the position BEFORE the .attach():

  myservo.write(pos);
  myservo.attach(7);  // attaches the servo on pin 7 to the servo object

This worked. Thank you