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

Adding Servo Sweep to Working Button / Debounce / V-Pin Simpletimer Code on Arduino Uno


#1

I previously got help creating a debounced button to operate a relay for a given time with a V-pin on Arduino Uno via IFTTT HTTP request.

Simple Timer was the solution, thanks to @Fettkeewl !
Now I want to put a servo sweep around this same action (sweep -> action -> sweep back)
The sweep will be to a fixed position (i.e. 0 to 180 - wait - back to 0)
So no slider necessary.

FYI - Blynk Button pushed via IFTTT PUT request via code below - (full old thread about this here)

Now the question:
WHERE SHOULD I ADD THE SERVO SWEEP EXAMPLE CODE PIECES IN THIS CODE TO WORK / NOT BREAK EXISTING FUNCTION?

(I realize this is a Frankenstein’s Monster code mess, but it does work exactly as needed right now!)


/*******************************************************************
  Blynk lets you create beautiful drag-and-drop visual interfaces
  for your projects in 5 minutes. And it works with almost every
  hardware out there.

  Docs, Tutorials, everything:  http://www.blynk.cc
  Github:                       http://github.com/blynkkk
  Blynk Community:              http://community.blynk.cc
  Follow Us:                    http://www.facebook.com/blynkapp
                               http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

*********************************************************************

  You’ll need, in general:
  - Blynk App (download from AppStore or Google Play)
  - Arduino UNO or similar microcontroller board
  - Decide how to connect Arduino to the Internet
    (USB, Ethernet, Wi-Fi, Bluetooth, ...)

  There is a bunch of great example sketches included to show you how to get
  started. Think of them as LEGO bricks  and combine them as you wish.
  For example, take the Ethernet Shield sketch and combine it with the
  Servo example, or choose a USB sketch and add a code from SendData
  example.

*********************************************************************

  Let’s turn ON your LED with Blynk!
  In this example we'll use Arduino UNO + Ethernet Shield

  5 Steps guide:
  (watch video tutorial here: )

  1. Connect LED to Pin 9
  ( http://arduino.cc/en/uploads/Tutorial/simplefade_bb.png )

  In the Blynk App:
  2. Create New Project
  3. Email yourself Auth Token. You can do it later at any time
  4. Add a Button Widget. Select Pin D9 in Widget's Settings
  5. Press Play icon. Enjoy Blynking!

  You can find a QR code for easy setup of this project here:
  https://github.com/blynkkk/blynk-library/tree/master/examples/GettingStarted/BlynkBlink
    indent preformatted text by 4 spaces
********************************************************************/

#define BLYNK_PRINT Serial // Enables Serial Monitor

// Following includes are for Arduino Ethernet Shield (W5100)
// If you're using another shield, see Boards_* examples
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <SimpleTimer.h>

char auth[] = "XXXXauthocodeXXXXX"; // Put your Auth Token here. (see Step 3 above)
SimpleTimer timer;
int buttonSimMillis = 15000; // 500 miliseconds simulation

BLYNK_WRITE(V0)
{
  int blabla = param.asInt();
  if (blabla == 2)
  {
    digitalWrite(3, HIGH);
    timer.setTimeout(buttonSimMillis, buttonOff); // This will call the function buttonOff after
    //buttonSimMillis duration specified above

  }
  else if (blabla == 1)
  {
    digitalWrite(3, HIGH);
  }
  else
  {
    digitalWrite(3, LOW);
  }

}

void buttonOff()
{
  digitalWrite(3, LOW);
}

void setup()
{
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(3, OUTPUT);

  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth); // Here your Arduino connects to the Blynk Cloud.
}
void loop()
{
  timer.run();
  Blynk.run(); // All the Blynk Magic happens here...
}

// You can inject your own code or combine it with other sketches.
// Check other examples on how to communicate with Blynk. Remember
// to avoid delay() function!


#2

Thank you for moving to your own topic, but the original answer still applies… controlling servos is not really a Blynk command specific issue.

No need for a slider controlled servo? Fine… but if you are not willing to take offered code suggestions and try experimenting for yourself (HINT, another timer call perhaps?) then I don’t see why you expect others to do it for you (but who knows, perhaps someone will be bored :wink: )

