I'm trying to get my servo to sweep back and fourth, and stop when I depress my button.
Now, I pretty much have that working. My only issue being that it will continue its rotation to completion before it stops.
My question is, is there any way to get this thing to stop like, halfway or where ever it was before I unpressed the button?
I've been at this for 5 or 6 hours, unfortunately, and haven't gotten anything to show for it.
My question is, is there any way to get this thing to stop like, halfway or where ever it was before I unpressed the button?
Yes, you need to say to the servo to rotate 1 or 5 degrees at a time, and check the button every iteration.
if you say goto 0 or 180, the servo wil do so, and not stop halfway ...
but to be sure thats the cause follow AWOL's advice ...
Yeah, that's probably a good idea.
Here's what i've got.
#include <Servo.h>
Servo myservo;
int pos = 0;
int button = 3;
int val;
void setup()
{
myservo.attach(9); pinMode (button, INPUT);
}
void loop()
{
val = digitalRead(button);
if ( val == HIGH)
{
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{
myservo.write(pos);
delay(15);
}
for(pos = 180; pos>=1; pos-=1)
{
myservo.write(pos);
delay(15);
}
}
}
}
Yes, you need to say to the servo to rotate 1 or 5 degrees at a time, and check the button every iteration.
if you say goto 0 or 180, the servo wil do so, and not stop halfway ...
but to be sure thats the cause follow AWOL's advice ...
That's kind of what I was thinking, but as to how I go about doing that, i'm not completely sure.
Actually, I really have no idea. Haha.
I tried putting that in, but it made the servo all jittery. Not sure what I may have done wrong.
But, I think i've taken 2 steps forward, and and step backward. I have it stopping when I remove the button, but only going up to 180.
I can't make it go back.
Any suggestions?
I feel like i'm just missing something really minor, but i'm not sure.