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

Do multiple tasks with one virtual write function

• 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?

Also, you can make this much neater…

by using:
if (param.asInt()) {

Pete.

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.

I’ve made the changes, and if you check you’ll see that the last line of the void setup declares the laser pin as output

I assume that its not working still?

What happens if you move the
digitalWrite(laser, HIGH);
up one line, so that it’s before the .servo.write command?

BTW, these lines are redundant now…

Pete.

Redundant lines deleted, I moved the

as suggested, it just made response to snapping the servo real slow. And laser still did not go HIGH.

The laser diode blinks and goes off for a split second when the servo snaps on.

Is your intention for the laser diode to be on all the time that the button widget is on?

Is your button widget set to Push or Switch?

How is your laser LED wired? - what is the other side of the LED connected to?

Pete.

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.

By that, do you mean the pin labelled D2?

Is the laser diode working as you want it to if you disconnect the servo (but still leave the code the same)?

Pete.

1 Like

Do you know the operation current and voltage of that laser diode?

Working voltage of the laser diode is 3.3, working current ~10ma. It works without the servo.

Sounds like your servo is killing the NodeMCU.
Try connecting an LED, with suitable resistor in place of the servo.

Pete.

1 Like