• Nodemcu 12E with wifi
• Android 9 OS
• Blynk local server
• Blynk Library version: latest
I want to perform 2 tasks when a virtual button is clicked on the app.
I want the servo to snap to 80 degrees when the button is pushed. (This it already does)
I also want to turn on a physical led at the same time. (This part does not work)
While troubleshooting i realized that the sketch will only either turn on the led or snap the servo, but never do both at the same time. Kindly take a look at my sketch and show me what i’m missing.
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Servo.h>
char ssid[] = "xxxxx";
char pass[] = "xxxxx";
char auth[] = "xxxxxxxxxxxxxxxxxxxxx";
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 = 4;
//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 (D4 on nodemcu)
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...
servo.write(80); // ... move the servo to position based on another variable
digitalWrite(laser, HIGH);
}
else
servo.write(0); // If variable doesn't equal 1 then set servo to this default position
digitalWrite(laser, LOW);
}
void loop()
{
Blynk.run();
}
I doubt that this code compiles, as you’re not declaring your LED digital pin as being an output (not necessary for compilation, but necessary for the sketch to work correctly), but you are declaring a pin that you’re referencing as “laser”…
This will give a compiler error of “laser not declared in this scope”.
GPIO5 is pin D1 on the NodeMCU
GPIO4 is pin D2 on the NodeMCU
Is this how you have it wired, with the other side of the LED connected to GND?
Actually, the led is a laser diode. I just changed laser to led after i posted the sketch. I’ll edit the sketch and make the cosmetic change as suggested.
Yes i intend for the laser diode to be on all the time the button widget is on.
Button widget is set to switch.
The anode of the laser diode is connected to pin 4 on the nodemcu, the cathode is connected to GND on the same nodemcu.