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);
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
}
}
}
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
}
}
}]
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
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.
Hi,
Can you explain how this "3-way" switch is wired?
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?
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);
}
}