URGENT help to finish servo code!!

I’ve been combining button and servo library code to run a servo sequence of 0 to 20 degrees then back again (repeated) when activated by a pushbutton. So far it is doing everything I need apart from at the end of the repeated sequence it needs to stay in the 20 degree position for a specified amount of time before finishing the loop.

I can make it stop on the 0 degree starting pos with a delay in the right place but not for the life of me will it remain frozen in the 20 degree position.

I have been scouring the net and trying all sorts of solutions but I can’t seem to crack it, I’m a virtual beginner and I think i’m missing something obvious.

I have a deadline in a few days for this work and would really appreciate some help finishing it off (sorry for the shoddy code), many thanks in advance! James.

#include <buttons.h>

Button B;

#include <Servo.h>

Servo myservo; // create servo object to control a servo

int pos = 0; // variable to store the servo position

void setup() {
B.assign(7);
B.setMode(OneShotTimer);
B.setTimer(1500);
B.setRefresh(500);

myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop() {

switch(B.check()) {
case ON:

for(pos = 0; pos < 20; pos += 1) // goes from 0 degrees to 20 degrees

{
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(50);
}
for(pos = 0; pos < 20; pos += 1) // goes from 0 degrees to 20 degrees

{
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(50);
}for(pos = 0; pos < 20; pos += 1) // goes from 0 degrees to 20 degrees

{
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(50);
}
}
}

Try this: http://arduino.pastebin.com/f3f6f4691 and see if it helps. Just added a write and a delay.

And formatted the code a bit.

That was one of the solutions I tryed, it just reverts back to the starting position (0) like its not even there. Got any other ideas? I'm really confused, I thought that would work.

Thanks, James.

finally I got it working, added in a second delay in the code you pasted after the first delay, never been so happy to see a servo stop at 20 degrees!! hehe.

Thanks very much for your help alphabeta, have a good day.

James.

#include <buttons.h>
#include <Servo.h>

Button B;
Servo myservo; // create servo object to control a servo

int pos = 0; // variable to store the servo position

void setup() {
B.assign(7);
B.setMode(OneShotTimer);
B.setTimer(1500);
B.setRefresh(500);

myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop() {
switch(B.check()) {
case ON:
for(pos = 0; pos < 20; pos += 1) // goes from 0 degrees to 20 degrees
{
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(50);
}
for(pos = 0; pos < 20; pos += 1) // goes from 0 degrees to 20 degrees
{
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(50);
}
for(pos = 0; pos < 20; pos += 1) // goes from 0 degrees to 20 degrees
{
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(150);
delay(8000); // delays the servo at 20 degrees
myservo.write(20); // puts the servo at 20 in a stable manner

break;
}
}
}

This bit:

           for(pos = 0; pos < 20; pos += 1) // goes from 0 degrees to 20 degrees
           {   
                 myservo.write(pos);     // tell servo to go to position in variable 'pos' 
                 delay(150); 
               delay(8000); // delays the servo at 20 degrees
           myservo.write(20); // puts the servo at 20 in a stable manner
           
           break;
}

is a bit wasteful.
[
You enter a loop, go to 0 degrees, delay for 8150 milliseconds, go straight to 20 degrees and exit the loop with a break, not passing through 1…19.

In effect, the whole thing is just:

myservo.write(0);
delay(8150);
myservo.write(20);

If you followed a consistent indentation pattern, you’d be able to see the structure of your program more easily.

Also, being picky, whilst the servo may sweep 20 degrees in the other loops, it only stops at 20 with the myservo.write (20); in all other cases it stops at 19.

Hi guys, just couldn´t find "buttons.h" library...., a link to....please ! :o

thanks

There is this: http://www.arduino.cc/playground/Code/Buttons

And this: http://www.arduino.cc/playground/Code/Button

Thanks ;)