Button on/off Servo

Hi Community,

i started a projekt with a servomotor to move 0 - 50 degrees and backward ( loop )

Code:

#include <Servo.h>

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

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

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

void loop() {
  for (pos = 0; pos <= 50; pos += 1) { // goes from 0 degrees to 50 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 50; pos >= 0; pos -= 1) { // goes from 50 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}

}

Now i want a button to start the code above. Push button one time to start and push another time to end.

I'am a very beginner and dont't know how to do it. Can someone help me?

Hi, @omni_k1
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

It will show you how to post your code so it is in a scrolling window.

Tom... :grinning: :+1: :coffee: :australia:

Hi,
Are you building this in a simulator or in the real world with hardware?

Tom... :grinning: :+1: :coffee: :australia:

grafik

real :slight_smile:

You shouldn't supply the servo from the arduino.
The voltageregulator on the arduino can deliver a maximum of 500 mA. Even a small servo pulls more current at least under load and then your arduino will do a reset or the voltage-regulator gets overheated and destroyed.

I'm a friend of self-initated learning.
I will help you if you have made a frist own attempt with googling.

do three different google-searches with three different keyword-combinations.

and post the google-links

best regards Stefan

The arduino reset without condensators. I installed two 100nf and it works fine.

#include <Servo.h>

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

byte pos = 0;    // variable to store the servo position
boolean
servoMove = false, // move or not servo
s = true // servo direction
;
const byte button = 4; // button from pin 4 to GND

void setup ()
{
  pinMode ( button, INPUT_PULLUP );
  myservo.attach ( 9 );  // attaches the servo on pin 9 to the servo object
  // myservo.write ( 24 ); // initial position if needed
}

void loop ()
{
  if ( !digitalRead ( button ) )
  {
    delay ( 64 ); // debounce
    while ( !digitalRead ( button ) ){};
    servoMove = !servoMove;
  }
  
  if ( servoMove )
  {
    if ( s ) pos ++; else pos --;
    if ( pos > 49 ) s = false;
    if ( pos < 1 ) s = true;
    myservo.write ( pos );
    delay ( 16 );
  }
  else
  {
    // myservo.write ( 24 ); // stop position if needed
    delay ( 256 );
  }
}

1 Like

@flashko This could work. But not with the button from the starter kit. Now i know what to do. I need a self-locking push button. Thanks for the code :slight_smile: