Blynk Void Loop query

I’ve been reading the Blynk application notes and there is a section that refers to keeping the void loop() as clean as possible.

I have an arduino controlled irrigation system and the code with the void loop is quite long. I do not use delay() functions. If I want a delay I used the millis() function accordingly.

I now want to integrate blynk into my code so that I can control the arduino from my smartphone.

Do you suspect I will have issues considering I have a lot of code within my void loop?

Yes!

If we knew what’s in your loop(), you might get a better answer than a simple “Yes” :slight_smile:

I think it would be OK for you to post your code here, even if it’s not Blynk related per se. Then it would be easier to steer you in the right direction.

Hi,
Thanks for your reply. Well I suppose my question was more generic and I was thinking it should not matter about the code being displayed in this question. When I read the Blynk documentation a few questions come to mind when it states to keep the void clear clean as possible. Are Blynk just stating not to use Blynk write commands in the void loop to avoid flooding of their server or are they referring to any code within the void loop even if it is not associated with Blynk commands at all.
To me it seems a bit silly and defeats the purpose of the void loop.
I suppose I was testing to see if there were people out there with code in the viod loop and operating blynk successfully or otherwise, then would have been the simple answer for me.
Anyway it seems as though you have ideas in your mind that may assist.
Below is the complete programe in my UNO. Note that I have a touch screen LCD interfaced to the serial port and this is how I interface to the irrigation system currently. All I am trying to add in the Blynk functionality to be able to control the irrigation system from my smartphone.
Note also that I measure the scan time of my programe and the average scan time is 20ms. This may be an important measurement by the sounds of it for Blynk.

***** I COULD NOTE PASTE THE CODE IN AS IT EXCEEDED THE CHARACTER LIMIT OF THIS POST******

Is there a way I could upload the Arduino code file to you?

Maybe here?

HI,

You need to create a custom function (or more than one if you are doing multiple tasks), then call that function users timers which are configured in void setup(), eg

void setup() {
  // more setup stuff
  timer.setInterval(3000L, doMyStuff); // this will call doMyStuff() every 3 seconds
}

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

void doMyStuff () {
  // all your old loop stuff here
  //
}

Search the forums for examples to use timers.

cul
billd

Thanks Bill,
That all makes sense and it’s not a big deal to change my code accordingly, cut and paste etc.
I suppose I don’t really understand the reason for it.

Hi,
I have an online Dropbox. Perhaps if you give me your email address I can share the .ino file with you in drop box. Hope thats OK. Not sure if that is acceptable in this forum?

HI,
I ended up using paste bin.
Below is the link:

Note I have not included any blynk code yet.
As far a the Blynk App is concerned I will be sending four commands from the phone to the arduino:

  1. Irrigation zone,
  2. Irrigation time,
  3. Start irrigation,
  4. Stop irrigation.

The Arduino will send back three parameters to the Blykn App:

  1. Irrigation zone “xxxxxx” running,
  2. Irrigation time remaining mins,
  3. Irrigation time remaining secs

The Blynk library does a lot of work in the background. It’s constantly talking to the Blynk server (every time Blynk.run is executed, which should be hundreds of times per second) as well as sending regular heartbeat signals and pings to the server. If you clutter-up your void loop then this slows-down communication with the Blynk server and makes the device sluggish when responding to input from the app. It will also lead to disconnections in the long term.

The Blynk library has BlynkTimer built-in, which can handle up to 16 timer tasks per instance of the timer. This does a millis comparison in the background, in a very slick way, so should be used to replace your millis comparisons in the code.
Anything that doesn’t need to happen hundreds of times per second should be moved out of your code and placed in a function that is called with a timer. If any of these functions will take a long time to execute (more than a second) then you should put additional Blynk.run commands in these functions.
If you want to have a process run for a set amount of time (which you obviously do) then you should either use BlynkTimer in timeout mode, or use the RTC widget and do a time comparison on a regular basis (every minute maybe) called via a timer.

You have one blocking delay in your code…
delay (3500); //let the display start up after the reset (This is important)
and when you add-in your Blynk code you should aim to do this delay before connecting to Blynk.

Pete.

Thanks Peter,
I will do a test. I will configure my Blynk app in a test code with no code in the void loop other than whats required for Blynk. When that is all working I will copy this code to my irrigation system and see what happens. If it does not work I will then create a function for all of the code in the void loop and call that function from the blynk timer.
Good pickup with the delay (3500) code I had in my code. I forgot about this, perhaps because it was not written by myself as I just copied this from the recommended code by 4D Systems.

Sweet mother of… that’s a hefty chunk of code! :smiley:

My main man Pete have already explained it, but if you want the developers thoughts about it, read this:

http://help.blynk.cc/en/articles/2091699-keep-your-void-loop-clean

If you search the forum, you’ll find many examples of irrigation systems made by others that might give you some inspiration :slight_smile:

Good luck!