Code not running

Hi, im trying a code to use the sound sensor with an esp8266 12E.
this is it:
#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

int ledPin= D0;
int sensorPin= D1;
boolean val =0;

char auth[] = "xxx";
char ssid[] = "xxx";
char pass[] = "xxx";

WidgetLED led1(V0);



void SoundSensor(void)
{
 Serial.println (val);
  if (val==HIGH) {
    digitalWrite(ledPin, HIGH);
    led1.on();
  } else {
    digitalWrite(ledPin, LOW);
    led1.off();
  }
}

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(sensorPin, INPUT);
    pinMode(ledPin, OUTPUT);
}
void loop()
{
  Blynk.run();
  SoundSensor();
  }

So first i had the “SoundSensor” void part in void loop but this made the esp8266 constantly disconnect and reconnect. But it did work. Now it seems like nothing is working.
Can anyone spot what i am missing ?
Thanks

Check out the PUSH_DATA example to see how you call functions at timed intervals.

1 Like

You suggest i put it back in void loop and request the data with a time interval to not crash the esp ?
I did not quite understand.

Do you understand each part of the PUSH_DATA example?

Not in loop().

1 Like

I suggest going through the DOCs and getting familiar with BLYNK. You should pay close attention to the “BLYNK Main Operations” Section, as this is key to making BLYNK function correctly.

The PUSH_DATA() Example has everything you need to make your project work. Study it until you Understand it.

Basically, you need to use the BlynkTimer (or SimpleTimer) , and call the SoundSensor function at a timed interval.

Add this before the WidgetLED

BlynkTimer timer;

Add this to your setup function

timer.setInterval(1000L, SoundSensor); // calls SoundSensor Function every 1 second

Change SoundSensor() in loop function to

timer.run(); // Initiates BlynkTimer
1 Like

I see, this helps alot.
Thanks to both of you !