Latest blynk local server
latest android app
Wemos D1 mini pro
I know virtual buttons on the app can control physical pin state because the below function snaps my servo to 80 degrees when pressed. However, i have not been able to toggle a simple led with the same function. It will trip on for a like a mili second and then off, but if i assign a button on the app to a physical pin such gpio 16, it controls the led just fine.
Am i missing something here?
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Servo.h>
char ssid[] = "Ovie";
char pass[] = "hebe11one";
char auth[] = "59ddffd0b99b4f75a5f87e2b28c159b8";
void setup_wifi() {
delay(100);
// We start by connecting to a WiFi network
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
int laser = 16; //attaches laser diode to pin D0 on mini pro
//volatile byte laser = LOW;
//int button;
Servo servo;
void setup()
{
Serial.begin(115200);
setup_wifi();
Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,242), 8080);
servo.attach(5); //attaches servo to pin 2 (on D1 mini pro)
pinMode (laser, OUTPUT);
}
BLYNK_WRITE(V3)
{
//button = param.asInt(); // This assigns the Button state to a variable
if (param.asInt()) { // This checks the value of the variable and if equals 1, does something like...
// digitalWrite(laser, 255);
Blynk.virtualWrite(V3,255);
servo.write(80);
}
else
servo.write(0); // If variable doesn't equal 1 then set servo to this default position
// digitalWrite(laser, 0);
Blynk.virtualWrite(V3,0);
}
void loop()
{
Blynk.run();
}
That’s probably because when you press a button widget you get two BLYNK_WRITE events - one when the button turns on, and another when it turns off.
If you have the widget set to its default of Push, then these two events come immediately after one another. First time around it turns your LED on, then on the release of the button it turns it off.
You can confirm this behaviour by, with the button off, pressing and holding it. The LED should stay on for as long as the button is pressed, then go off when released.
Changing the button to Switch rather than Push will make it latch on, until it’d pressed again to turn it off.
If that doesn’t solve the problem then post your actual full code, along with a screenshot of how the button is configured.
Pete.
I have edited the question to show the entire code. I also included a picture of my app config.
I tried switching between push button and switch button as
@PeteKnight suggested but the result is the same.
I noticed that with the led button set to physical pin 16, i can toggle the led. And when the the virtual pin connected to the servo is pressed, to turn the servo “off” the led also goes off but will not come on when virtual pin 3 is pressed to turn on.
I hope i explained the issue clear enough.
From what I understand you want to use 2 buttons 1 physical and 1 on the app. For them to work together you have to code in some button states.
Actually no. I want to use just one button to control the servo and led. such that when i snap the servo to 80 degrees the led comes on and when i snap the servo back to 0 degrees the led goes off. but so far the led just flickers and will not stay on so i put the physical button there to troubleshoot.
If you’re controlling an LED Widget then you should attach it to a virtual pin and use Blynk.virtualWrite(Vpin,255)
to turn it on, and Blynk.virtualWrite(Vpin,0)
to turn it off.
Pete.
Also you have a widget there with a label gp16…what is this and where is it referenced in the code?
Here…
But it’s the wrong way to do it.
Pete.
Okay then I will try your above answer.
Whats the difference between blynk.virtualWrite
and digitalWrite
? Arduino throws an error that says ‘blynk’ was not declared in this scope
Digital write sets the physical GPIO pin on the board high or low.
Virtual write is simply sending a message between the app, server and hardware.
That’s because you used a lower case ‘b’ rather than an upper case ‘B’.
Pete.
I did that but it says “blynk was not declared in this scope scope”
Code at the top edited to show new full code. I was typing blynk.virtualWrite
instead of Blynk.virtualWrite
. I’ve fixed that and the error is no more, but after the edit to the code the servo no longer turns neither does the led flicker.
Yes, I told you that was the issue here:
But you said that the error still persisted!
If you are trying to control an LED widget with this code then why do the virtual write to the switch widget labelled ‘servo’?
Pete.
Because i want one blynk button to control the servo and led. Simply put, press blynk button, servo turns and led lights up. press button again, servo turns and led goes off.
Yes, I fully appreciate that.
The LED widget needs to be connected to a different virtual pin to the button widget, and you need to virtual write 255 to that virtual pin to turn the LED on, and 0 to turn it off.
Pete.
This means that one button cannot simultaneously control the led and servo right?
Yes it can. The servo is a physical device and therefore connected to a physical GPIO pin, or to some controller that is connected to the physical GPIO pin.
The LED widget is connected to a virtual pin.
When the button is pressed you will control the physical pin with a digital write and the virtual pin with a virtual write.
Pete.
Oh…Excuse me for not mentioning that it is a physical led i’m dealing with.