I am new to both Blynk and arduino, I am trying to control two servo with blynk app but with the following code only one servo is getting controlled whereas other motor is not getting controlled rather.In short both motor work fine individually but when tried to control both only one gets control
// ********************************************************
Download latest Blynk library here:
https://github.com/blynkkk/blynk-library/releases/latest
Blynk is a platform with iOS and Android apps to control
Arduino, Raspberry Pi and the likes over the Internet.
You can easily build graphic interfaces for all your
projects by simply dragging and dropping widgets.
Downloads, docs, tutorials: http://www.blynk.cc
Sketch generator: http://examples.blynk.cc
Blynk community: http://community.blynk.cc
Follow us: http://www.fb.com/blynkapp
http://twitter.com/blynk_app
Blynk library is licensed under MIT license
This example code is in public domain.
*************************************************************
Rotate a servo using a slider!
App project setup:
Slider widget (0...180) on V3
*************************************************************/
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <Servo.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "pasted auth token here";
Servo servo;
BLYNK_WRITE(V3)
{
servo.write(param.asInt());
}
Servo servo1;
BLYNK_WRITE(V1)
{
servo1.write(param.asInt());
}
void setup()
{
// Debug console
Serial.begin(9600);
Blynk.begin(auth);
servo.attach(9);
servo1.attach(11);
}
void loop()
{
Blynk.run();
}
I have found that Blynk’s internal use of hardware interrupt timers can cause some issues with the servo libraries use of same interrupt timers; delays, stuttering, etc.
Try acquiring the slider inputs as you have done, but then running the servo actions, one after another, in a separate, possibly timed, loop?
You might have to play around with using different pins for one of the servo, or even trying a different servo library.
The OP already had a slider for each, and when using servos, running with virtual pins and servo library is much smoother than direct pin control via the app. I have ran servos both way, many times… Try it, you will see
@Lichtsignaal Yes, that can help with power issues, as will having separate power source (actually essential with multiple servos), but the OP issue is only one servo is being controllable at a time… from my testing of multiple servos over Blynk, it seems to be a conflict between Blynk, Timers and Servo Libraries as all tend to rely on the interrupt timers (the more timers I had running the more erratic the jittering). And that was even with separate power for the servos.
@Darshini I have tried a few things with servos and the only reasonable way to stop jittering and help smooth out control was by attaching and detaching a servo before and after each movement. This might help with multi-servo control as well,
@Darshini This is some code I used to sweep a single servo back and forth using timers instead of delays, however you might be able to see how it works and modify it to control two servos together.
Instead of driving a servos from each slider function, you would need to take the data values from your slider inputs as the occur and feed them to a timer loop that ran the servos ever 200-500ms or so, depending on the overall purpose of your sketch.
//===== 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
}
Would it be an option to use a separate I2C board for controlling servo’s? I use a similar thing which can address 16 servo’s via I2C. I think it’s an Adafruit clone board.
Yes, I know… your code will not work sufficiently. That is why I suggested using a timer loop. And @Lichtsignaal’s suggestion of a I2C external servo controller IC is also a great idea as it avoids the otherwise troublesome Blynk/Arduino/Timer interrupt conflicts.
As for PWM pins… servos don’t properly run on PWM, they run better on PPM; Similar but different That is what the Servo Libraries do - provide a proper PPM signal.