Servo Motor Question

I'm using a 3 way switch to control a servo motor. I can get it to change for CW to CCW when i change position on the switch. Do i need to add a ELSE statement to get it to stop on a predetermined angle ?

#include <Servo.h>

Servo myservo;  // TO control a servo with a 3 way switch
// Use 3 way switch to control servo position.
// First position moves CW
// Second position moves CCW

int pos = 0;    // variable to store the servo position and starting position
int buttonApin = 2;
int buttonBpin = 3;

bool buttonAstatus; // stores button state
bool buttonBstatus;

void setup()
{
  Serial.begin(9600);
  Serial.print("Running v2.0 of Servo controlled by a 3 way switch");
  pinMode(buttonApin, INPUT_PULLUP);
  pinMode(buttonBpin, INPUT_PULLUP);

  buttonAstatus = digitalRead(buttonApin);
  buttonBstatus = digitalRead(buttonBpin);
  Serial.println(buttonAstatus);
  Serial.println(buttonBstatus);

  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop()
{
  buttonAstatus = digitalRead(buttonApin);
  buttonBstatus = digitalRead(buttonBpin);
  Serial.println(buttonAstatus);
  Serial.println(buttonBstatus);

  if (buttonAstatus == LOW)
  {
    myservo.write((digitalRead(2)) ? 0 : 180);
    {
      Serial.print(pos);
    }
  }

  if (buttonBstatus == HIGH)
  {
    myservo.write((digitalRead(3)) ? 180 : 0);
    {
      Serial.print(pos);
    }
  }
}
    myservo.write((digitalRead(2)) ? 0 : 180);

You gave the pins names. Why did you quit using the names? You only call the write() method when the switch is pressed. So, why do you need to read the state of the switch again?

Is this a continuous (360 degree) servo? If so then yes you will need some code to write a value other than 0 or 180 if you want it to stop. The value needed is usually around 90 but you'll have to test to get the exact number.

But you cannot stop a continuous servo at a specific position. You can tell to stop but it will stop in any random position it feels like.

If it's not a continuous servo then I don't know why you're talking about CW and CCW. If you send any number from 0 to 180 then it will go to that specified position.

If you draw a schematic so we know how your "3-way switch" is wired and provide a link to exactly what switch you have then we might provide more help.

Steve

Thanks for questioning the Continuous Servo. I was using the wrong servo motor for my application.