How to enable for loop with digital input

I am new to programming and want to use a digital input to activate a sweep routine for a servo using an Arduino UNO. When the input goes high the routine should start. Conversely, when the input goes low, the routine should stop. Is a for loop the best way to approach this or would something else like a while loop or if statements work better? Thanks.

I’d go with if statements, definitely.

Have you tried the Servo examples offered in the IDE?

You can find them in the File / Examples menu.

Also have a peek at File / Examples / 05.Control / IfStatementConditional.

What is it that your “a sweep routine” is meant to do?


Depends what this sweep is used for. If it is for learning the programming language, than you should follow the advice of alto777.
If you need that sweep for your project, and want an easy way to do that, then use a library like MobaTools, that can move your servo slowly.

Thanks alto. I have taken a look at the examples for the sweep routine in the IDE and was trying to get something working utilizing that. Unfortunately, it’s not working out so well which is why I started this chat. I am looking at starting the routine with a digital input and then having the servo go from one position to another continuously until the input is removed. For example going from 45 to 90 degrees back and forth. Thanks.

Thanks MicroBhaner. I appreciate the link to MobaTools also, I’ll check that out.

Since I am currently writing some examples for the next MobaTools version here is one with a sweeping servo.

#include <MobaTools.h>
/* Demo to move a servo slowly
   In this example a servo sweeps slowly between two positions as long as
   a button is pressed. If the button is released, it stops immediately and
   starts again from that position if the button is pressed again.
   The sketch does not block and can be extended to do other tasks.

// The button must be connected between pin and Gnd
const int buttonPin1 = A2;  //button1 Pin 2
const int servoPin   = 3 ;  // The servo is connected to this pin

// Position to sweep
const byte target1 = 30;
const byte target2 = 120;

bool buttonPressed;
byte targetPos = target1;    // actual targetposition for the servo

MoToServo myServo;

void setup() {
  pinMode(buttonPin1, INPUT_PULLUP);

  myServo.setSpeed( 5 );    // set speed of servo

void loop() {
  buttonPressed =  !digitalRead(buttonPin1); // 'ButtonPressed is 'true' if pin is LOW

  if (buttonPressed && not myServo.moving() ) {
    if ( == target1 ) targetPos = target2;
    if ( == target2 ) targetPos = target1;
    myServo.write(targetPos); //will move slowly

  if ( !buttonPressed ) myServo.write( );    // stop immediately

Edit: I changed my example a little bit :wink:

Here is a modification of the Servo->Sweep example that will do what you want. The added code is marked with “////”

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

const byte ButtonPin = 2; ////

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

void setup()
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  pinMode(ButtonPin, INPUT_PULLUP); ////

void loop()
  if (digitalRead(ButtonPin) == HIGH) ////
  { ////
    for (pos = 0; pos <= 180; pos += 1)   // goes from 0 degrees to 180 degrees
      if (digitalRead(ButtonPin) == LOW) ////
        return; ////

      // 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 = 180; pos >= 0; pos -= 1)   // goes from 180 degrees to 0 degrees
      if (digitalRead(ButtonPin) == LOW) ////
        return; ////

      myservo.write(pos);              // tell servo to go to position in variable 'pos'
      delay(15);                       // waits 15ms for the servo to reach the position
  } ////

I hear you. So you have been able to get a servo working with the examples programs exactly as they are written and now you want to do something else.

The fact that you are asking about if, for and while suggests that you may be working beyond your current abilities.

Usually we would ask for details about what you are trying to do, some or all of which you may have supplied, and your current efforts, as in code that doesn’t work or doesn’t do quite what you want.

You running before you can walk, maybe even before you have your shoes on.


I am far from an expert but this was most helpful. Thank you.

This was also, most helpful. I am working on testing this soon. I will load both programs so I can familiarize myself with each approach. Thank you.

I agree with you alto. I have done some reading and purchased some components to test with. Baby steps…

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.