3 way toggle pin and servo motor

Hello!

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 :slight_smile:

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
}

#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 servoMotor;

void setup() {
  servoMotor.attach(servoPin);
  pinMode(switchPin1, INPUT_PULLUP);
  pinMode(switchPin2, INPUT_PULLUP);
  pinMode(switchPin3, INPUT_PULLUP);
}

void loop() {
  if (!digitalRead(switchPin1))servoMotor.write(0); 
  if (!digitalRead(switchPin2))servoMotor.write(90); 
  if (!digitalRead(switchPin3))servoMotor.write(180); 
}

Welcome to the forum

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 ?

Hi,

Here is the switch that I have : McMaster-Carr

This code that you provide to me doesn't work unfortunally :frowning: . I want the servo to stay in the position and not moving back and forth

why? is servo damaged? or you try this on some simulator?
switch itself may be issue

Hello b_d

Provide a schematic of the hardware setup.

You wiring is wrong for that type of switch
sw

Here is who I wired everything

How do I adapt my code to this kind of wiring?

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
}

Just connect the middle pin of the switch to GND rather than Arduino Pin 3

How do I adapt my code to this kind of wiring?

Don't use that wiring. Connect like this
image
use INPUT_PULLUP mode on the inputs

When I turn it to the left it goes to 0 degree
When I turn it to the right nothing happens
When I turn it in the middle it goes at 180 degrees

Why is this happening?

Did you use my code?

Yes

And how is the switch wired ?

the middle wire to the ground and one to the 2 and one to the 4

Post images of your wiring. Post an image of the switch. You probably have an error.

Try swapping the wires on pin 2 and 4. Report the behaviour.

Hello b_d

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);
}

Have a nice day and enjoy coding in C++.