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

Using Continuous Rotating servo with Button

esp8266

#1

Hello everyone,
I’m totally new to Arduino and coding. I’m working on one project where I need the help of yours.
I’ve one continuous rotation servo and esp8266, the idea is when I turn the button “ON” servo rotates from point A to B which take 10 seconds and deactivate there. When I turn the button “OFF” it gets activate and move to point B to A which also take 10 seconds and get deactivate there.
Please help me with the code.

Thanks.


#2

Hello and Welcome to Blynk. Sorry we do not teach programming here, there are lots of other sites that do that, but we will try to help you learn about Blynk, how to find information, and answer your Blynk questions.

For starting off learning about Blynk, I recommend you start reading through the Documentation and Help Center… those links are right at the top of this page… scroll up until you see them.

For your project I recommend you start here, with a basic Servo control example…

https://examples.blynk.cc/?board=ESP8266&shield=ESP8266%20WiFi&example=GettingStarted%2FServo

Your continuous rotation servo will work, just act a bit differently… experiment to see how and why.

For more info, Google how continuous rotation servers are controlled via standard servo commands (I will leave the Googling part for you :wink: )

Read up on Virtual Pins…

Modify the example to use a Button and if else comparators to determine the “button” state and rotate the servo acordingly…

https://www.arduino.cc/reference/en/language/structure/control-structure/if/

https://www.arduino.cc/reference/en/language/structure/control-structure/else/

A code hint to get you started…

BLYNK_WRITE(vPin) {
  int value = param.asInt();
  if (value == 1) {
    // rotate your servo here for 10 seconds before stopping (via timeout timer)
  } else {
    // counter rotate your servo here for 10 seconds before stopping (via timeout timer)
  }
}

But, for your 10 second “delay” before shutting off the servo… do NOT use delay() but rather look into Blynk Timer and particularly the timeout() Timer that can then call the routine to stop your servo…

http://docs.blynk.cc/#blynk-firmware-blynktimer

https://playground.arduino.cc/Code/SimpleTimer#Functions


#4
#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";
#define OPENING 1
#define CLOSING 0
Servo s;
BLYNK_WRITE(V3)
{
  s.write(param.asInt());
}
//Turns servo counter-clockwise (opens)
void left(Servo servo) {
  servo.writeMicroseconds(700); 
}
//Turns servo clockwise (closes)
void right(Servo servo) {
  servo.writeMicroseconds(2300);
}

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

  Blynk.begin(auth, ssid, pass);
  s.attach(9);
  left(s);

  //Init Button
  pinMode(V3, INPUT);
  digitalWrite(V3, HIGH);
  
  int action = OPENING;
  int nextAction = CLOSING;
  long baseClock = 0;
  
      // close 
      if (nextAction == CLOSING) {
        action = CLOSING;
        right(s);
        baseClock = millis();
      }

      // open 
      else  { action = OPENING;
        left(s);
        baseClock = millis();
      }
}

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

How about this? Please correct me


#5

How about running the code on your hardware and seeing how it performs, then having a go at correcting it yourself if it doesn’t do exactly what you’re expecting?

If you get stuck then come back with some feedback about what does and doesn’t work, what you’ve tried, and what you think might be the cause of the problems that you’re experiencing.

Pete.