hey everyone,
i am new to arduino (only a few weeks) and im having trouble with the code.
i have so far hacked a servo to be continuous rotation and got that working with arduino using servo library, also have got as far as reversing the direction of servo when limit switch is hit.
my intentions are to spin the servo one way until it hits a limit then it change direction and spin other way until other limit switch is hit.
I want it to keep doing this in a loop or at least a few times.
here is the code i have so far which just makes it change direction while the switch is held. i want to just change direction when its hit and then keep going until it hits other ends limit switch.
any help at all would be greatly appreciated. thanks.
(have tried using state change detection also but cant figure it out)
// Rotating a continuous servo (tested with a hobbytech ym-2763)
// button on digital pin 2 with 10k resistor connected between pin 2 and ground.
// limit switch on common and normally open
#include <Servo.h>
Servo myservo; // create servo object to control a servo
const int buttonPin = 2; // the number of the pushbutton pin
int buttonState = 0;
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn servo on:
myservo.write(100); // rotate counterclockwise slow speed
delay(500);
myservo.writeMicroseconds(1500); // stop
delay(3000);
}
else {
myservo.write(45); // rotate clockwise slow speed
delay(500);
myservo.writeMicroseconds(1500); // stop
delay(3000);
}
}
start servo running either way
start of loop
read left button
if pressed at this moment
run servo left to right
end of if
read right button
if pressed at this moment
run servo right to left
end of if
end of loop
The servo is running one way or the other all the time and changes direction when either button is pressed. There are smarter ways to do the same thing but keep it simple to start with.
NOTE - the buttons do not need to be held in, a momentary push will cause the servo to change direction.
hi i have tried doing what you have said and have got as far as controlling the direction of the servo using either limit switch but i still cant figure out how to get it to loop till the other button is pressed.
any help with how to code this would be greatly appreciated.
here is the new code so far
#include <Servo.h>
Servo myservo; // create servo object to control a servo
const int buttonPin = 2; // the number of the pushbutton pin
const int buttonPin4 = 4;
int buttonState = 0;
int buttonState4 = 0;
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
buttonState = digitalRead(buttonPin);
{
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn servo on:
myservo.write(100); // rotate counterclockwise slow speed
delay(500);
myservo.writeMicroseconds(1500); // stop
delay(3000);
}
}
buttonState4 = digitalRead(buttonPin4);
{
if (buttonState4 == HIGH) {
myservo.write(45); // rotate clockwise slow speed
delay(500);
myservo.writeMicroseconds(1500); // stop
delay(3000);
}
}
}
If the servo is to change direction when one limit switch is hit, and run until the other limit switch is hit, those delay()s have got to go. Every single one of them.
oh i thought about that. the problem is those delays give me the type of gearing i need to make it go slow enough.
what i mean is i want it to go half a turn then stop there for an amount of time before going again.
is it not possible to do this and use limit switches as i currently have it configured???
thanks
Look at your code. You are reading one switch, and, if it is not pressed, you start the motor moving, wait, stop the motor, and wait some more. Then, you read the other switch, and make the motor run the other way a little bit.
You need a while loop that keeps the motor running at the slow speed until the other limit switch is pressed.
I really hate to see code like:
const int buttonPin = 2; // the number of the pushbutton pin
const int buttonPin4 = 4;
If you number one variable, number both of them. Although using the pin number in the name is silly. What happens if you need to move the switch to another pin?
int buttonState = 0;
int buttonState4 = 0;
Why are these global? Which functions use them? If only one function uses a variable, it should be local to that function. Globals are shared between all functions. No sharing == not global.
sorry im not sure what you mean by global???
also i am just messing around with arduino at the moment so im not aware of code etiquette and i thank you for ur suggestions but maybe a little less contempt would be a little more helpful (just my opinion).
i have deleted delays and i am getting the effect i was looking for thank you but my servo is spinning to fast for what i need so maybe ill have to gear it down or something.
thankyou
yea i have played around with those numbers and have got it slower but i was hoping for a .5 rpm kind of speed and driving servo to slowly seems to kill its power. i am trying to figure out a while loop to put in but cant get my head around it. thanks for your help though i am alot further along now.
i am trying to figure out a while loop to put in but cant get my head around it.
buttonState = digitalRead(buttonPin);
{
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH)
{
while((buttonState = digitalRead(buttonPin4)) != HIGH)
{
// Make the servo move until it triggers the other limit switch...
}
}
thanks i will try and get it to work.
i have no and nc contacts on my limit switches, is it possible to turn servo in opposite direction when limit is on and not turn again till other limit is off??? if that makes sense :~
thanks again for everyones help
If you want, say, the left to right change to happen when the left nc contacts open and the right to left change to happen when the right no contacts close then test at one end for the button to be HIGH and at the other end for it to be LOW. Which way round it is depends on how the buttons are wired, ie held normally LOW or HIGH.
You could also test both ends for HIGH or LOW if you wire the buttons to be kept in the appropriate state using pull up/down resistors.
hey i think i have finally figured it out thanks to everyones help.
here is the code i have if anyone wants to point out any issues or errors in it or any pointers when im writing code in future please let me know
thanks again.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
const int buttonPin = 2; // the number of the pushbutton pin
const int buttonPin4 = 4;
int buttonState = 0;
int buttonState4 = 0;
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
buttonState = digitalRead(buttonPin);
{
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH)
while((buttonState4 = digitalRead(buttonPin4)) != HIGH) {
// turn servo on:
myservo.write(100); // rotate counterclockwise slow speed
delay(250);
myservo.writeMicroseconds(1500); // stop
delay(3000);
}
}
{
buttonState4 = digitalRead(buttonPin4);
{
if (buttonState4 == HIGH)
while((buttonState = digitalRead(buttonPin)) != HIGH) {
myservo.write(45); // rotate clockwise slow speed
delay(250);
myservo.writeMicroseconds(1500); // stop
delay(3000);
}
}
}
}
also i think the moderator told me to put code in brackets when im posting. can someone plz tell me how to do this
Just above the row of smiley faces, there is an icon with a # on it. Select that before pasting code. Or, paste the code, then select it all, then press the icon with the #.
You can modify your last post, and add the code tags (as above).