Pages: [1]   Go Down
Author Topic: Servo code question  (Read 1261 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 2
Posts: 96
There are two ways to write error-free programs; only the third one works.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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?
Logged

"#define QUESTION ((bb) || !(bb))  - Shakespeare."

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 548
Posts: 46003
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Jr. Member
**
Karma: 2
Posts: 96
There are two ways to write error-free programs; only the third one works.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

power's from the arduino, and that's all good smiley-wink 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
Logged

"#define QUESTION ((bb) || !(bb))  - Shakespeare."

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 548
Posts: 46003
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Jr. Member
**
Karma: 2
Posts: 96
There are two ways to write error-free programs; only the third one works.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yes, it is uselessly small. smiley-razz it is a 9g tiny little thing. is that too big to run on arduino?
Logged

"#define QUESTION ((bb) || !(bb))  - Shakespeare."

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 548
Posts: 46003
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. smiley
Logged

0
Offline Offline
Jr. Member
**
Karma: 2
Posts: 96
There are two ways to write error-free programs; only the third one works.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

"#define QUESTION ((bb) || !(bb))  - Shakespeare."

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 548
Posts: 46003
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: