#include <BlynkSimpleEsp32.h>
#include <AccelStepper.h>
// Blynk authentication token
char auth[] = "YOUR_AUTH_TOKEN";
// Define the number of steps per revolution for your stepper motor
const int stepsPerRevolution = 200;
// Initialize the stepper motor object with the number of steps and the pin numbers
Stepper stepper(stepsPerRevolution, 8, 9, 10, 11);
// Flag to indicate if the motor is currently running
bool isMotorRunning = false;
void setup() {
// Initialize the serial communication for debugging
Serial.begin(9600);
// Connect to the Blynk server
Blynk.begin(auth);
// Set the motor speed
stepper.setSpeed(60); // Set the initial speed in RPM
}
void loop() {
// Run the Blynk background tasks
Blynk.run();
// If the motor is running and the Blynk app is disconnected, stop the motor
if (isMotorRunning && !Blynk.connected()) {
stopMotor();
}
}
// Function to start the motor
void startMotor() {
Serial.println("Starting motor");
stepper.step(stepsPerRevolution);
isMotorRunning = true;
}
// Function to stop the motor
void stopMotor() {
Serial.println("Stopping motor");
stepper.step(-stepsPerRevolution);
isMotorRunning = false;
}
// Blynk button callback function
BLYNK_WRITE(V1) {
int buttonState = param.asInt();
// If the button is pressed, start the motor
if (buttonState == HIGH) {
startMotor();
}
// If the button is released, stop the motor
else {
stopMotor();
}
}