Esp8266 Servo LightSwitch Neutral?

Would a delay in the code work to reset my servo controlled light switch back to a neutral position? Right now I have a Switch/Button in the Blynk app set to various values to switch the light switch at home to on or off. I want to have it reset to a neutral position after the button is switched to on or off.

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>

#include <BlynkSimpleEsp8266.h>

#include <Servo.h>

// You should get Auth Token in the Blynk App.

// Go to the Project Settings (nut icon).

char auth[] = "YourAuthToken";

// Your WiFi credentials.

// Set password to "" for open networks.

char ssid[] = "YourNetworkName";

char pass[] = "YourPassword";

Servo servo;

BLYNK_WRITE(V3) {

servo.write(param.asInt());
#these two lines are my edits delay(500);
#servo.write(60);

}

void setup() {

// Debug console Serial.begin(115200);

Blynk.begin(auth, ssid, pass);

// You can also specify server:

//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);

//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);

servo.attach(15); // 15 means D8 pin of ESP8266

}

void loop()

{

Blynk.run(); // You can inject your own code or combine it with other sketches.

}

1 Like

Yes, a simple timeout delay that activates/deactivates your lights and then. after a short delay, moves the servo to neutral will work fine.

BlynkTimer timer;  // Place in the beginning of the code with the defines

BLYNK_WRITE(V3) {
servo.write(param.asInt());
timer.setTimeout(2000L, ServoNeutral);  // In two seconds, Run the void ServoNeutral() routine.
}

void ServoNeutral() {
servo.write(60);
}

timer.run();  // Place in the main void loop
1 Like

Thank you so much! It works and centers back so I can manually control it without the servo fighting me haha.

1 Like

The engineering part is :ok_hand::ok_hand::ok_hand: