Noob question (ezbutton + accelstepper)

Hi!

Im playing with ezButton library and AccelStepper library to make a motor move to different predeterminate places (if i press button1 go to x position while doing something).

the question now is why i cant put an AND inside an If to check that 2 buttons are pressed, when i do that the code don't run

  if (medida1.isPressed() && start.isPressed())
      stepper.moveTo(Park * 20);

if i use the same code but with only one of the buttons, is working.

There is my full code (sorry for the coments in spanish xD)

#include <AccelStepper.h>
#include <ezButton.h>

#define MAX_POSITION 0x7FFFFFFF // maximum of position
#define DEG_PER_STEP 1.8
#define STEP_PER_REVOLUTION (360 / DEG_PER_STEP)

//Motor///////////////////////
AccelStepper stepper(AccelStepper::FULL2WIRE, 47, 46);

long Park = STEP_PER_REVOLUTION;
long go = MAX_POSITION;

//Botones/////////////////////
ezButton homeS(A1);
ezButton limitS(A0);
ezButton start(8);
ezButton medida1 (9);
ezButton medida2 (10);
ezButton medida3 (11);
ezButton medida4 (12);

//Bombas accionadas por aire///
const int uno_cuchillo_up = 22;
const int dos_rueda_central_up = 23;
const int tres_rueda_doble_up = 24;
const int cuatro_cuchillo_dwn = 25;
const int seis_rueda_central_dwn = 26;
const int siete_sosten = 27;
const int ocho_separacion = 28;
const int nueve_succion = 29;
const int diez_agarre = 30;
const int vacio = 3;
const int buzzer = 4;
const int pistonH = 44;
const int pistonV = 45;
const int lamp = 46;

//FIN DECLARACIONES////////////
///////////////////////////////

void setup() {
  Serial.begin(9600);

//Debouncing para botones/////
  homeS.setDebounceTime(50);
  limitS.setDebounceTime(50);
  start.setDebounceTime(50);
  medida1.setDebounceTime(50);
  medida2.setDebounceTime(50);
  medida3.setDebounceTime(50);
  medida4.setDebounceTime(50);

//defino pines como salida/////
  pinMode(uno_cuchillo_up, OUTPUT);
  pinMode(dos_rueda_central_up, OUTPUT);
  pinMode(tres_rueda_doble_up, OUTPUT);
  pinMode(cuatro_cuchillo_dwn, OUTPUT);
  pinMode(seis_rueda_central_dwn, OUTPUT);
  pinMode(siete_sosten, OUTPUT);
  pinMode(ocho_separacion, OUTPUT);
  pinMode(nueve_succion, OUTPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(vacio, OUTPUT);
  pinMode (pistonH, OUTPUT);
  pinMode (pistonV, OUTPUT);
  pinMode (lamp, OUTPUT);

//Seteo de velocidad motor////
  stepper.setMaxSpeed (1200);
  stepper.setAcceleration(1500.0); 
  stepper.setCurrentPosition(0);  

//Motor busca Home////////////
  go = -1 * go;       // cambia la direccion a CCW (para buscar el home)
  stepper.moveTo(go); // mueve el motor hasta el infinito y mas alla (home)
}

void loop() {
  homeS.loop(); // loop para botones y switchs
  limitS.loop();
  start.loop();
  medida1.loop();
  medida2.loop();
  medida3.loop();
  medida4.loop();
  
///////////////////////////////////////////////////////////

  if (homeS.isPressed())
    if (stepper.distanceToGo() != 0) {
      stepper.setCurrentPosition(0); // position to 0
      go = -1 * go; // cambia de nuevo a CW
      stepper.moveTo(Park); // move motor one revolution
    }
    
/*
  if (limitS.isPressed())      
    if (stepper.distanceToGo() != 0) {
      stepper.setCurrentPosition(0); // reset position to 0
      go = -1 * go; // reverse direction
      delay (3000);
      stepper.moveTo((Park * 59)* -1);
      }
*/


  if (medida1.isPressed()) && start.isPressed())
      stepper.moveTo(Park * 20);
    

    
//////////////////////////////////////////////////////////
  stepper.run(); // MUST be called as frequently as possible
}

Thx for the help :slight_smile:

does ezButton support the detection of multiple simultaneous button presses?

then there's the need to defer recognition of a single button in order to allow time (small fraction of a second) for a 2nd button press?

does "isPressed" mean that a button is being held down or is it the singular event of a button that was not pressed becoming pressed?

i'm really not sure, didn't find anything on their reference page

You mean debouncing?

That i know, isPressed is a singular event.

Did you know some library for buttons that support multiple simultaneous button presses??

