Hello all
i am very new to programing and to arduino as well...
how ever, i'm trying to operate a servo with one push button, which will activate one cycle,(starting point 90 degree to 50 degrees & backwoods with 1500 ms delay in btween) also i'd like the servo to stop for 1500ml in between the angles , added a delay(1500); but didn't work code attached
cheers!
#include <Servo.h>
int posi = 0 ; //position
int Servopin = 10 ;
int ServoDelay = 30 ;
Servo coolServo;
From what op asked, it seems the button doesnt need to interrupt the sweep, just start it. So it depends if the fors with delays are blocking anything else as yet unstated.
Thst said, @op, do you even need to step the servo? Does your project require that, as opposed to just doing a single servo write to the other side?
But to use the code you have, I'd just put the servo code in a function. Have a boolean called say servoMustMove and start it false. If the button is pressed* set the flag true.
Have an if checking the flag and if its true, run the servo function. After the return sweep, set the flag false.
It neednt even be a state change detect, can just look for it to be pressed. Long as its released by the time the sweeps are finished it will be ok.
(Also realised I left out the delay between the up and down sweeps, added that.)
This code may benefit from:
Using millis() rather than delay() as suggested by IoT
Using state change detect for the button. Right now it will sweep again and gain if the button is held down
(It depends what (if anything) the sketch needs to do other than what we already know.)
In the code below I changed it so the servo attach-es at 50 not the defualt 90.
BTW OP, did you mean to sweep "up" in 3's but "down" in 2's?
// amended code from forum thread 644994: servo does one cycle after button press
#include <Servo.h>
int posi = 50 ; //position XXX changed so it attaches at the start point of sweep
int Servopin = 10 ;
int ServoDelay = 30 ;
Servo RedServo; //was coolServo XXXX
//these lines are new for the button cycle stuff
bool servoMustMove = false;
byte buttonPin = 3; //button from pin to ground
void setup()
{
Serial.begin(9600);
RedServo.write(posi); // XXX new so it attaches at the start point of sweep
RedServo.attach(Servopin);
pinMode(buttonPin, INPUT_PULLUP); //button from pin to ground
Serial.println("setup done");
}
void loop()
{
// the servo sweep stuff has been moved to a function servoSweep()
if (!digitalRead(buttonPin)) servoMustMove = true; //! means not, this checks for button press
if (servoMustMove) servoSweep();
}//loop
void servoSweep()
{
for (posi = 50; posi <= 95; posi = posi + 3) // angles ~ posotions from 90 degrees to 50 degrees XXX lose the ;
{
//Serial.print("going up ");
//Serial.println(posi);
RedServo.write(posi);
delay(ServoDelay);
}
delay(1500); //XXXX new, forgot it before
for (posi = 95 ; posi >= 50; posi = posi - 2) // backwards angles positions from 50 back to 90 XXX lose the ;
{
//Serial.print("going down ");
//Serial.println(posi);
RedServo.write(posi);
delay(ServoDelay);
}
servoMustMove = false; // XXXX new
}//servoSweep
xl97:
I still dont think using delay() is a good idea.
That's why I said it wasn't necessarily wrong and not that it's a good idea. In this case we don't know if op will get away with it. There may be other, unstated requirements.
xl97:
Learning about it now and how its a blocking function will help for future projects.
Not going to disagree with that: absolutely right. But sometimes in the heat of the moment and perhaps facing a deadline, it's better to go with what works immediately, and leave the learning for another day, provided it's not painting you into a corner in the current project.