Hi gang. Not only am I new to the forum, but I'm new to arduino. I need a high torque solution for a project and began playing with continuous rotation servos. I can get them to run, change speed, change direction and only run for a short amount of time, but I'm lost when I began adding a button into the mix.
Here's what I'd like it to do. If you could tell me if I'd have better luck with a stepper motor or if I'm just barking up the wrong tree, I'd appreciate it.
I'd like to press a button and have the servo move in some direction for a given duration then stop. I would then like to press the same button and have it reverse itself for the same duration of time and stop. I don't want a looped delay. I may not press the button for hours.
Let's say I press the button and it moves CC for 2 seconds. Then it waits there until I press the button again. This time it moves C for 2 seconds. Any place you could point me to help shine some light on this would be great. Thanks.
#include <Servo.h>
Servo myservo;//create servo object to control a servo
int servoPin = 9;
void setup()
{
myservo.attach(9);//attachs the servo on pin 8 to servo object
I understand that this is an incredibly simple loop at present. I'll also post the code with the button press I've been using.
#include <Servo.h>
Servo myservo;//create servo object to control a servo
const int buttonPin = 8; //pushbutton pin
const int servoPin = 9; //servo pin
Servo servo; //servo name
int counter = 0;
void setup() {
servo.attach (servoPin);
// Set up the pushbutton pins to be an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// local variable to hold the pushbutton states
int buttonState;
//read the digital state of buttonPin with digitalRead() function and store the //value in buttonState variable
buttonState = digitalRead(buttonPin);
//if the button is pressed increment counter and wait a tiny bit to give us some //time to release the button
if (buttonState == LOW) // move the servo
{
counter++;
delay(150);
}
if(counter == 0)
servo.write(90); // doesn't move until button is pressed
else if (counter == 1)
servo.write(0); //run servo full speed. Here is where I have an issue. I'd like to put a delay funcion
//delay(2000); //that would run the servo 2 seconds then completely stop until I press the button again //servo.detatch //but when I use it I get a bunch of compiling errors
else if(counter == 2)
servo.write (180); //run servo full speed opposite direction. Run 2 seconds and stop until pressed again
//else reset the counter to 0 which resets thr servo to 0 degrees
else
counter = 0;
}
This will let me press the button to change direction, but I can't write in the run time. If I attempt to write in any commands under "myservo.write()" I just get compile errors concerning the "if, else" code. Any help is much appreciated. Thanks for taking a look at it.
Apparently the "quote" tab is not the tab for entering code. Sorry. I'll get it figured out.
#include <Servo.h>
Servo myservo;//create servo object to control a servo
const int buttonPin = 8; //pushbutton pin
const int servoPin = 9; //servo pin
Servo servo; //servo name
int counter = 0;
void setup() {
servo.attach (servoPin);
// Set up the pushbutton pins to be an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// local variable to hold the pushbutton states
int buttonState;
//read the digital state of buttonPin with digitalRead() function and store the //value in buttonState variable
buttonState = digitalRead(buttonPin);
//if the button is pressed increment counter and wait a tiny bit to give us some //time to release the button
if (buttonState == LOW) // move the servo
{
counter++;
delay(150);
}
if(counter == 0)
servo.write(90); // doesn't move until button is pressed
else if (counter == 1)
servo.write(0); //run servo full speed. Here is where I have an issue. I'd like to put a delay funcion
//delay(2000); //that would run the servo 2 seconds then completely stop until I press the button again
//servo.detatch //but when I use it I get a bunch of compiling errors
else if(counter == 2)
servo.write (180); //run servo full speed opposite direction. Run 2 seconds and stop until pressed again
//else reset the counter to 0 which resets thr servo to 0 degrees
else
counter = 0;
}
and the simple one again
#include <Servo.h>
Servo myservo;//create servo object to control a servo
int servoPin = 9;
void setup()
{
myservo.attach(9);//attachs the servo on pin 8 to servo object
}
void loop()
{
delay(2000);
myservo.write(0);
delay(2000);
myservo.write(90);
delay(2000);
myservo.write(180);
delay(2000);
myservo.write(90);
}
When you want several commands to be controlled by an if/else they need to be enclosed by {}. Like
else if (counter == 1)
{
servo.write(0); //run servo full speed. Here is where I have an issue. I'd like to put a delay funcion
delay(2000); //that would run the servo 2 seconds then completely stop until I press the button again
servo.detatch //but when I use it I get a bunch of compiling errors
}
It's good practise to use the braces {} even if you only have a single command after the if. But they are only essential with more than one. So that's why you got away with the other ifs.
I'm not sure that will completely solve your problems but it should get you a bit further.
That was a great help. I can feel myself getting further along. Here's as far as I got. This will allow me to press the button, it moves a certain amount of time then stops until I press the button again. That's cool. Now, I'd like to press it again and turn it the exact same amount in the opposite direction, then stop.
This is what I have.
#include <Servo.h>
Servo myservo;//create servo object to control a servo
const int buttonPin = 8; //pushbutton pin
const int servoPin = 9; //servo pin
Servo servo; //servo name
int counter = 0;
void setup() {
servo.attach (servoPin);
// Set up the pushbutton pins to be an input:
pinMode(buttonPin, INPUT);
}
void loop()
{
// local variable to hold the pushbutton states
int buttonState;
//read the digital state of buttonPin with digitalRead() function and store the //value in buttonState variable
buttonState = digitalRead(buttonPin);
//if the button is pressed increment counter and wait a tiny bit to give us some //time to release the button
if (buttonState == LOW) // move the servo
{
counter++;
delay(150);//jiggle delay
}
if (counter == 0)
{
servo.attach(9); //attach the servo
servo.write(90); // doesn't move until button is pressed
}
else if (counter == 1)
{
servo.write(0); //run servo full speed.
delay(300); //run time
servo.write(90); //make sure it tells it to stop
servo.detach(); //detatches it to ensure it stays off
counter = 0; //sends back to counter 0 where I have the servo reattach itself.
}
}
I played around with setting up a second "if else" with a (counter == 2) and have it attach the servo when I hit the button the second time, then the regular commands, but I don't think it likes it very much. Can I assume that when you detach the servo it stalls the entire program?
This is what I was attempting
#include <Servo.h>
Servo myservo;//create servo object to control a servo
const int buttonPin = 8; //pushbutton pin
const int servoPin = 9; //servo pin
Servo servo; //servo name
int counter = 0;
void setup() {
servo.attach (servoPin);
// Set up the pushbutton pins to be an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// local variable to hold the pushbutton states
int buttonState;
//read the digital state of buttonPin with digitalRead() function and store the //value in buttonState variable
buttonState = digitalRead(buttonPin);
//if the button is pressed increment counter and wait a tiny bit to give us some //time to release the button
if (buttonState == LOW) // move the servo
{
counter++;
delay(150);
}
if (counter == 0)
{
servo.attach(9); //attaches servo to pin 9
servo.write(90); // doesn't move until button is pressed
}
else if (counter == 1)
{
servo.write(0); //run servo full speed.
delay(300); //run time
servo.write(90); //make sure it tells it to stop
servo.detach(); //detatches it to ensure it stays off
counter = 0; //sends back to counter 0 where I have the servo reattach itself.
}
else if (counter == 2)
{
servo.attach(9);// should attach to pin 9
delay(25)//give it a little time to attach before next command
servo.write(180); //run servo full speed.
delay(300); //amount of time it runs
servo.write(90); //tells it to stop
servo.detach();//detatches it to ensure it stays off
counter = 0;
}
}
With this code, it almost works. Sometimes it will work great, then you'll press the button and get no change at all. Sometimes holding the button down will get it to kick into counter 2. It just seems really buggy. The first set of code seems tight. No bugs at all. Adding the counter 2 seems to do something I don't understand. Thanks for all the help so far. I'll keep reading on the language and banging along.
No, detach() doesn't stop the program. What it does is allow the servo to be moved by any load without any commands. In your program it serves no useful purpose that I can see. You really don't need to keep attaching and detaching the servo. You have the attach in setup(). Try taking all the other attach()/detach() out. I've never yet needed to use detach() in the servo programs I've written.
Problems not seeing a button push are very likely caused by the extra delays in your program. delay() does completely stop the program from doing anything including reading buttons for the length of the delay. If you want a program to be properly responsive you really need a different method of timing using millis() as in the BlinkWithoutDelay example in the IDE. But that would need a fairly major rewrite.