Press Button to Begin Code Once

I am a beginner at coding. I am wanting to press a button and my Code begins. I have been running the following code. It works when the button is pressed the first time but just continues on loop. Has anyone got a better code for this??

Code is:

#include <ezButton.h>

ezButton button(7);  // create ezButton object that attach to pin 7;

#include <Servo.h>
Servo myservo1; 
Servo myservo2;
Servo myservo3; // create servo object to control a servo 
                // twelve servo objects can be created on most boards

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

void setup() {
  button.setDebounceTime(50); // set debounce time to 50 milliseconds

  while (!button.isPressed())
    button.loop(); // MUST call the loop() function first
 { 
  
  myservo1.attach(9);
  myservo2.attach(6);
  myservo3.attach(5); // attaches the servo on pin 9 to the servo object 
} 
}

void loop() {
 { 
  myservo1.write(180);             
    delay(2000); ////stops motor for 8 seconds 
  for(pos = 180; pos>=0; pos-=1)// goes from 180 degrees to 0 degrees
  myservo2.write(180);             
    delay(2000); ////stops motor for 8 seconds 
  for(pos = 180; pos>=0; pos-=1)// goes from 180 degrees to 0 degrees
    myservo3.write(180);             
    delay(2000); ////stops motor for 8 seconds 
  for(pos = 180; pos>=0; pos-=1)// goes from 180 degrees to 0 degrees
  {                                
    myservo1.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(5); // waits 15ms for the servo to reach the position 
    myservo2.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(10); // waits 15ms for the servo to reach the position 
    myservo3.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(5); // waits 15ms for the servo to reach the position 

  } 

    myservo1.write(0);              
    delay(4000); //stops motor for 4 seconds   
  for(pos = 0; pos <= 180; pos +=1) // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo1.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);      // waits 15ms for the servo to reach the position

  } 

} 
}

Move the code from loop() to setup(). Wire the button as a reset button. The code will run once, each time the button is pressed.

Thank you,
I'm having trouble with moving the code to the setup from the loop. it keeps coming up with an error.

The error is a secret?

What do I change the value of 7 to? As the wire going to 7 is now in Reset.

ezButton button(7);  // create ezButton object that attach to pin 7;

What error are you getting?

I've managed to work through the errors it was just } in the wrong places.

But the reset is not working.

The button is connected to reset power and ground.

The button works once when wiring to 7 and pressing but only on the first time.

Change

to
//ezButton button(7); as well as any other references to a button press.

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