I'm using a 3 way switch to control a servo motor. I can get it to change for CW to CCW when i change position on the switch. Do i need to add a ELSE statement to get it to stop on a predetermined angle ?
#include <Servo.h>
Servo myservo; // TO control a servo with a 3 way switch
// Use 3 way switch to control servo position.
// First position moves CW
// Second position moves CCW
int pos = 0; // variable to store the servo position and starting position
int buttonApin = 2;
int buttonBpin = 3;
bool buttonAstatus; // stores button state
bool buttonBstatus;
void setup()
{
Serial.begin(9600);
Serial.print("Running v2.0 of Servo controlled by a 3 way switch");
pinMode(buttonApin, INPUT_PULLUP);
pinMode(buttonBpin, INPUT_PULLUP);
buttonAstatus = digitalRead(buttonApin);
buttonBstatus = digitalRead(buttonBpin);
Serial.println(buttonAstatus);
Serial.println(buttonBstatus);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
buttonAstatus = digitalRead(buttonApin);
buttonBstatus = digitalRead(buttonBpin);
Serial.println(buttonAstatus);
Serial.println(buttonBstatus);
if (buttonAstatus == LOW)
{
myservo.write((digitalRead(2)) ? 0 : 180);
{
Serial.print(pos);
}
}
if (buttonBstatus == HIGH)
{
myservo.write((digitalRead(3)) ? 180 : 0);
{
Serial.print(pos);
}
}
}