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