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