Servo defaults to 90 degrees every time program uploads

Hi,

I have a program that I’m working on modifying and everything works how I want it to except for one thing - every time I upload the program or hit the reset button the servo I’m controlling immediately defaults to 90 degrees.

I don’t see anything in the program that would cause this to happen, but I am fairly new to Arduino so it’s very possible that I’m missing something.

I’ve included the code for the program below.

If anyone can tell me why this is happening and how to fix it I would greatly appreciate it.

Thanks.

#include <Servo.h>

Servo myservo;  

int angle = myservo.read();    // initial angle  for servo
int angleStep =5;


#define UP 12   
#define DOWN 2  

void setup() {
  Serial.begin(9600);          //  setup serial
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  pinMode(UP,INPUT_PULLUP); // assign pin 12 as input for UP button
  pinMode(DOWN,INPUT_PULLUP);// assign pin 2 as input for DOWN button
  Serial.println("Servo Button");
}


void loop() {
 
  
  while(digitalRead(DOWN) == LOW){

    if (angle > 0 && angle <= 180) {
      angle = angle - angleStep;
       if(angle < 0){
        angle = 0;
       }else{
      myservo.write(angle); // move the servo to desired angle
       }
    }
    
  delay(100);
  }
 

  while(digitalRead(UP) == LOW){

    if (angle >= 0 && angle <= 180) {
      angle = angle + angleStep;
      if(angle >180){
        angle =180;
       }else{
      myservo.write(angle); // move the servo to desired angle
       }
    }
    
  delay(100); // waits for the servo to get there
  }// 

  
}

If anyone can tell me why this is happening and how to fix it I would greatly appreciate it.

It is standard behaviour for the Servo library. If you do a write() before the attach() you can set your own default angle

That’s the default start position for the library.
The Arduino doesn’t know where the servo is (there’s no position encoder and feedback) so it has to start some where.
You can write a different value at startup, or devise some way to save the last position to EEPOM (might be a problem if you write to EEPOM too often and destroy it - e.g. after every step).

Servo myservo; 
int angle = myservo.read();    // initial angle  for servo

This will always set 'angle' to 90, the default position when the Servo object is created.

If you want to measure the ACTUAL position of the servo you need a servo with a position output. You might be able to modify a servo yourself by opening it and connecting a wire to the wiper of the position feedback potentiometer. Connect that to an analog input and, by experient, figure out what analogRead() value correlates with each angle.

That's the normal default that Servo sets when you do the attach(). But when you say you want to "fix it" what did you want the servo to do instead?

Steve

Hi,
Hint to make your code more readable;

#define UP 12   
#define DOWN 2

Is better defined as

#define upPin 12   
#define downPin 2

That way the Pin variable is recognized throughout your code as a pin definition.

Tom... :slight_smile:

Thank you all for your replies and advice.