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
}
}
}