[SOLVED] Help with code - Blynk.begin

Hi, everyone. I’m new at blynking, just trying to control a car with bluetooth.
So, I’m in testing fase and I tried to see some values sent from app in my notebook. The problem is my leds turn on, but there’s nothing on serial monitor. I have tried to write some code to turn on the led on pin 13 without the need of blynk, but it does nothing, it seems to be ignoring the rest of the code and only obey blynk project. Can anyone tell me what am I missing?
Thank you guys.

Yes we can!

But not without: (properly formatted code, add three backticks ` before and after your code

Is this good? Well, this is one test I was trying to made, just wanted to see when the HC was connected or not, this is what blynk.connected do, am I right? But I tried several another things, not even just a Serial.print(“hello”) worked. So if you know what im missing, i would be pleased if you help me.

#define BLYNK_USE_DIRECT_CONNECT

// You could use a spare Hardware Serial on boards that have it (like Mega)
#include <SoftwareSerial.h>
SoftwareSerial Serial2(2,3 ); // RX, TX

#define BLYNK_PRINT Serial2
#include <BlynkSimpleSerialBLE.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "52a9d9aa5afa4960b1cc857265ba7626";
int x,y;
bool conectado;

void setup()
{
  // Debug console
 Serial.begin(19200);
 Serial2.begin(9600);

  Blynk.begin(Serial2,auth);
  // Blynk will work through Serial
  // 9600 is for HC-06. For HC-05 default speed is 38400
  // Do not read or write this serial manually in your sketch
  
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
    
}

BLYNK_WRITE(V0)
{
  x=param.asInt();
   if(x==0)
   {
    digitalWrite(10,LOW);
    digitalWrite(9,LOW);
   }
    else
    {
      if(x>10)
          {
        digitalWrite(9, LOW);
        analogWrite(10, abs(x));
          }
      if(x<-10)
          {
        digitalWrite(10, LOW);  
        analogWrite(9,abs( x));
          }
   }
}

void loop()
{
  conectado=Blynk.connected();
Serial.print(conectado);
delay(500);
Blynk.run();
   
}

Remove the delay from the loop. If you want to send data on a regular interval use Blynk.timer. An example for that can be found in the PushData example sketch. Other than that, this should work fine.

What if I just put a “digitalWrite(13, HIGH)” in the loop? whitout delay, it should work? because it doesnt, I have to use a interval ?
I will study the blynk.timer, anyway, thanks a lot.

The only thing in the loop should be the timer running and blynk.run.

Check out the Avoiding The Void part for more info.

http://help.blynk.cc/getting-started/step-by-step/how-to-display-any-sensor-data-in-blynk-app

But im not trying to send data to blynk, I understand that the avoiding the void part is only for sending data to the app. Im only trying to turn on a fisical led without blynk. I can’t have anything else in void loop but blynk functions?

Even if you are not sending much data to the App, Blynk still needs to be in constant communication with the server to some extent.

Thus it is because you are running Blynk (which resides as a running background library) that you need to follow some general rules in order to keep everything running smoothly. Best practice for either Virtual (App) I/O or Physical I/O polling is via timers, or in the case of physical, possibly interrupts.

It will try to process the command hundreds of times a second which is at the least very poor use of resources but might also crash your system.

1 Like

Also, change the “connected” test to an “if NOT connected” loop, otherwise it will also try to print your connected message hundreds of times a second.

void loop()
{
  if (!Blynk.connected())
  {
    // do something here if Blynk is NOT connected
  } else {
    Blynk.run();
  }
  timer.run();  // This will check any timer routines you have running
}

And perhaps sandwich the Blynk.run() in an else statement but not the timer.run().
You don’t want Blynk.run() being called when you are not connected to the server.
timer.run() should then be used to check at intervals if you are connected to the server and reconnect if you are not.

Good point, that I forgot… I have updated my example.

1 Like

Thank you guys, I will try your tips now!

So, I read some codes with blynktimer and tried to do something like it. But again it doesn’t work haha

#define BLYNK_USE_DIRECT_CONNECT
// You could use a spare Hardware Serial on boards that have it (like Mega)
#include <SoftwareSerial.h>
SoftwareSerial Serial2(2,3 ); // RX, TX

#define BLYNK_PRINT Serial2
#include <BlynkSimpleSerialBLE.h>


// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "52a9d9aa5afa4960b1cc857265ba7626";
int x,y;
bool conectado;
BlynkTimer timer;


 
void whydontyouwork(){
  Serial.print("I cant stand this anymore");
}

void setup()
{

  // Debug console
 Serial.begin(19200);
 Serial2.begin(9600);

  Blynk.begin(Serial2,auth);
  // Blynk will work through Serial
  // 9600 is for HC-06. For HC-05 default speed is 38400
  // Do not read or write this serial manually in your sketch
  
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(13, OUTPUT);
  
  timer.setInterval(500, whydontyouwork);

  
  
  
}

BLYNK_WRITE(V0)
{
  x=param.asInt();
   if(x==0)
   {
    digitalWrite(10,LOW);
    digitalWrite(9,LOW);
   }
    else
    {
      if(x>10)
          {
        digitalWrite(9, LOW);
        analogWrite(10, abs(x));
          }
      if(x<-10)
          {
        digitalWrite(10, LOW);  
        analogWrite(9,abs( x));
  
          }
   }
}






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

@Mateus_Henrique_da_S if Bluetooth is using Serial then you need to change your line of code to:

Serial2.print("Now it should work");

@Costas, but bluetooth is using serial2. Im trying to use Serial to print in the notebook screen. My timer.run seems to be doing nothing, it doesnt call the function in setInterval

If BT is 2 shouldn’t this be just Serial?

Actually, I changed that, I commented this line. See, I want to print something independent from blynk, this blynk_print is for receiving data from app, I guess.

ok keep as Serial for the print but change 500 to 2500L.