servo with button

Hello, I have taken the “sweep servo” sketch and tried to add the button sketch. The code is not complete if someone can help me structure where things go because I have tried several different ways and cannot compile.
I want to when i press and release button the servo starts sweeping left to right until I press and release the button again then it stops. Here is my code…

/* Sweep
  by BARRAGAN <http://barraganstudio.com>
  This example code is in the public domain.

  modified 28 May 2015
  by Michael C. Miller
  modified 8 Nov 2013
  by Scott Fitzgerald

  http://arduino.cc/en/Tutorial/Sweep
*/

#include <Servo.h>
const int buttonPin = 2;
int buttonState = 0;

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


void setup() {
 
  pinMode(buttonPin,INPUT);
  myservo.attach(2);  // attaches the servo on GIO2 to the servo object
}

void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
   
  } else 
  
  
  int pos;

  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(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 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
  }
}

I want to when i press and release button the servo starts sweeping left to right until I press and release the button again then it stops.

Start by looking a the StateChangeDetection example in the IDE

  pinMode(buttonPin,INPUT);

How is your button sewn on?

INPUT_PULLUP makes wiring switches much easier.

PaulS:

  pinMode(buttonPin,INPUT);

How is your button sewn on?

INPUT_PULLUP makes wiring switches much easier.

Thank you for the reply, i have got the button pulled low through 10k resistor and when the button is pressed it is connected to 5v.
Is that what your meant?

Is that what your meant?

That is what I wanted to know, when I asked how your button was sewn on.

You have a push-button type of switch. The type is COMPLETELY irrelevant. What you have is a switch, not a button.

I have tried several different ways and cannot compile.

You need to show us ONE way, and explain why you can't perform the simple action of clicking the Verify icon.

If you CAN do that, but the compiler is telling you that your code is crap, I'm relatively certain that it does so in polite terms, and I am 100% certain that you need to share the compiler output with us.

Arduino: 1.8.9 (Windows Store 1.8.21.0) (Windows 10), Board: "NodeMCU 0.9 (ESP-12 Module), 80 MHz, Flash, Disabled, 4M (no SPIFFS), v2 Lower Memory, Disabled, None, Only Sketch, 115200"

C:\Users\tjone\AppData\Local\Temp\arduino_modified_sketch_658529\Button.ino: In function 'void loop()':

Button:36:8: error: 'pos' was not declared in this scope

   for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees

        ^

Button:41:8: error: 'pos' was not declared in this scope

   for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees

        ^

Multiple libraries were found for "Servo.h"
 Used: C:\Users\tjone\OneDrive\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.5.0\libraries\Servo
 Not used: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\Servo
exit status 1
'pos' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
 } else

"else" without curly brackets, needed to define the code block to be executed.

jremington:

 } else

"else" without curly brackets, needed to define the code block to be executed.

thats the thing, i dont know what to put. thanks for the reply

Arduino: 1.8.9 (Windows Store 1.8.21.0) (Windows 10), Board: "NodeMCU 0.9 (ESP-12 Module), 80 MHz, Flash, Disabled, 4M (no SPIFFS), v2 Lower Memory, Disabled, None, Only Sketch, 115200"

C:\Users\tjone\AppData\Local\Temp\arduino_modified_sketch_658529\Button.ino: In function 'void loop()':

Button:36:8: error: 'pos' was not declared in this scope

   for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees

        ^

Button:41:8: error: 'pos' was not declared in this scope

   for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees

        ^

Multiple libraries were found for "Servo.h"
 Used: C:\Users\tjone\OneDrive\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.5.0\libraries\Servo
 Not used: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\Servo
exit status 1
'pos' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Where do you declare pos in your program ?