...everything I could think of. Now I need to ask Arduino world.
Hi all, i have a servo project that should respond to three conditions. Here is a picture of my circuit:
and code:
//This program establishes 3 servo position for 3 input conditions.
//1. 45deg when switch 1 is on and switch 2 is off.
//2. 180deg when switch 1 is off and switch 2 is on.
//3. 90deg when switch 1 and switch 2 are off.
#include <Servo.h> //call up servo function from Arduino library
Servo motorSlave; //define servo motor
int servoPin = 9; //define servo signal pin
int switch1 = 8; //switch 1 in pin 8
int switch2 = 7; //switch 2 in pin 7
int switch1_value; //define input 1 condition
int switch2_value; //define input 2 condition
int servoPos1 = 0; //define servo output
int servoPos2 = 180;
int servoPos3 = 90;
void setup() {
pinMode(switch1, INPUT); //switch 1 is an input
pinMode(switch2, INPUT); //switch 2 is an input
motorSlave.attach(servoPin, 645, 1575); //servo attached through servoPin
}
void loop() {
switch1_value = digitalRead(switch1); //read switch 1
switch2_value = digitalRead(switch2); //read switch 2
if (switch1_value == HIGH && switch2_value == LOW) { //condition 1
motorSlave.write(servoPos1);
}
if (switch1_value == LOW && switch2_value == HIGH) { //condition 2
motorSlave.write(servoPos2);
}
if (switch1_value == LOW && switch2_value == LOW) { //condition 3
motorSlave.write(servoPos3);
}
}
The servo violently gyrates in condition three. I really have tried hundreds of possible solutions with this thing. The only thing that will cause the servo to go to 90 is connecting the ground to my servo control line.
Alternatively the command 'while' works for the third condition but it wont recognize the other two conditions after words.
Can anyone tell what is happening here? Thank you!!