(SOLVED) Looking to control a servo motor with slider

hello may I ask something @Costas

I also try this example for manual/automatic mode. But the monitor still not working, it is does not display anything, i do not know why is it.
Another is that, for my example during manual mode i want to control the servo motor using slider widget, but i do not know how the code to perform it, can anyone show me based on my code below. Because i want to control servo automatically and manually.
here is my code

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <LiquidCrystal.h>
#include <Servo.h> 

//Monitor
WidgetTerminal terminal(V1);
LiquidCrystal lcd(14, 12, 13, 15, 02, 00);

#include "EmonLib.h"
// Include Emon Library
EnergyMonitor emon1;
// Create an instance

//SERVOMOTOR//
int servoPin1 = 05;
Servo Servo1;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "blabla";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "@@@";
char pass[] = "#####";

SimpleTimer timer;
int selectMode;
int LED1=04;

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);

  pinMode(LED1, OUTPUT);
  pinMode(servoPin1, OUTPUT);

  //SERVOMOTOR//
  Servo1.attach(servoPin1);
  Servo1.write(150); //initialize servo motor at 90 degree

  timer.setInterval(5000L, runfunction);
  
  emon1.current(0, 50.1);             // Current: input pin, calibration.
}

void runfunction()
{
   if (selectMode == 1) //If button MANUAL on app
 { 
   terminal.print("Manual mode :");
   terminal.flush();
   lcd.clear();
   lcd.print("Manual Mode");
   digitalWrite(LED1,LOW);

 }
   else 
 {
   terminal.print("Automatic mode :");
   terminal.flush();
   lcd.clear();
   lcd.print("Automatic Mode");

   double Irms_1st = emon1.calcIrms(1480);  // Calculate Irms only
   Serial.print("1st= ");
   Serial.print(Irms_1st*230.0);           // Apparent power
   Serial.print(",");
   Serial.println(Irms_1st);             // Irms
  
   Servo1.write(0);
   Servo1.write(90);
   digitalWrite(LED1,HIGH);
 }

}

BLYNK_WRITE(V2)
 {
  selectMode = param.asInt();
  runfunction;
  terminal.print("button pressed");
  terminal.flush();
 }

BLYNK_WRITE(V3)
{
  Servo1.write(param.asInt());
}

//loop
void loop()
{
  Blynk.run();
  timer.run();
}

As I stated before, we are not here to write your code for you… you will need to learn how to code for Blynk on your own, but we will help with specific issues as you learn.

For example, controlling a servo motor from a slider is very easy…

BLYNK_WRITE(Vx) // Slider set to Vx and to range 0-180
{
  int SrvoPos = param.asInt(); // get slider value
  Servo1.write(SrvoPos); // move servo to value
}

But not sure what you mean by automatic mode… you want the servo to just keep moving back and forth or based on some other form of sensor input?

I am so sorry for trouble you with my question, I have already solve the problem and thanks to your advice before, i tried and learn based on topic that had been discuss before.
once again i am sorry for trouble you with my question.

Excuse me i want to ask something, is it possible to control servo using slider widget only during manual mode not during the automatic mode…but during the automatic mode the servo motor can move freely and if we control the servo using slider widget it is does not move…the slider widget only can be use during the manual mode…
Is it possible to do it?
Can anyone help me.

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <LiquidCrystal.h>
#include <Servo.h> 

//Monitor
LiquidCrystal lcd(14, 12, 13, 15, 02, 00);

#include "EmonLib.h"
// Include Emon Library
EnergyMonitor emon1;
// Create an instance

//SERVOMOTOR//
int servoPin1 = 05;
Servo Servo1;
int Servopos;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "blablabla";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "hi";
char pass[] = "hi";

SimpleTimer timer;
int selectMode;
int LED1=04;

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);

  timer.setInterval(5000L, runfunction);

  pinMode(LED1, OUTPUT);

  //SERVOMOTOR//
  Servo1.attach(servoPin1);
  Servo1.write(150); //initialize servo motor at 90 degree

  emon1.current(0, 50.1);             // Current: input pin, calibration.

}

void runfunction()
{
  Blynk.virtualWrite(V0, " SMART POWER");
  Blynk.virtualWrite(V1, " RECOVERY SYSTEM");
   if (selectMode == 1) //If button MANUAL on app
 { 
   Blynk.virtualWrite(V0, "Status:");
   Blynk.virtualWrite(V1, "Manual Mode");
   lcd.clear();
   lcd.print("Manual Mode");
   digitalWrite(LED1,LOW);
   Servo1.write(Servopos);

 }
   else 
 {
   Blynk.virtualWrite(V0, "Status:");
   Blynk.virtualWrite(V1, "Automatic Mode");
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("Automatic Mode");

   Servo1.write(0);
   delay(1200);
   Servo1.write(90);
   delay (2000);
   digitalWrite(LED1,HIGH);
 }

}

BLYNK_WRITE(V4)
 {
  selectMode = param.asInt();
  runfunction;

 }

BLYNK_WRITE(V5)
{
  int Servopos = param.asInt();
  Servo1.write(Servopos);
}

//loop
void loop()
{
  Blynk.run();
  timer.run();
}

Servos are not really ment to “move freely” due to possible gear damage from manually turning the servo arm. They are designed to hold the position you place them in.

But you can try the Arduino command Servo Detach linked to a Blynk button (in switch mode) to attach and detach a specific servo. https://www.arduino.cc/en/Reference/ServoDetach This should disconnect the control pin (thus the active holding power) from the servo.

BLYNK_WRITE(Vx) // Button Widget (in switch mode) for Servo1 Attach/Detach Function
{
  SrvoButtonState = param.asInt();
  if (SrvoButtonState == 0) {
    Servo1.detach();
  } else if (SrvoButtonState == 1) {
    Servo1.attach(servoPin1);
  } // END else if
} // END Blynk Function

I have even had some results with integrating the attach/detach commands into the actual slider control function… however a short delay is required before detaching it, otherwise it just detaches before moving; Too short and the servo stutters or may not achieve its destination, too long and risk disconnect issues with Blynk.

BLYNK_WRITE(Vx) // Slider set to Vx and to range 0-180
  myservo.attach(Servo1);   // Attach Servo
  SrvoPos = param.asInt();  // Get slider value
  Servo1.write(SrvoPos);  // Move servo to value
  delay(500);  // Set minimum delay for servo to reach desired position, keep as low as possible.
  Servo1.detach();   // Detach Servo - NOTE servo pin designation not required for detach
} // END Blynk Function

I already able to control the servo manually and automatically using the detach and attach code…but disconnect issues with Blynk always happen…so how do i counter that because of course i will use delay for the servo…

That is the million dollar question. Sorry, but there is no single answer. Anything requiring constant communication and/or timing in order to maintain functionality will require both smart code and compromises.

If you must detatch a servo then avoid mixing it in ways that require delays, like my slider example… rather use the prior example to enable manual servo mode, manually, with a separate function call.

And avoid long delays like this… create separate timers if you need to move the servo back and forth.

Good to hear. I will consider this topic solved.

so in order to face less problem with disconnect issues with blynk, i need to use less delay? is it right?

Yes. http://docs.blynk.cc/#blynk-main-operations-limitations-and-recommendations

Thanks for your help @Gunner, you already have solve my problem…thanks again

which part did you initialize “myservo” ???

@Mus_Sopee This is an old topic and I don’t really understand your question anyhow. Please look up how the servo library commands works here…

https://www.arduino.cc/en/Reference/Servo

https://www.arduino.cc/en/Reference/ServoAttach