loading

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

Step 3: The Program/Sketch

Picture of 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...
 
Michael_oz5 years ago
The Constrain should either be directly before the write, or after the if.