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

Don't write code in void loop! :D

Hello Blynkers… i’m working with arduino uno and ethernet shield for my iot projects…
i want to write some code in arduino but in “docs” said :
"Don’t put Blynk.virtualWrite and any other Blynk. command inside void loop()- it will cause lot’s of outgoing messages to our server and your connection will be terminated;* "
anyone have sollution?
Thanks

SimpleTimer as per many of the Blynk examples and documented on the Arduino sites.

1 Like

@Costas i want to write code for capacitive sensor… so i think we can’t use timer as well… i wan’t to write some code like:
if (capacitiveValue == LOW) {
digitalWrite (relay, LOW);
}

but they don’t permit me to write in void loop as usual…
help

@gabryel_rosely avoid the loop() like the plague. Probably look at attachInterrupt for the cap sensor but same rule applies, just set a flag in the interrupt routine.

You would probably get away with this in loop() when the setting is HIGH.
And with a quick digitalWrite() probably OK when LOW in loop().
There are exceptions to the no code in Blynk loop() “rule”.

Test and report back.

I have never used the cap sensors but if you use a short SimpleTimer (100ms) to keep reading for LOW then you could probably do the rest inside or outside of loop().

1 Like

Use timmer function as mentioned in example

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <SimpleTimer.h>

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

const int in1 = 2;
const int in2 = 3;
const int button = 9;
const int led = 6;

int v1update = 0;
int lastv1update = 0;
int buttonState = 0;

// This function will be called every time Slider Widget
// in Blynk app writes values to the Virtual Pin 1
BLYNK_WRITE(V1)
{
  buttonState = digitalRead(button);
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  // You can also use:
  // String i = param.asStr();
  // double d = param.asDouble();
  if (pinValue == 1) { 
    v1update = 1;
  }
  if (pinValue == 0) {
    v1update = 0;
  }
  
  if (v1update != lastv1update) {
    digitalWrite(led, HIGH);
    if (v1update == 1) {
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
  }
  if (v1update == 0) {
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
  }
}

lastv1update = v1update;

}

void sendls ()
{
  buttonState = digitalRead(button);
  
  if (buttonState == HIGH) {
    Blynk.virtualWrite(V5, HIGH);
  }else{
    Blynk.virtualWrite(V5, LOW);
  }
}
    
void setup()
{
  // Debug console
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(button, INPUT);
  
  Serial.begin(9600);

  Blynk.begin(auth);
  
  timer.setInterval(1000L, sendls);
}

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

.
.
.
i do like that but i got nothing

@scientist1995

did you have some projects like
“if your room terperature getting high , turn on fan” ?

No but i made a project on water level indicator in blynk

can you post it :smiley:

Its therr on blynk community check that out go to project made wity blynk

ok :+1:

Great hope that’s help you!!!

@Costas
@scientist1995

Thanks for you all. by using “short” simpletimer finaly it’s done
Thanks :+1:

1 Like

SimpleTimer rocks, you can’t really do any serious Blynking without it.

2 Likes

Thats what i suggested @Costas