How to control stepper motor and water pump with two buttons?

Hi, I am working on a project by pressing two buttons to start the stepper motor and a water pump. it seems like my code is wrong but I don't know how to write it correctly....
Below is the code I write (I only can control them separately, it is not working when I combine them together. Can anyone help me please@@

#include <ezButton.h> // include ezButton library
#include <ezOutput.h> // include ezOutput library


// Define pin connections & motor's steps per revolution
const int dirPin = 2;
const int stepPin = 3;
const int stepsPerRevolution = 200;
ezOutput pump(A5);    // create ezOutput object attached to pin A5
ezButton button1(12);  // create ezButton object attached to pin 12
ezButton button2(13);

void setup()
{
  // Declare pins as Outputs
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  Serial.begin(9600);
  button1.setDebounceTime(50); // set debounce time to 50 milliseconds
  button2.setDebounceTime(50);
  pump.low(); // turn pump off

}
void loop()
{
  if (button1.isPressed()) {
  pump.loop();   // MUST call the loop() function first
  button1.loop(); // MUST call the loop() function first
  button2.loop(); 

 Serial.println("Pump is started");
    pump.low();
    pump.pulse(10000); // turn on for 10000 milliseconds ~ 10 seconds
    // after 10 seconds, pump will be turned off by pump.loop() function

  }
  if (button2.isPressed()) {
  // Set motor direction clockwise
  digitalWrite(dirPin, LOW);
  // Spin motor slowly
  for(int x = 0; x < stepsPerRevolution; x++)
  {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(12000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(12000);
  }
  // Set motor direction clockwise
  digitalWrite(dirPin, LOW);
  // Spin motor quickly
  for(int x = 0; x < stepsPerRevolution; x++)
  {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(12000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(12000);
    

  }
delay(1000);
  }

}


once a button is pressed you are using delays to time the event which in effect stops of execution of the program for the specified time
have a look at using millis(), e.g. program which when button is pressed lights LED for 5 seconds

// DMU shield on UNO
// using millis() on button press light LED for 5 seconds

#define button 11  // button is on pin 11

void setup() {
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  delay(250);
}

void loop() {
  static long buttonTimer = 0;
  // if button pressed switch on LED for 5 seconds
  if (!digitalRead(button)) {
    Serial.println("LED ON");
    digitalWrite(LED_BUILTIN, HIGH);        // turn the LED on
    buttonTimer = millis();                 // start delay time
    while (!digitalRead(button)) delay(1);  //wait for button release
  }
  // if button timer is running delay 5 seconds then switch off LED
  if (buttonTimer)
    if (millis() - buttonTimer > 5000) {
      Serial.println("LED OFF");
      digitalWrite(LED_BUILTIN, LOW);  // turn the LED OFF
      buttonTimer = 0;                 // clear delay timer
    }
}

Some non-blocking timing tutorials:
Blink without delay().
Blink without delay detailed explanation
Beginner's guide to millis().
Several things at a time.

1 Like

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