Servo Motor Question

I'm trying a servo with a 3 way switch and a potentiometer. The 3 way switch move my servo to my set angel. When i use my potentiometer to move the servo to random position it will clich after a few seconds. I have attached my program below.

``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(10); // attaches the servo on pin 10 to the servo object
}

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

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

if (buttonBstatus == HIGH)
{
myservo.write((digitalRead(3)) ? 180 : 0);
{
Serial.print(pos);
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180 ); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(600); // waits for the servo to get there
}
}
}

That's not a compete program. Post the actual program and a diagram showing how the 3-way switch is wired.

Steve

Sorry about that. I have attached a wiring diagram.


[code#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 180
// Second position moves back to 0

int pos = 180;    // variable to store the servo position and starting position
int buttonApin = 2;
int buttonBpin = 3;
int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin


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(10);  // attaches the servo on pin 10 to the servo object
}

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

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

  if (buttonBstatus == HIGH)
  {
    myservo.write((digitalRead(3)) ? 180 : 0);
    {
      Serial.print(pos);
      val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
      val = map(val, 0, 1023, 0, 180 );     // scale it to use it with the servo (value between 0 and 180)
      myservo.write(val);                  // sets the servo position according to the scaled value
      delay(600);                           // waits for the servo to get there
    }
  }
}]

Elegoo Drawing+POT-Layout1.pdf (69.3 KB)

Sorry, but I can't make any sense of how the 3 way switch is wired in your diagram. Does it move t 3 positions or does it have 3 poles or maybe something else

As to your code

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

Why do you read the state of pin 2 (buttonApin) again when you already know that it is HIGH ?

The switch just has three poles. Should I remove the second read function?

The switch just has three poles.

Do you mean that it is a 3 pole on/off switch with no common contact ?

Should I remove the second read function?

You certainly don’t need to read the status of pin 2 twice as it will not have changed between reads.

I also note that reading the pot depends on the state of pin 3. Is that what you intend

You correct on the switch. I want to be able to turn the switch to open and close but use the pot to adjust the angel.

Here is your sketch, simplified.
If button A reads HIGH (switch open), move servo to 0.
If button B reads HIGH (switch open), move servo to 180 then immediately move to the pot position then wait .6 seconds.

I expect that the glitches are caused by trying to move to both 180 and the pot position when buttonB reads HIGH.

#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 180
// Second position moves back to 0
const byte buttonApin = 2;
const byte buttonBpin = 3;
const byte potpin = 0;  // analog pin used to connect the potentiometer
const byte servoPin = 10;


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


  Serial.println(digitalRead(buttonApin));
  Serial.println(digitalRead(buttonBpin));


  myservo.attach(servoPin);
}


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


  if (digitalRead(buttonApin) == HIGH)
  {
    myservo.write(0);
    Serial.print(myservo.read());
  }


  if (digitalRead(buttonBpin) == HIGH)
  {
    myservo.write(180);
    Serial.print(myservo.read());
    myservo.write(map(analogRead(potpin), 0, 1023, 0, 180 ));
    // sets the servo position according to the scaled value
    delay(600);
  }
}

Your correct on the glitch. I loaded your program. The glitch is when the b button reads high and the angel is not 180. I followed your instruction, but i still have a glitch.

flex59:
Your correct on the glitch. I loaded your program. The glitch is when the b button reads high and the angel is not 180. I followed your instruction, but i still have a glitch.

Does your switch really have three positions?
What switch pins are connected in each position?
What do you want the sketch to do in each position?

I have attached my circuit. When A goes high i want it to go closed. When B goes high i want to be able to set the angel and leave it. When A goes back high it should close.

Elegoo Drawing+POT-Layout1.pdf (69.3 KB)

The circuit diagram is useless when it comes to figuring out how your switch works.

If you take out this line it should fix the problem:

  myservo.write((digitalRead(2)) ? 0 : 180);

Hi,
OPs last PDF.


Please when posting diagrams, post them as Exported jpg files, they are easier to read.

Thanks.. Tom... :slight_smile:

Hi,
Can you explain how this "3-way" switch is wired?
abv1.jpg
Can you post a picture of it so we can see your component layout?
What does Black, Gold, Gold mean?
OR is your switch a 3 POSITION switch?

Tom..

abv1.jpg

HI,
I think this is what you have;

Your 3 pos switch would read;

Input Pin 2 Input Pin 3
Preset
Pos 1

0

1
Preset
Pos 2

1

0
Pot
Input

0

0

Tom... :slight_smile:

Hi,
See if this code works;
When reading the 3 pos switch, you have to consider not just the inputs that are high but the low inputs as well.

#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 180
// Second position moves back to 0
const byte buttonApin = 2;
const byte buttonBpin = 3;
const byte potpin = A0;  // analog pin used to connect the potentiometer
const byte servoPin = 10;
bool ButtonAState;
bool ButtonBState;
int analogRaw;
int servoAngle;

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

  Serial.println(digitalRead(buttonApin));
  Serial.println(digitalRead(buttonBpin));

  myservo.attach(servoPin);
}

void loop()
{
  ButtonAState = digitalRead(buttonApin);
  ButtonBState = digitalRead(buttonBpin);
  analogRaw = analogRead(potpin);
  servoAngle = map(analogRaw, 0, 1023, 0, 180);

  Serial.print("Button A = ");
  Serial.println(ButtonAState);
  Serial.print("Button B = ");
  Serial.println(ButtonBState);
  Serial.print("Pot = ");
  Serial.println(analogRaw);
  Serial.print("Pot Angle = ");
  Serial.println(servoAngle);

  if (ButtonAState == HIGH && ButtonBState == LOW)  //Preset 2 mode
  {
    myservo.write(0);
    Serial.print(myservo.read());
  }

  if (ButtonAState == LOW && ButtonBState == HIGH)  //Preset 1 mode
  {
    myservo.write(180);
    Serial.print(myservo.read());
  }

  if (ButtonAState == HIGH && ButtonBState == HIGH) // Pot mode
  {
    myservo.write(servoAngle);
    // sets the servo position according to the scaled value
    delay(600);
  }
}

Tom… :slight_smile:

Are you using a 3-way light switch intended for house electrical wiring? That would be a single-pole, double-throw switch for wiring purposes.

John,

Thanks for all your help. I had to remove - myservo.write((digitalRead(3)) ? 0 : 180); instead of (2) to get the glitch to go away.

Chuck