Stop a servo using a button?

Dad got me an UNO kit for Christmas, it is pretty good fun, just trying my own ideas now and i’ve come to a halt…

Perhaps my code is a bit off but i seem to be having real trouble with this!

#include <Servo.h>
Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created
int pos = 0;    // variable to store the servo position
int sensorPin = 0;    // select the input pin for the potentiometer
int serPin = 9;      // select the pin for the servo
int sensorValue = 0;  // variable to store the value coming from the sensor
int inputPin1 = 2; // button 1
int inputPin2 = 3; // button 2
void setup() {
  // declare the servo pin as an OUTPUT:
  pinMode(serPin, OUTPUT);  
  pinMode(inputPin1, INPUT); // make button 1 an input
  pinMode(inputPin2, INPUT); // make button 2 an input
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
void loop(){
  if (digitalRead(inputPin1) == LOW) {
    SerLoop(); // turn servo loop on
  } else if (digitalRead(inputPin2) == LOW) {
    myservo.detach(); // turn servo off

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

Basically i am controlling the swing speed of the servo using a pot (which is working perfectly :)) and i have two buttons, one which runs the servo loop and another that stops the servo.

I have tried numerous methods for stopping the servo but none of them seem to work.

Is it because once i start the serloop it stops the normal loop from working?

Also, while on the subject of servos, it makes it very hard to upload new code to the UNO, it says the com port is in use, i have to close the code editor and unplug the uno then try again. All the other components work fine, anyone know if this is just a thing or is there a way of fixing it?

Thanks for any help!

I don't think the issue is his SerLoop function, since this in itself is not looping.

I think if "Serial.println("Pin is Low");" was added after:

if (digitalRead(inputPin1) == LOW) {

You'll see the problem.

I think inputPin1 is being held low..

How did You hook up the buttons ?

Exactly the same as the button example

During the execution of SerLoop, the state of the switch is not read, so expecting that pressing the switch, while the servo is moving, will have some effect is an unreasonable expectation.

Are you holding the switch down long enough? The servo must complete an entire sweep (both directions) before the switches are read again.

This could be a problem, too:

    delay(sensorValue/50);                       // waits 15ms for the servo to reach the position

How long will the delay last if the value in sensorVaue is less than 50? The delay() function really does not like being called with a value of 0.