Controlling an RC Servo motor with an Arduino and two Momentary Switches

Step 3The Program/Sketch

The Program/Sketch
Here's my code that I used. I might explain it later, I'm kind of lazy. Thats what this and this are for.


#include <Servo.h>

Servo myservo;
int button7=0;
int button6=0;
int pos=90;

void setup()
{
pinMode(7, INPUT);
pinMode(6, INPUT);
myservo.attach(9);
}

void loop()
{
button7=digitalRead(7);
button6=digitalRead(6);

myservo.write(pos);

delay(5);

pos=constrain(pos,0,180);

if(button7==1 && button6==0)
{
pos++;
}

if(button7==0 && button6==1)
{
pos--;
}
}

Any bugs, glitches? I don't notice any...
« Previous StepDownload PDFView All StepsNext Step »
1 comment
Jul 10, 2010. 9:27 PMMichael_oz says:
The Constrain should either be directly before the write, or after the if.

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
4
Followers
2
Author:geeklord