servo push button

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;

void setup() {
Serial.begin(9600);
RedServo.attach(Servopin);

}

void loop() {
for (posi=50;posi<=95;posi= posi+3); { // angles ~ posotions from 90 degrees to 50 degrees
RedServo.write(posi);
delay(ServoDelay);

}

for(posi = 95 ;posi >= 50; posi-2); { // backwards angles positions from 50 back to 90
RedServo.write(posi);
delay(ServoDelay);
}

}

Firstly, in your code, replace all "RedServo" by "coolServo".

Secondly, your code currently does NOT implement button. If you implement button, please read this post Button FAQ: common mistake - button does NOT work as expected. - Introductory Tutorials - Arduino Forum.

Thirdly, you SHOULD NOT use delay() function when using button and servo motor. You can refer to Blink Without Delay example

It's not necessarily wrong to use delay().

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.

First here's OP's code from above fixed so that it actually works like it should:

  • There were ; on the ends of the for's so they won't have worked
  • Servo name fixed as pointed out by IoT_h
  • The decrement of posi looked odd; made same as other one
  • Serial prints added for debug; currently //'d out
  • 1500ms delay added after return sweep
// original code from forum thread 644994 but fixed and 1500ms delay added
#include <Servo.h>

int posi = 0 ;       //position
int Servopin = 10 ;
int ServoDelay = 30 ;
Servo RedServo; //was coolServo XXXX

void setup()
{
  Serial.begin(9600);
  RedServo.attach(Servopin);
  Serial.println("setup done");
}

void loop()
{
  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);
  }

  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);
  }

  delay(1500);
}

And here's a version that works with a button.

(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

Many Thanks Everybody sayHovis you are the Man

sayHovis : regarding your question posi-3 aand posi +2 are to increas / decrease the speed of the servo movement

I still dont think using delay() is a good idea.

Learning about it now and how its a blocking function will help for future projects.

xl97:
I still dont think using delay() is a good idea.

That's why I said it wasn't necessarily wrong :wink: 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.

OP heed the concerns about using delay().

one side to ground with the Arduino and the servo (common) and d other one to a pin mode ,like the picture but wit just one button

btw should i attach a resistor to a push button? some youtube videos recomend doing so

Arrow008:
sayHovis : regarding your question posi-3 aand posi +2 are to increas / decrease the speed of the servo movement

I wondered why it was different in the 2 directions is all

Arrow008:
btw should i attach a resistor to a push button? some youtube videos recomend doing so

You'll see I had pinMode(pin, INPUT_PULLUP) which turns on the internal resistor: no external resistor required