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?