My servo wont turn when accepting commands from an arduino

arduinocontrolservo

I'm trying to turn a servo with my arduino when it receives commands from a serial input, in this case my keyboard. I have already done this with motors and it works fine but when I try with my servo it doesn't move at all. So far I have this written

#include <SoftwareServo.h>

SoftwareServo myservo;
int movemotor;

void setup() {  
  Serial.begin(9600);
  myservo.attach (10);
  myservo.write(90);
}

void loop() {
  movemotor = Serial.read();

  if (movemotor = 111) {
     for (int pos = 90; pos >=0; pos--) {
       myservo.write(pos);
       delay(15);
    }
  }

  else if (movemotor = 99) {
     for (int pos = 90; pos <= 180; pos++) {
       myservo.write(pos);
       delay(15);
    }
  }
}

I'm powering the servo externally with a 9 volt battery and have made sure to connect its ground to that of the arduino but can't see anything else I did wrong. Any help would be greatly appreciated.

Best Answer

Have you tried going through an Arduino servo example/tutorial? The linked example shows one thing you are missing: a call to refresh()

refresh() You must call this at least once every 50ms to keep the servos updated. You can call it as often as you like, it won't fire more than once every 20ms. When it does fire, it will take from .5 to 2.5 milliseconds to complete, but won't disable interrupts.

Edit: as TheTerribleSwiftTomato mentioned, you are not checking the value of movemotor but setting it. Because of this, the first if statement should always be true as any non-zero value will evaluate to true. You can avoid these assignment condition errors by adopting the Yoda condition style. It looks a bit funny, but helps the compiler catch errors for you.