Go Down

Topic: Servo code question (Read 1 time) previous topic - next topic


I've been working on making an arduino-based "most useless machine ever". It's not the best way you can do it, but it is helping me to learn some servo coding and stuff. Here's my current code:
Code: [Select]
#include <Servo.h>               //include servo library
Servo myservo;                   //create servo object
int pos = 10;                     //variable for servo position
int start =10;
const int togglePin = 2;         //toggle switch pin 2
const int ledPin =  13;          //diagnostic LED pin 13

int toggleState = 0;             //variable for reading toggle switch state

void setup()
 pinMode(ledPin, OUTPUT);       //labels LED pin as output
 pinMode(togglePin, INPUT);     //labels toggle pin as input
 myservo.attach(9);             //attaches servo to pin 9

void loop()
 toggleState = digitalRead(togglePin);
 if (toggleState == HIGH)
 digitalWrite(ledPin, HIGH);
 for(pos = 10; pos <=80; pos+= 5)
   myservo.write(start);              // tell servo to go to position in variable 'pos'
   delay(15);                       // waits 15ms for the servo to reach the position
 digitalWrite(ledPin, LOW);

can anyone spot any extra crud in there? I need to adjust the position numbers for my particular box size and servo configuration, but even still it's acting slow and jittery. Anything in the code that would make this happen?
"#define QUESTION ((bb) || !(bb))  - Shakespeare."


What is acting slow and jittery? The servo? If that's the case, you need to say something about how the servo is powered.

If it's the reaction to the button, the problem is with the delay statements in the for loops.

Keep in mind that the else part of the loop will execute on every pass through loop when the button is not pressed.

You might want to look at the Button library, and only actually move the servo when the button state changes (released to pressed, move one way; pressed to released, move the other way).


Try commenting out that for loop and replacing it with just myservo.write(80);
I think the servo library handles the pulses and stepping.


power's from the arduino, and that's all good ;) I think I fixed part of it by editing delays, I'm going to try removing the for() loop to see if that fixes it. Thanks
"#define QUESTION ((bb) || !(bb))  - Shakespeare."


power's from the arduino, and that's all good

No, it's not. Unless it is incredibly tiny (to the point of uselessly small), it draws more current than the Arduino can safely provide. You will, sooner or later, compromise the Arduino.


yes, it is uselessly small. :P it is a 9g tiny little thing. is that too big to run on arduino?
"#define QUESTION ((bb) || !(bb))  - Shakespeare."


Model name? Model number? Link to data sheet? Facts, man! We need facts!

We don't need no steeenking badges, but we do need data. :)


it's a tower pro SG90 9g servo, plastic gears, I can't seem to find a datasheet right now, but they run on 3-7.2V
"#define QUESTION ((bb) || !(bb))  - Shakespeare."


I'd still try an external power source for the servo. Connect the servo power supplies ground to the Arduino ground, too.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131