Hi,
I have been working on a project where I want a motor servo (classic SG90) to swipe as soon as a virtual pin is pushed, for 5 seconds (with a 1sec pause between each swipe) and then stop.
Somewhat the ESP is deconnecting from the network before the action is completed. Sometimes later sometimes sooner than the full 5 sec. What am I doing wrong ?
Servo is plugged on external power supply 5V - ESP8266 through USB (Sharing same Ground)
I am loosing my mind, I have tried different option - I donβt want to use delays as I would like to add up LED blinking afterwards.
Thanks a lot
//Power saving code version
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Servo.h>
Servo myservo;
int pos = 2000; // variable to store the servo position
unsigned long previousMillis = 0;
const long actionDuration = 5000; // duration of led and servo function
const long pauseServo = 1000;
const long onDuration = 1000; // led ON Duration
const long offDuration = 1000; // led OFF Duration
//Variable
#define LED 4 //D2
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "XXXXXXXXXXXX";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "XXXXXXXXX";
char pass[] = "XXXXXXXXXXXX";
void setup()
{
// Debug console
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
pinMode (LED, OUTPUT);
myservo.attach(14); // attaches the servo on pin 1 to the servo object
myservo.writeMicroseconds(pos);
}
void loop()
{
Blynk.run();
}
// Switch ON 60seconds and 0FF (1 time)
BLYNK_WRITE(V1)
{
int buttonState = param.asInt();
if (buttonState == 1) { //button is pressed AND this is the first digitalRead() that the button is pressed
Serial.println("button is pressed");
unsigned long startTime = millis();
while ((millis() - startTime) < actionDuration) // Run this for 5 second
{ // trigger action
servo();
}
}
}
void servo()
{
if (pos == 2000)
{
if ((millis() - previousMillis) >= pauseServo) // Pause 1 sec
{
pos = 1500; // change the POS
myservo.writeMicroseconds(pos);
previousMillis = millis(); // remember Current millis() time
}
}
else
{
if ((millis() - previousMillis) >= pauseServo) // Pause 1 sec
{
pos = 2000; // Change the POS
myservo.writeMicroseconds(pos);
previousMillis = millis(); // remember Current millis() time
}
}
}