#define BLYNK_TEMPLATE_ID "TMPL4VGhyHYlI"
#define BLYNK_TEMPLATE_NAME "stepper motor"
#define BLYNK_AUTH_TOKEN "ebPtwMGGWADPM5YJl0ElcYLZWgHhWiqD"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
char auth[] = BLYNK_AUTH_TOKEN; // Auth Token
char ssid[] = "Wokwi-GUEST"; // Nama hotspot yang digunakan
char pass[] = ""; // Password hotspot yang digunakan
const int dirPin = 4;
const int stepPin = 2;
const int stepsPerRevolution = 200;
BLYNK_WRITE(V0)
{
int speed = param.asInt(); // assigning incoming value from pin V1 to a variable
digitalWrite(dirPin, HIGH); // Set motor direction clockwise
// Spin motor slowly
for(int x = 0; x < stepsPerRevolution; x++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(speed);
digitalWrite(stepPin, LOW);
delayMicroseconds(speed);
}
Blynk.virtualWrite(V0, speed);
delay(1000); // Wait a second
Serial.println("forward 1");
// Set motor direction counterclockwise
digitalWrite(dirPin, LOW);
// Spin motor quickly
for(int x = 0; x < stepsPerRevolution; x++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(speed);
digitalWrite(stepPin, LOW);
delayMicroseconds(speed);
}
delay(1000); // Wait a second
Serial.println("backward 1");
Blynk.virtualWrite(V0, speed);
}
void setup()
{
Serial.begin(9600);
// Declare pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
Blynk.begin(auth, ssid, pass); // Memulai Blynk
}
void loop()
{
Blynk.run();
}