How to turn off a servo with a sketch?

I'm making a project that includes a continuous rotation servo that will need to go left with one button, right with another, and the disabled and enabled (a toggle) with another button. Here is how far I've gotten, and please keep it simple. This is my first class with Arduino. Thanks for any helpful support.

MY CURRENT SKETCH (I NEED TO DISABLE AND ENABLE THE SERVO NOW):
#include <Servo.h> // add servo library
#define sw1_pin 5
#define sw2_pin 6
Servo myservo; // create servo object to control a servo

volatile boolean sw1 = false;
volatile boolean sw2 = false;

uint8_t sw1ButtonState = 0;
uint8_t sw2ButtonState = 0;

uint8_t lastsw1ButtonState = 0;
uint8_t lastsw2ButtonState = 0;

void setup() {
Serial.begin(9600);
pinMode(sw1_pin, INPUT_PULLUP);
pinMode(sw2_pin, INPUT_PULLUP);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop() {

checkIfSw1ButtonIsPressed();
checkIfSw2ButtonIsPressed();

if( sw1){
Serial.println("sw1");
sw1 = false;
myservo.write(60);
delay(15);
}
else if( sw2){
Serial.println("sw2");
sw2 = false;
myservo.write(110);
delay(15);
}

// waits for the servo to get there
}

void checkIfSw1ButtonIsPressed()
{
sw1ButtonState = digitalRead(sw1_pin);

if (sw1ButtonState != lastsw1ButtonState)
{
if ( sw1ButtonState == 0)
{
sw1=true;
}
delay(50);
}
lastsw1ButtonState = sw1ButtonState;
}

void checkIfSw2ButtonIsPressed()
{
sw2ButtonState = digitalRead(sw2_pin);

if (sw2ButtonState != lastsw2ButtonState)
{
if ( sw2ButtonState == 0)
{
sw2=true;
}
delay(50);
}
lastsw2ButtonState = sw2ButtonState;
}

What do you mean by enable/disable ?

You can detach() a servo and attach() it again when required or you could use a variable to keep track of its status and ignore commands to it when it is disabled.

UKHeliBob:
What do you mean by enable/disable ?

You can detach() a servo and attach() it again when required or you could use a variable to keep track of its status and ignore commands to it when it is disabled.

UKHeliBob:
What do you mean by enable/disable ?

You can detach() a servo and attach() it again when required or you could use a variable to keep track of its status and ignore commands to it when it is disabled.

When I say disable/enable I mean I want to turn off the servo instead of keeping it at 90 so it won't move and it won't burn out b/c of staying on. I'll do research on your command, thanks.

If you repeatedly write the same value to the servo it will not burn out

If by "disable" you mean "power down" you need additional hardware, but indeed there's no reason to as a servo won't burn or so.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.
Before you post your code, in the IDE select TOOLS, AUTO FORMAT, this will indent your code so it will be easier to read.

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

Can you post spec/data link for the servo please?

Thanks.. Tom.. :slight_smile:

Kite1433:
I'm making a project that includes a continuous rotation servo that will need to go left with one button, right with another, and the disabled and enabled (a toggle) with another button.

I think that means that the 3rd button is used to disable the other two buttons rather than to disable the servo.

If that is the case create a variable called (say) servoEnabled and change it between true and false using the 3rd button.

Then the code for buttons 1 and 2 will check servoEnabled and if it is false they will do nothing

...R