BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Servo Speed


#1

Hi I need help please.
Im using bynk app to controll 2 Servos. But I want them to run with diffrent speed.
does anyone have a simple solution?

here is my working code so far:

#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[] = "xxx";

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

Servo servo;

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

Servo servo2;

BLYNK_WRITE(V4)
{
  servo2.write(param.asInt());

}

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);

  servo.attach(4);
  servo2.attach(5);
}

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

#2

controlling servo speed is really crude system i think. Your codes might change according to your servo type.

I did some projects about sg90 servo speed. I used for loop to change degree and i added delay in loop.

for example,

for(int x = 0; x<=180; x++) {
 servo.write(x);
delay(100);
}

if you increase delay, your servo will turn slowly but low res.

some servos can move slowly by decreasing vcc voltage

#3

thank you for your help! unfortunately your advice doesnt work. maybe because my servo turns 270 degree?


#4

just edit for loop. this loop will work until variable x reaches 180. make it 270.

and also you can change increment details. linear or parabolic curves you can add…


#5

still not working… can u see whats wrong? thx!

Servo servo;

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

Servo servo2;

BLYNK_WRITE(V4)
{
  servo2.write(param.asInt());

}

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
 

  servo.attach(4);
  servo2.attach(5);
}

void loop()
{
  Blynk.run();
for(int x = 0; x<=270; x++) {
 servo.write(V4);
delay(200);

#6

Please properly format all posted code as per forum instructions.

BTW, this is not really a Blynk related topic, more like “how to code something”… lots of info via Google. :wink:

As far as integrating such option into Blynk… stay away from delays, use timers… (and search this forum :stuck_out_tongue_winking_eye: )


#7

seems like you didnt get what i want. i cant translate your example to my problem. i dont want the servo to move by itself. i want to control the angle with a slider and it should move slow all the time. help would be much apprechiated


#8

I ‘got’ exactly what you want :wink: but servos don’t have speed control, they have position control.

If you want slower speed you control the rate of incremental position.

My example shows how to do that without delays… it is not intended to work for your exact need, but give you something to work from.


#9

hi, do you think something like this could work? (still not working properly)
regards

#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[] = "xxx";

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

Servo servo;

BLYNK_WRITE(V3)
{
  servo.write(param.asInt());
}
const int targetPin =  4;
const int servoDuration = 10; // number of millisecs that servo is on
const int interval = 10; // number of milisecs when the servo is off


//------------ VARIABLES (will change)---------------------

byte targetservoState = servo.attach(4); // this variable is necesary to help arduino monitor the status of servo
                            //the initial condition (t=0) can be either run or stop
unsigned long currentMillis = 0;    // stores the value of millis() in each iteration of loop()
unsigned long previousservoMillis = 0;   // will store last time the servo was updated


//========== THE SETUP ==============================

void setup() {
  // Debug console 
  Serial.begin(9600); //default comm speed between arduino and serial monitor. no need to change.
  Serial.println("servoMillis.ino");  // so we know what sketch is running
  pinMode(targetPin, OUTPUT);
  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);




}

//========== THE LOOP ==============================

void loop() {
 
  Blynk.run();

  
  currentMillis = millis();   // capture the latest value of millis()
                              // this is equivalent to noting the time from a clock
  updateTargetservo();  //monitoring status of servo and do approproate actions

}

//------------ OTHER FUNCTIONS ---------------------

void updateTargetservo() {

  if (targetservoState == servo.attach(0)) { //if the servo is OFF
     Serial.println("detach()");
    if ((unsigned long) currentMillis - previousservoMillis >= interval) { 
      targetservoState = servo.attach(4);
      digitalWrite(targetPin, targetservoState);
      // and save the time when we made the change
      previousservoMillis += interval;
      // NOTE: The previous line could alternatively be
      //        previousservoMillis = currentMillis
      //        Adding on the interval is a better way to ensure that succesive periods are identical
    }
  }
  else {  // i.e. if  servoState is run (if the servo is ON)
    Serial.println("servo.attach(4)");
    if ((unsigned long) currentMillis - previousservoMillis >= servoDuration) {
      targetservoState = servo.attach(0);
      digitalWrite(targetPin, targetservoState);
      // and save the time when we made the change
      previousservoMillis += servoDuration;
      // NOTE: The previous line could alternatively be
      //        previousservoMillis = currentMillis
      //        Adding on the duration is a better way to ensure that succesive periods are identical
    }
  }
}