Is it Possible - to have a digital.write() in the main loop with Blynk.run()?

void setup()
{
  SwSerial.begin(9600);
  Blynk.begin(auth);
  // Default baud rate is 9600. You could specify it like this:
  //Blynk.begin(auth, 57600);
}

void loop()
{
  Blynk.run();
}

In here, is it possible that I put,
digital.write(PIN, HIGH) ?

void setup()
{
  SwSerial.begin(9600);
  Blynk.begin(auth);
  // Default baud rate is 9600. You could specify it like this:
  //Blynk.begin(auth, 57600);
}

void loop()
{
  Blynk.run();
  digital.write(PIN, HIGH); // How can I use another code through blynk example?
}

Actually, I want to use another code in blynk example,
however, in Blynk.run(); I think the process is in only loop ‘Blynk.run()’.
So I cannot use another code in loop.
How can I fix it?

I have combined several example from blynk… and what I did was add the code under void setup().
here’s my code


#define BLYNK_PRINT SwSerial
#define BLYNK_PRINT DebugSerial

#include <SoftwareSerial.h>
SoftwareSerial SwSerial(10, 11); // RX, TX
SoftwareSerial DebugSerial(10, 11); // RX, TX
    
#include <BlynkSimpleStream.h>
#include <Servo.h>


// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "8f11761774984fbd805ac7b40aefb1cd";

void notifyOnButtonPress()
{
  // Invert state, since button is "Active LOW"
  int isButtonPressed = !digitalRead(2);
  if (isButtonPressed) 
  {
    
    SwSerial.println("Button is pressed.");
    
    // turn LED on:
    digitalWrite(LED_BUILTIN, HIGH);
  } else {
    // turn LED off:
    digitalWrite(LED_BUILTIN, LOW);
  }
  
    // Note:
    //   We allow 1 notification per 15 seconds for now.
    Blynk.notify("Yaaay... button is pressed!");
}

Servo servo;

BLYNK_WRITE(V1)
{
  int pos = param.asInt();
  {
      if (pos==0)
      {
        servo.write(0);
        Serial.println("Open");
      }
      else if (pos==1)
      {
        servo.write(180);
        Serial.println("Open");
      }
  
  Serial.println(pos);  
  }
}

void setup()
{
  // Debug console
  SwSerial.begin(9600);

  // Blynk will work through Serial
  // Do not read or write this serial manually in your sketch
  Serial.begin(9600);
  Blynk.begin(Serial, auth);

  // Setup notification button on pin 2
  pinMode(2, INPUT_PULLUP);
  // Attach pin 2 interrupt to our handler
  attachInterrupt(digitalPinToInterrupt(2), notifyOnButtonPress, CHANGE);
  //for LED
  pinMode(LED_BUILTIN, OUTPUT);

  servo.attach(9);

}

void loop()
{
  Blynk.run();
}

@11120 leaving a digital.Write() in the void loop() will have it running many hundreds of times a second… so use timers instead

http://docs.blynk.cc/#blynk-firmware-blynktimer

void setup()
{
  SwSerial.begin(9600);
  Blynk.begin(auth);
  timer1.setInterval(1000L, PinAction);  // Run this void "PinAction" every second
}

void PinAction()  // This void function gets called every second by timer1
{
digitalWrite(PIN, HIGH); // Set this pin HIGH
digitalWrite(PIN, LOW); // Set this pin LOW
}

void loop()
{
  Blynk.run();
  timer1.run();  // check the timer, called timer1
}

To complement @Gunner’s answer, I’d like to add, that YES, YOU CAN :grin: use digital.write() inside loop, but then you need to take care it will be called in a timed manner. It won’t work without it. And no, the delay() cant’t be used here (in loop() )

This isn’t a command… where did you find this example? needs to be updated to digitalWrite()

Hah!.. That’s what I get for sleeptyping :stuck_out_tongue: … I just copied from the OP’s code but didn’t do a syntax check… opps :blush:

Ohhh I thought you got it from the sketch builder or some official example :stuck_out_tongue: stand down solider

1 Like