#include <Servo.h>
Servo myservo;
int pos = 0;
int count = 0; // might need later
int runXTimes = 0;
#define S1 2
#define S2 3
#define S3 4
#define S4 5
int s1state = HIGH;
int s2state = HIGH;
int s3state = HIGH;
int s4state = HIGH;
void setup() {
myservo.attach(9);
pinMode(S1, INPUT_PULLUP);
pinMode(S2, INPUT_PULLUP);
pinMode(S3, INPUT_PULLUP);
pinMode(S4, INPUT_PULLUP);
pinMode(9, OUTPUT);
}
void loop() {
s1state = digitalRead(S1);
s2state = digitalRead(S2);
s3state = digitalRead(S3);
s4state = digitalRead(S4);
// Servo control through Switch 1
if (s1state == LOW) { // read the input pin
for (int runXTimes = 0; runXTimes < 5; runXTimes++) { // Servo to sweep for 5 times
for (pos = 0; pos <= 80; pos += 1) // goes from 0 degrees to 80 degrees in steps of degree
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5);
}
for(pos = 80; pos>=0; pos-=1) // goes from 80 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5);
}
}
}
}
// Servo control through Switch 2. Used servo detach here
else if (s2state == LOW) { // read the input pin
for (int runXTimes = 0; runXTimes < 10; runXTimes++) { // Servo to sweep for 10 times
for (pos = 0; pos <= 80; pos += 1) // goes from 0 degrees to 80 degrees in steps of degree
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5);
}
for(pos = 80; pos>=0; pos-=1) // goes from 80 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5);
}
}
myservo.detach();
}
// Servo control through Switch 3
else if (s3state == LOW) { // read the input pin
for (pos = 0; pos <= 80; pos += 1) // goes from 0 degrees to 80 degrees in steps of degree
{ //
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5);
}
for(pos = 80; pos>=0; pos-=1) // goes from 80 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5);
}
}
// Servo control through Switch 4
else if (s4state == LOW) { // read the input pin
for (pos = 0; pos <= 80; pos += 1) // goes from 0 degrees to 80 degrees in steps of degree
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5);
}
for(pos = 80; pos>=0; pos-=1) // goes from 80 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5);
}
}
}
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user