For a project, I’m planning on having five servos that are all controlled by their own button. So far, I have written code for the servos to sweep continuously. I’ve never used buttons before and I’ve included an attachment of what I have so far. I’m trying to (if it’s possible) have the servos turn 180 degrees when the button is pressed and turn back to the original position when the button is pressed again. Thanks so much for any help!
String readString;
#include <Servo.h>
Servo myservoa, myservob, myservoc, myservod, myservoe;
int pos = 0;
void setup() {
myservoa.attach(5);
myservob.attach(4);
myservoc.attach(3);
myservod.attach(2);
myservoe.attach(1);
}
void loop(){
for (pos = 0; pos <= 180; pos += 1)
{
myservoa.write(pos);
myservob.write(pos);
myservoc.write(pos);
myservod.write(pos);
myservoe.write(pos);
delay(20)
;}
for(pos = 180; pos>= 0; pos-= 1)
{
myservoa.write(pos);
myservob.write(pos);
myservoc.write(pos);
myservod.write(pos);
myservoe.write(pos);
delay(20);
}
}
sweep.ino (695 Bytes)