Problem with coding

Hi
I use Blynk know than over a week and it’s amazing :smile: .
But if i put a delayMicroseconds(2); into my void loop() my “not virtual widgets” dont work anymore or react seconds later. Here is my project: I have a temperature-sensor, a hc-sr04 and a rgb-LED with three slider-widgets. But with the “delayMicroseconds(2);” everything works but not my rgb-LED (or it reacts seconds later). So all Digital-Widgets are interrupted by the “delayMicroseconds(2);” . [ 1, 2, 6 and 8 are virtual LED’s // V5 is the distance (hc-sr04) and V9 the temperature]

#define BLYNK_DEBUG // Optional, this enables lots of prints
#define BLYNK_PRINT Serial

/**************************************************************

  • Blynk is a platform with iOS and Android apps to control
  • Arduino, Raspberry Pi and the likes over the Internet.
  • You can easily build graphic interfaces for all your
  • projects by simply dragging and dropping widgets.
  • Downloads, docs, tutorials: http://www.blynk.cc
  • Blynk community: http://community.blynk.cc
  • Social groups: http://www.fb.com/blynkapp
  •                           http://twitter.com/blynk_app
    
  • Blynk library is licensed under MIT license
  • This example code is in public domain.

  • This example shows how to use Arduino Ethernet shield (W5100)
  • to connect your project to Blynk.
  • Feel free to apply it to any other example. It’s simple!

**************************************************************/

//#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>

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

int TMP = A0;
int temperature = 0;

int trigPin = 7;
int echoPin = 6;

void setup()
{
Serial.begin(9600);
Blynk.begin(auth);
// You can also specify server.
// For more options, see Transports/Advanced/CustomEthernet example
//Blynk.begin(auth, “server.org”, 8442);
//Blynk.begin(auth, IPAddress(…), 8888);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop()
{
Blynk.run();

long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
Blynk.virtualWrite(5, distance);

temperature = map(analogRead(TMP), 0, 410, -50, 150);
Blynk.virtualWrite(9, temperature);

{ if (temperature >= 20)
{
Blynk.virtualWrite(1,HIGH);

}
else
{
Blynk.virtualWrite(1,LOW);
}

if (temperature < 20)
{
Blynk.virtualWrite(2,HIGH);

}
else
{
Blynk.virtualWrite(2,LOW);
}

}

{ if (distance >= 10)
{
Blynk.virtualWrite(8,HIGH);

}
else
{
Blynk.virtualWrite(8,LOW);
}

if (distance < 10)
{
Blynk.virtualWrite(6,HIGH);

}
else
{
Blynk.virtualWrite(6,LOW);
}

}

}

I hope you can help me with this ;).

I have added a simple 50 millisecond delay so I could blink an LED in the loop and it hasn’t affected the performance, however delays in your main loop may degrade the performance as stated in Blynk github page. I would maybe try something like: A very simple delayMicroseconds(2) routine and then slowly add more of the code in your loop and see when it breaks. Another thing is to add a timer. Here is one I use. Code:

//#define BLYNK_PRINT Serial    
#include <Bridge.h>
#include <BlynkSimpleYun.h>
#include <SimpleTimer.h>   //Good timer library
#include <dht.h>
#define dht_dpin 9 
dht DHT;
int temp;
int humidity;
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxx";

SimpleTimer timer;

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth);
  // Setup function to be called every 2 seconds
  timer.setInterval(2000, sendData);
}

void sendData()
{
  DHT.read11(dht_dpin);
  temp = (DHT.temperature * 1.8 + 32); 
  humidity = DHT.humidity;
  Blynk.virtualWrite(5, temp);
  Blynk.virtualWrite(6, humidity);
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates SimpleTimer
}

Also, if you highlight your code and then hit the </> button when writing your coment, it will format it better so we can read it easier.

1 Like