Go Down

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

alexhb

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
 }
 }
 else
 {
 digitalWrite(ledPin, LOW);
 myservo.write(pos);
 delay(15);
 }
}

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."

PaulS

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).

Zybex

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

alexhb

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."

PaulS

Quote
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.

alexhb

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."

PaulS

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. :)

alexhb

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."

PaulS

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

Go Up