#define BLYNK_TEMPLATE_ID "TMPL6cHuewr1n"
#define BLYNK_TEMPLATE_NAME "Motor Stepper"
#define BLYNK_AUTH_TOKEN "K-5nCQW43G5HRyENFbm2DrndI1lI1wBR"
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <Stepper.h>
// Set up for Stepper motors
float stepmax = 600;
Stepper Stepper1(stepmax, 12, 14, 27, 26);
Stepper Stepper2(stepmax, 21, 19, 18, 5);
float steptarget1 = 500;
float steptarget2 = 300;
float deltaT = 5;
float speed1;
float speed2;
// WiFi credentials
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST"; // WiFi SSID
char pass[] = ""; // WiFi Password (blank for Wokwi)
BlynkTimer timer;
void setup() {
// Serial initialization for debugging
Serial.begin(115200);
// Blynk initialization
Blynk.begin(auth, ssid, pass);
// Calculate the initial speed of the stepper motors
speed1 = ((steptarget1 * 60) / (deltaT * stepmax));
speed2 = ((steptarget2 * 60) / (deltaT * stepmax));
// Set speed of the motors
Stepper1.setSpeed(speed1);
Stepper2.setSpeed(speed2);
// Print initial speed for verification
Serial.println(speed2);
// Set timer to update stepper motors control from Blynk every second
timer.setInterval(1000L, updateMotors);
}
// Function to control stepper motors based on Blynk input
void updateMotors() {
Stepper1.step(steptarget1);
Stepper2.step(steptarget2);
// Sending data to Blynk for monitoring
Blynk.virtualWrite(V1, steptarget1); // Target step 1 to Blynk
Blynk.virtualWrite(V2, steptarget2); // Target step 2 to Blynk
}
// Blynk handler to receive target step updates from sliders
BLYNK_WRITE(V3) {
steptarget1 = param.asFloat(); // Receive value from Blynk slider for stepper 1
Stepper1.setSpeed((steptarget1 * 60) / (deltaT * stepmax));
}
BLYNK_WRITE(V4) {
steptarget2 = param.asFloat(); // Receive value from Blynk slider for stepper 2
Stepper2.setSpeed((steptarget2 * 60) / (deltaT * stepmax));
}
void loop() {
// Run Blynk for updating the control interface
Blynk.run();
// Run timer for periodic tasks
timer.run();
}