#include <Servo.h>
#define BLYNK_TEMPLATE_ID "TMPL6zo_j5NI1"
#define BLYNK_TEMPLATE_NAME "Servo motor control with nodemcu"
#define BLYNK_AUTH_TOKEN "LnVZcVKsYJAqZpFuEElTjpZjgw40prt4"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
Servo motor1;
//Servo motor_2;
int led=4;
void cradle() {
//you begin your own personal code for servo here
int pos;
for (pos = 40; pos <= 140; pos ++) { // goes from 40 degrees to 140 degrees
// in steps of 1 degree
motor1.write(pos); // tell servo to go to position in variable 'pos'
delay(10); // waits 15ms for the servo to reach the position
}
for (pos = 140; pos >=40; pos --) { // goes from 140 degrees to 40 degrees
motor1.write(pos); // tell servo to go to position in variable 'pos'
delay(10); // waits 15ms for the servo to reach the position
}
//your personal code for servo should end here
}
//bool status = false;
BLYNK_WRITE(V0)
{
int pinValue = param.asInt();
if (pinValue == 1) { // if Button sends 1
Serial.println("servo and led is on");
digitalWrite(led, HIGH);
cradle(); // start the function cradle
Blynk.run(); // Run rest of show in-between waiting for this loop to repeat or quit.
int pinValue = 0; // Set V0 status to 0 to quit, unless button is still pushed (as per below)
Blynk.syncVirtual(V0); // ...Then force BLYNK_WRITE(V0) function check of button status to determine if repeating or done.
}
else{
Serial.println("servo and led is off");
digitalWrite(led, LOW);
}
}
void setup()
{
Serial.begin(115200);
pinMode(led, OUTPUT);
motor1.attach(21);
Blynk.begin(auth, ssid, pass);
}
void loop() {
Blynk.run();
}