I am trying to control a servo motor with a 3 way toggle switch. When I turn my switch to the right, I want the servo motor to turn to 180 degrees, when I turn it to the left I want it to be at 0degree and finally when it is in the neutral position I want it to be at 90 degrees.
Right now my code only works for left and middle position and I am not able to make work the right position.
Could someone help me plz
Here is my code for the moment :
#include <Servo.h>
// Pin numbers for the switches and servo
const int switchPin1 = 2;
const int switchPin2 = 3;
const int switchPin3 = 4;
const int servoPin = 9;
// Servo object
Servo servoMotor;
// Variables to track the switch state
int switchState1 = 0;
int switchState2 = 0;
int switchState3 = 0;
// Variables to store the servo positions
int servoPosition = 90;
void setup() {
// Initialize the servo and switch pins
servoMotor.attach(servoPin);
pinMode(switchPin1, INPUT_PULLUP);
pinMode(switchPin2, INPUT_PULLUP);
pinMode(switchPin3, INPUT_PULLUP);
// Set the initial servo position to 90 degrees
servoMotor.write(servoPosition);
}
void loop() {
// Read the current state of the switches
switchState1 = digitalRead(switchPin1);
switchState2 = digitalRead(switchPin2);
switchState3 = digitalRead(switchPin3);
// Check switch positions and control servo accordingly
if (switchState1 == LOW) {
// Switch 1 ON - Move servo to 0 degrees
//for (int pos = servoPosition; pos >= 0; pos--) {
servoMotor.write(0);
//delay(15);
//}
//servoPosition = 0;
} else if (switchState2 == LOW) {
// Switch 2 ON - Set servo to 90 degrees
servoMotor.write(90);
//servoPosition = 90;
} else {
// Switch 3 ON - Move servo to 180 degrees
//for (int pos = servoPosition; pos <= 180; pos++) {
servoMotor.write(180);
//delay(15);
//}
//servoPosition = 180;
}
delay(100); // Delay for stability
}
Please provide more details of the switch. For instance, does it have a common pin which is connected to one or other of the other 2 pins and is not connected to anything when in the centre position ? Or is there a different arrangement ?
void loop() {
// Read the current state of the switches
switchState1 = digitalRead(switchPin1);
switchState3 = digitalRead(switchPin3);
// Check switch positions and control servo accordingly
if (switchState1 == LOW) {
// Switch 1 ON - Move servo to 0 degrees
servoMotor.write(0);
} else if (switchState3 == LOW) {
// Switch 3 ON - Set servo to 180 degrees
servoMotor.write(180);
} else {
// Move servo to 90 degrees
servoMotor.write(90);
}
delay(100); // Delay for stability
}
Consider - keep in track the port pins for the switch.
#include <Servo.h>
// Pin numbers for the switches and servo
const int switchPin1 = 2;
const int switchPin2 = 3;
const int servoPin = 9;
// Servo object
Servo servoMotor;
// Variables to store the servo positions
int servoPosition = 90;
void setup()
{
// Initialize the servo and switch pins
servoMotor.attach(servoPin);
pinMode(switchPin1, INPUT_PULLUP);
pinMode(switchPin2, INPUT_PULLUP);
// Set the initial servo position to 90 degrees
servoMotor.write(servoPosition);
}
void loop()
{
(digitalRead(switchPin1)?LOW:HIGH)?servoMotor.write(90):servoMotor.write(0);
(digitalRead(switchPin2)?LOW:HIGH)?servoMotor.write(90):servoMotor.write(180);
}