1
\$\begingroup\$

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.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Could you add a photo of your setup? Also, this is probably unrelated, but "movemotor = 111" means "assign value of 111 to the variable movemotor", and evaluates as true. You were probably aiming for "movemotor == 111". Same goes for the second if. \$\endgroup\$ – mikołak Mar 29 '12 at 20:43
  • \$\begingroup\$ Look at the output signal. It should be a pulse from 1 to 2 ms in length repeating every 20-50 ms. \$\endgroup\$ – Olin Lathrop Mar 29 '12 at 21:08
1
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ Hmmm... the Servo library that I use (from the bog-standard Arduino IDE, looks to be the most recent version) doesn't require this call. In any case, the inquirer seems to use a different servomotor lib. \$\endgroup\$ – mikołak Mar 29 '12 at 21:06
  • \$\begingroup\$ ...which does indeed need a call to refresh() . My bad. \$\endgroup\$ – mikołak Mar 29 '12 at 21:12
  • \$\begingroup\$ Thanks for the help on this, I had initially included the call to refresh but took it out while playing around with the code and forgot to put it back in. I think the main problem was that I was setting the value to move motor rather than checking it. \$\endgroup\$ – user1291703 Mar 30 '12 at 3:27
0
\$\begingroup\$

Try this code. You might also want to check that the pins you are attaching to the arduino and servo are correct; eg, voltage, ground, and pulse.

// Sweep
// by BARRAGAN <http://barraganstudio.com> 
// This example code is in the public domain.


#include <Servo.h> 

Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 

int pos = 0;    // variable to store the servo position 

void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
} 


void loop() 
{ 
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
} 
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.