No I don't think that's what @gcjr means. If you want to see if two buttons are newly pressed, and can't check them both at exactly the same instant, then when either of them becomes pressed you need to start the clock, then look for the other button to become pressed. If that happens within a certain time, then that fits the bill as simultaneous.

i would write my own code, it's not very much. it would need to defer reporting a single button press to allow recognition of two presses before a single button press is reported

how do you prevent (defer) recognizing one of the two buttons being pressed and processed?

oh, wow...
i was trying by all means to not use mills and timers :confused: because i get messy and dont really understand how to.. i tried couple of times but is something that resist me

yeah, i wish i knew enough to do it, or have the time to :expressionless:

Now if i put a small delay between the pressing of the buttons is almost working,..
Just ingoring the state of one of the buttons (medida1)

   if (medida1.isPressed()){
        delay(10);
        if (start.isPressed())
     stepper.moveTo(Park * 10);
   }

Even when the medida1 is not pressed, if i press start the motor is moving :angry:

As you can see im almost a complete newie.
its not clear to me if inside a IF i can ask another IF

i mean, if button1 is on, whait 1 second, check if the button 2 is on, and if both of the buttons are on, move the motor

why do you need to detect two simultaneous button presses?

to be able to do this, no?

  if (medida1.isPressed() && start.isPressed())
      stepper.moveTo(Park * 20);

if not, i don't understand why is not working

yes, but why do you need to use 2 buttons to do that? why not just one button?

ezButtons's methods .isPressed and .isReleased are badly named: they check for a button having just become pressed.

Here's what may be happening. First time through loop(), when ezButton.loop() runs, it will check the state of the buttons and perhaps medida1.isPressed() is true and the start.isPressed() is not. Then it zooms through to the top of loop again, re-checks ezButton.loop. So now, medida.isPressed will be false, since it isn't newly pressed, it's still pressed from last time. Maybe start.isPressed() is true, but it doesn't matter: the other one isn't (any more) so the test with the && fails.

I think you need to check whether both buttons are pressed, not newly pressed (which is what ezButton's badly named "is"Pressed tells you).

ah, because it will have more than one place where i want to send the motor

  if (medida1.isPressed() && start.isPressed())
      stepper.moveTo(Park * 20);

  if (medida2.isPressed() && start.isPressed())
      stepper.moveTo(Park * 40);

  if (medida3.isPressed() && start.isPressed())
      stepper.moveTo(Park * 60);

etc..

the idea is to select with a switch which parking place i want to put the motor, and then press Start button to make it go there

it's sounds logical
but..
if i comment the .loop of medida1 and/or start doesn't work neither

No, you can't do that, it needs to run its loop() methods every time through (Arduino) loop().

You're using the wrong ezButton method I think. Try the .getState() method which returns the steady (debounced) state of the button. (Internally, it uses that state in its test for the so-called "is".Pressed.)

Here's the ezButton source code.

Btw have you got a pullup resistor on each button or what? ezButton caters for the built in pullups as you will see in the source, and it then does an internal pinMode() with the pullups enabled if that's the mode you choose.

I was thinking to to use the getState and use the if to check that they are low or high

No, i dont. i read in the ezButton use the internal one, so i didn't care to check

if no actions are taken when the medida buttons are pressed, recognize which of those buttons is pressed and then move the motor and the start button is pressed

Yes, its what i want to do xD

with my null knowledge im trying this

if (medida1.isPressed()){
        delay(10);
        if (start.isPressed())
     stepper.moveTo(Park * 10);
   }

but is not working...

how about

    if (start.isPressed ())
      stepper.moveTo (pos);

    else if (medida1.isPressed ())
        pos = Park * 20;

    else if (medida1.isPressed ())
        pos = Park * 40;

    else if (medida1.isPressed ())
        pos = Park * 60;

1 Like

Its working :slight_smile:

But, when i start to put another things that need to be started with the Start button i have problems. i mean the motor is waiting for me to press the start, but all the other things that i have not lol

I understand why is doing it, but i cant think of another way to do it :confused:

  if (start.isPressed ()){
      stepper.moveTo (pos);
  }
  else if (medida1.isPressed ()){
        pos = Park * 5;
        delay (2000);
        digitalWrite (dos_rueda_central_up, HIGH);
  }   
/*  else if (medida2.isPressed ()){
        pos = Park * 40;
  }    
  else if (medida3.isPressed ()){
        pos = Park * 60;
  }*/

Here the digitalWrite is going High as soon as i push the medida1 button.
How can i do to make all the digitalWrite and delays hold on until i push the start button?

btw, i really appreciate your and @ shetland_siren help. Thanks very much :heart: