Hi
Im very new to all this and green.
At the moment the delay is blocking the second button press.
I would like to be able to press both buttons at any time instead of wait.
I looked up millis() instead for delay but don't understand how to write it.
could someone help me write the code below to have the same moment and wait without blocking.
many thanks
Jason
#include <ezButton.h>
#include <Servo.h>
ezButton buttonleft(7);
ezButton buttonright(8); // create ezButton object that attach to pin 7;
Servo servoleft;
Servo servoright;
int dt=50;
int pos = 50;
void setup() {
servoleft.attach(9);
servoright.attach(10);
Serial.begin(9600);
servoleft.write(87);
servoright.write(90);
}
void loop() {
buttonleft.loop();
buttonright.loop();// MUST call the loop() function first
delay(dt);
if(buttonright.isPressed()){
Serial.println("The button left is pressed");
// Delay just means WAIT! Remember every
// 100 = 0.1 second!
delay(100);
servoleft.write(130); // Move servo to 180 degrees!
delay(1000); // Wait 1 second
for (pos = 130; pos >= 87; pos -= 1) {/* goes from 0 to 70
degrees in steps
of 1 degree */
servoleft.write(pos); // go to position 'pos'
delay(5); // waits 5ms
}
}
if(buttonleft.isPressed()){
Serial.println("The button right is pressed ");
// Delay just means WAIT! Remember every
// 100 = 0.1 second!
delay(100);
servoright.write(40); // Move servo to 180 degrees!
delay(500); // Wait 1 second
for (pos = 40; pos <= 90; pos += 1) { /* goes from 0 to 70
degrees in steps
of 1 degree */
servoright.write(pos); // go to position 'pos'
delay(5); /* waits 5ms
}
}
}
That's the easy stuff to change but changing to non blocking timing using millis() is going to need bigger changes. All of the delay()s need to go, even assuming that they were needed in the first place
I assume that the delay()sin the servo movement for loops are to slow down the servo movement but what about the others ?
I feel that I should point out that your code as posted does not compile
Thanks
I copied and pasted bits so probably missed something.
I’m a magician and have been using it to make a sign fall in my show (left side then the right). It works but would like it to drop both together as a finale.
I think I could make it works with number of buttons presses which might be easier and then would only need 1 button I think?
It’s first side drops
I put it back
Then first side drops again
This time I put it back and hold it.
Then second side drops
I put it back walk away
Then they both drop.
The buttons are link to a remote with 2 channels
But at the moment I can’t get them both to drop at the same time. Because of the delay
So will try to figure out the button number count and do it that way.