Trouble Controlling Servo with Toggle Switch

Good Afternoon Everyone,

I am trying to control the position of a servo via a toggle switch. Quite simply; switch in OFF position correlates to servo at 90 deg, switch in ON position correlates to servo at 100 deg. When the switch is turned back off it would then just go back to 90.

I have read and tried to implement a few examples I found on here but am not getting a response from the servo motor. Attached is the connection diagram I have generated, Including a capacitor to help the board with the initial current draw of the servo, and a resistor to pull down the voltage.

Here is the code I am working with (from this forum)

#include <Servo.h>
int button = 4; //button pin, connect to ground to move servo
int press = 0;
Servo servo;
boolean toggle = true;

void setup()
{
  pinMode(button, INPUT); //arduino monitor pin state
  servo.attach(9); //pin for servo control signal
  digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
      servo.write(90);
      toggle = !toggle;
    }
    else
    {
      servo.write(100);
      toggle = !toggle;
    }
  }
  delay(500);  //delay for debounce
}

Greatly appreciate your help

Your sketch is too complicated. Try this:

#include <Servo.h>
const int switchPin = 4; //active low
const int servoPin = 9;
Servo servo;

void setup() {
  pinMode(switchPin, INPUT_PULLUP);
  servo.attach(servoPin); 
}

void loop() {
  if (digitalRead(switchPin))
    servo.write(90);  // HIGH (switch is off)
  else
    servo.write(100);  // LOW (switch is on)

  delay(500);  //delay for debounce
}