I needed to smoothly control a servo, without the Servo library (as it blocks PWM on a few needed pins… as does Servo2). While Arduino forum was uninformative on the topic (just a lot of “use other pins” ), I did find some interesting info, via Google, that I used to make up my own method… And it works perfectly, outside of Blynk, as it is shown here:
// Manual servo control - Without Library
// Quick and minimal jitters.
//
// Currently using a potentiometer, but could be fed a number from
// 0-1023 that then gets mapped to microsecond pulses for the servo.
// My servo worked best with 690-2350 (0.69ms - 2.35ms) for a 180 deg range.
// Adjust accordingly to fit your servo.
//
// Gunner.
#define SERVO_PIN 5
#define POT_PIN A2
int ServoPos;
void setup()
{
pinMode(SERVO_PIN, OUTPUT);
}
void ServoMove()
{
ServoPos = map(analogRead(POT_PIN), 0, 1023, 2350, 690); // Map to microsecond pulse (i.e. 2.35*1000=2350)
digitalWrite(SERVO_PIN, HIGH); // Activate servo pin.
delayMicroseconds(ServoPos); // Delay for the length of the pulse.
digitalWrite(SERVO_PIN, LOW); // Deactivate to servo pin.
delay(5); // Just a little extra delay seemed necessary to stabilize everything.
}
void loop()
{
ServoMove();
}
So, on with my question… In order to get this to work as a Blynk function (as it only moved part way, requiring multiple touches in the same spot of the slider to complete the move), I found I needed to add Blynk.syncVirtual(V12);
right after the delayMicroseconds(ServoPos);
Now the servo goes where it should… it is almost as smooth and sometimes buzzes for a few seconds once it gets there, but it technically works.
The Blynk code is here:
BLYNK_WRITE(V12) // Slider set on V12 with 0-1023 range - Works with or without Send on Release.
{
SrvoPos = map(param.asInt(), 0, 1023, 2350, 690); // Convert input to microsec output.
digitalWrite(SRVO, HIGH); // Activate servo pin.
delayMicroseconds(SrvoPos); // Delay for the length of the pulse.
Blynk.syncVirtual(V12); // *** Needed to complete pulse for some reason? ***
digitalWrite(SRVO, LOW); // Deactivate to servo pin.
delay(5); // Just a little extra delay seemed necessary to stabilize everything.
}
Now… what I don’t understand is why? Shouldn’t the flow of action in a Blynk Function be just the same as a Void outside of Blynk? i.e. Enter Void/Function - Get Delay Time - Turn ON Pin - Wait Specified Delay Time - Turn OFF Pin - Exit Void/Function.
Better minds needed in-order to answer this Life Saving question