Servo connected to 6pin 3 way toggle switch code help!!

Hi,

I have a continuous servo motor (Tower Pro MG995) and i want to use a 6pin 3 way toggle switch to control the direction of the servo so that push up and the servo spins, push down and the servo spins the opposite direction and if the switch is in the middle position the servo stops completely.

I have this code below but it doesn't seem to workn and im not sure why. I am new to this so any help would be much appreciated.

Thanks

//pin 5 and 6 are connect to toggle swtich

#include <Servo.h>
Servo myServo; // Create Servo object to control the servo

int forwardPin = 5;
int backwordPin = 6;

bool lastStateOfFowardPin = LOW;
bool lastStateOfBackwordPin = LOW;

void setup() {
myServo.attach(9); // Servo is connected to digital pin 9
pinMode(13,OUTPUT);
digitalWrite(13,HIGH);
pinMode(forwardPin,INPUT);
pinMode(backwordPin,INPUT);

}

void loop() {

if(digitalRead(forwardPin) == HIGH && (lastStateOfFowardPin != HIGH))
{
myServo.writeMicroseconds(1700); // Counter clockwise
lastStateOfFowardPin = HIGH;
lastStateOfBackwordPin = LOW;
}
else if(digitalRead(backwordPin) == HIGH && (lastStateOfBackwordPin != HIGH))
{
myServo.writeMicroseconds(1300); // Clockwise
lastStateOfBackwordPin = HIGH;
lastStateOfFowardPin = LOW;
}
else if ((digitalRead(forwardPin) == LOW && digitalRead(backwordPin) == LOW)&&(lastStateOfFowardPin != LOW || lastStateOfBackwordPin != LOW))
{
myServo.writeMicroseconds(1500); // Stop
lastStateOfFowardPin = LOW;
lastStateOfBackwordPin = LOW;
}
}

Hi.

You forgot to use code tags, do not forget those.
You told us you are using a 6 pin toggle switch, and the code tells us that you have to connect it to pins 5 and 6.
Where does the (at least one) other pin go ?
You are looking for a HIGH state on a pin, how do you ensure it gets HIGH ?
You are looking for a LOW state on a pin, how do you ensure it gets LOW?

The code doesn't seem to work ?
What does that mean ?
Tell us what you expected to see, and what you do see instead.

Hi,

The other wire from the center pin of the switch goes to the 5v pin on the arduino.

The High and Low state confuses me to be totally honest.

What im after is for the toggle switch to control the direction of the servo so that push up and the servo spins, push down and the servo spins the opposite direction and if the switch is in the middle position the servo stops completely.

What i get is that when i connect the servo yellow wire to pin 9 it spins and nothing stops it unless u inplug the wire. The position of the switch has no impact on the servo.

Thanks

So, that is how you get the pin HIGH, you connect it to 5 volts when the switch is on.
But you aren't connecting it to GND, so you don't know it gets to e LOW state (it will be undefined).
This is where a pull down resistor would come in, meaning more external components are required.
You can do without an external pull down resistor, and enable the internal pull up resistor puls connecting GND instead of 5 volts to that center pin.
But that means you need to invert the logic.
Not too hard to do but you shouldn't forget that.
So the pin is active when pulled LOW and inactive when pulled HIGH.
This way you will ensure either a LOW or a HIGH level at those pins, and not some undefined condition.

The servo starts spinning because, as it seems, you aren't sending the correct value to have it stop.
Have the sketch tell the servo to start in the off position by sending that in setup, and after you have initialized it.
I guess you have to look for the correct value to send to that servo, i really don't know it.

There's a few ways to find that value.
One way would be to use the "knob" sketch from the examples, and add a serial communication to it, sending the actual value sent to the servo.
That way you can have a look in serial monitor to find the correct value (which should e somewhere around 90 degrees).
You'd need to look up which micro seconds number that would be, because i'm too lazy to do that for you right now.
But i guess that that 1500 you have in your sketch already, is correct.

Below is some servo two button test code you should be able to use with your center return toggle switch. I haven't tested the included else statement.

//zoomkat servo button test 12-29-2011
// Powering a servo from the arduino usually *DOES NOT WORK*.

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(170);
  }    
  
  press2 = digitalRead(button2);
  if (press2 == LOW)
  {
    servo1.write(10);
  }
  else
  {
    servo1.write(90);
  }
}

Hi,
To clarify please..

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Also how are you powering your servo?

Tom..... :slight_smile:

Hi Tom,

I have attached the copy of my circuit. I am very new to this and not worked with motors before especially Servo's.

What my project is is i am building a guitar with a the pickup mounted on two sliders with a belt mounted underneath the runs around the servo and a pulley so when the servo is powered and spinning it slides the pickup up and down (so can choose what position the pickup is within the guitar or different sounds). Like how a scanner works. I am using the servo because it has the motor and the gears built in. It is not very quiet though which is my main issue, so if anyone has any inexpensive solutions that would be much appreciated.

Thanks

My code is:

//pin 5 and 6 are connect to toggle swtich

#include <Servo.h>
Servo myServo;        // Create Servo object to control the servo 

int forwardPin = 5;
int backwordPin = 6;

bool lastStateOfFowardPin = LOW;
bool lastStateOfBackwordPin = LOW;

void setup() { 
  myServo.attach(9);  // Servo is connected to digital pin 9
 pinMode(13,OUTPUT);
 digitalWrite(13,HIGH);
 pinMode(forwardPin,INPUT);
 pinMode(backwordPin,INPUT);
 
} 

void loop() { 
  
  if(digitalRead(forwardPin) == HIGH && (lastStateOfFowardPin != HIGH))
  {
    myServo.writeMicroseconds(1700);  // Counter clockwise
    lastStateOfFowardPin = HIGH;
    lastStateOfBackwordPin = LOW;
  }
  else if(digitalRead(backwordPin) == HIGH && (lastStateOfBackwordPin != HIGH))
  {
    myServo.writeMicroseconds(1300);  // Clockwise
    lastStateOfBackwordPin = HIGH;
    lastStateOfFowardPin = LOW;
  }
  else if ((digitalRead(forwardPin) == LOW && digitalRead(backwordPin) == LOW)&&(lastStateOfFowardPin != LOW || lastStateOfBackwordPin != LOW))
  {
  myServo.writeMicroseconds(1500);  // Stop
  lastStateOfFowardPin = LOW;
  lastStateOfBackwordPin = LOW;
  }
}

Hi,
You will need to connect a 10K resistor from arduino pin 5 to gnd and another from arduino pin 6 to gnd.

At the moment you have the switch connecting pin5 or 6 to 5V, but not to gnd when 5 or 6 is not.

The 10K resistors are what is called pull-down resistors, to pull pins 5 and 6 to gnd when they are not connected to 5V through your switch.

Also you are trying to run your servo off the arduino's 5V, this is not good, the arduino cannot supply the current needed to run the servo properly and can cause damage to your arduino.

Do you have a DMM to measure voltages around you circuit?

Tom.... :slight_smile:

Hi Tom,

I will look at getting the 10k resistors. The servo runs on 4.8v, I have a 3.7v 3000mah lipo Battery with a powerboost 500 charger board to power the arduino, would i be able to run the servo as well as the arduino from this?

I dont have a DMM to do this unfortunately.

Other than this is the code ok and will the code work correctly once i have the resistors connected?

Thanks for your help