Also, when posting code, please format it properly as described here so that is is properly viewable. I have already edited your above post, please look at how it was done. Thank you.


#3

Probably quite unsurprisingly, this is broken. The servo seems to twitch independent of the Blynk code, which shows offline. I know I’m trying to do some controversial stuff here (Blynk = Servo moves then pin goes on then Servo moves back) - really appreciated the Community’s continued help and hope this was at-least a solid shot in the dark!


    #define BLYNK_PRINT Serial // Enables Serial Monitor
    // Following includes are for Arduino Ethernet Shield (W5100)
    // If you're using another shield, see Boards_* examples
    #include <SPI.h>
    #include <Ethernet.h>
    #include <BlynkSimpleEthernet.h>
    #include <SimpleTimer.h>
    #include <Servo.h>

    Servo myservo;  // create servo object to control a servo
    // twelve servo objects can be created on most boards

    int pos = 0;    // variable to store the servo position


    char auth[] = "RealAuthCodeHere"; // Put your Auth Token here. (see Step 3 above)
    SimpleTimer timer;
    int buttonSimMillis = 15000; // 500 miliseconds simulation

    BLYNK_WRITE(V0)
    {
      int blabla = param.asInt();
      if (blabla == 2)
      {
        digitalWrite(3, HIGH);
        timer.setTimeout(buttonSimMillis, buttonOff); // This will call the function buttonOff after
        //buttonSimMillis duration specified above

      }
      else if (blabla == 1)
      {
        digitalWrite(3, HIGH);
      }
      else
      {
        digitalWrite(3, LOW);
      }

    }

    void buttonOff()
    {
      digitalWrite(3, LOW);
    }

    void setup()
    {
      // initialize digital pin LED_BUILTIN as an output.
      pinMode(3, OUTPUT);

      Serial.begin(9600); // See the connection status in Serial Monitor
      Blynk.begin(auth); // Here your Arduino connects to the Blynk Cloud.

      myservo.attach(9);  // attaches the servo on pin 9 to the servo object
    }

    void loop()

    {
      timer.run();
      Blynk.run(); // All the Blynk Magic happens here...

      for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
     myservo.write(pos);              // tell servo to go to position in variable 'pos'
        delay(15);                       // waits 15ms for the servo to reach the position
      }

      timer.run();

      for (pos = 0; pos <= 180; pos += 90) { // goes from 0 degrees to 180 degrees
        // in steps of 1 degree
        myservo.write(pos);              // tell servo to go to position in variable 'pos'
        delay(15100);                       // waits 15ms for the servo to reach the position
      }
      Blynk.run(); // All the Blynk Magic happens here...
     for (pos = 180; pos >= 0; pos -= 90) { // goes from 180 degrees to 0 degrees
        myservo.write(pos);              // tell servo to go to position in variable 'pos'
        delay(15);                       // waits 15ms for the servo to reach the position
      }
    }

    // You can inject your own code or combine it with other sketches.
    // Check other examples on how to communicate with Blynk. Remember
    // to avoid delay() function!


#4

The twitching is an indication of the internal timing “interactions” between libraries. The offline is because of the delayed().

Using delays and running looping functions that have long or otherwise “blocking” processes lead to the disconnects. Using them is not so much controversial as it is flat out guaranteed to halt all function.

I have found that the best way to control servos with Blynk is to absolutely avoid ALL delay() commands, constructively use timer routines and detatch the servos after the move (to avoid the idle jitter).

Here is a basic example that sweeps a serbo back and forth when a button widget (in switch mode) is activated… break it down to see how both the timers and servo detach work together.

//===== Servo Sweep Switch Function =====
BLYNK_WRITE(V0) // Switch Widget
{
  SrvoButtonState = param.asInt();
  if (SrvoButtonState == 1) {
    myservo.attach(servoPin);
    timer.setTimer(800L, Servo1, 1);  // Run Servo1 loop every 800ms
  } else
    myservo.detach();
} // END Blynk Function

void Servo1() {
  myservo.write(160);
  timer.setTimer(800L, Servo2, 1);  // Run Servo2 loop every 800ms
}

void Servo2() {
  myservo.write(0);
  Blynk.syncVirtual(V0);  // Check if switch still activated
}