int servoStart = 70; //servo minimum starting position
int servoStop = 110; //servo maximum stopping position
int servoSW1 = A1; //servo in straight position
int servoSW2 = A2; //servo in thrown position
int val1; //1023 if switch is straight
int val2; //0 if switch is thrown
int greenLED = 2; //pin green LED is attached to
int redLED = 3; //pin red LED is attached to
String msg1; //declare msg1 variable for displaying messages
const int button = A0; //value from pushbutton
#include <Servo.h>
Servo myservo[1]; //create servo object
void setup() {
Serial.begin(9600); //starts serial monitor
myservo[0].attach(4); //sets servo control to digital pin 6
pinMode(greenLED,OUTPUT); //Green LED
pinMode(redLED,OUTPUT); //Red LED
pinMode(button, INPUT);
pinMode(servoSW1, INPUT);
pinMode(servoSW2, INPUT);
//the following for debugging
delay(100);
val1 = analogRead(servoSW1);
val2 = analogRead(servoSW2);
Serial.println("SW1="+String(val1)+" SW2="+String(val2));
if (val1) {
myservo[0].write(servoStart);
}
else {
myservo[0].write(servoStop);
}
}
void loop() {
while(!analogRead(button));
//Serial.println("switchState="+ String(switchState)+" buttonRead="+ String(buttonRead)+" servoPos=" + String(servoPos) + " button=" + String(button));
delay(200);
if (val1) { //HIGH if switch is straight
digitalWrite(greenLED,HIGH);
digitalWrite(redLED,LOW);
int switchState = HIGH;
}
else { //LOW if switch is thrown
digitalWrite(greenLED,LOW);
digitalWrite(redLED,HIGH);
int switchState = LOW;
}
}