Sound sensor and Blynk

Hello! I need some help with my project. I want to make a LED turn on or off using a sound sensor with a digital output and the blynk app using a button . I managed doing it separated but i actually want that when the sound sensor is transmiting 1 and the LED is on, the Blynk button to change into ON and if i press it to turn off the LED and change into off. Same if the LED is off.
So basically i want to control the LED with bouth sensor and app , app knowing all the time the LED state .

The Blynk button widget needs to be connected to a virtual pin.

You would do this with a Blynk.virtualWrite(Vx,0) command, to turn the switch widget off (0).

The state change of the button widget fires the BLYNK_WRITE(Vx) function. You would test if the switch is on (1) or off (0).
You’d then switch your physical LED on or off accordingly, using a digitalWrite command.

Take a look at some of the sketch builder examples:

or the Blynk docs:
https://docs.blynk.cc

for more details.

Pete.

I did it ! I can’t belive i did it, i am so glad ! Thank you for the advice!

Here is the code, maybe someone is trying to do the same thing.

int soundSensor=7;
int LED=23;
boolean LEDStatus=false;
#define BLYNK_PRINT Serial


#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

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

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "   ";
char pass[] = "      ";

// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial1

// or Software Serial on Uno, Nano...
//#include <SoftwareSerial.h>
//SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

void setup()
{
  pinMode(soundSensor,INPUT);
pinMode(LED,OUTPUT);
  // Debug console
  Serial.begin(9600);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);



}

      BLYNK_WRITE(V0)
 {
 if(param.asInt()==HIGH)
  {
     digitalWrite(LED, HIGH);
  }
  else
  {
     digitalWrite(LED, LOW);
  }
 }
 

void loop()
{
 Blynk.run();

  int SensorData=digitalRead(soundSensor); 
  Serial.println(SensorData);
  
  if(SensorData==1)
  {

    if(LEDStatus==false)
    {
        LEDStatus=true;
        digitalWrite(LED,HIGH);
       Blynk.virtualWrite(V0,HIGH);
        delay(1000);
        
    }
    else if(LEDStatus==true)
    {
      LEDStatus=false;
        digitalWrite(LED,LOW);
      Blynk.virtualWrite(V0,LOW);
        delay(1000);
    }
       
    }
 
 
      }
1 Like

Well done on getting this far, but you should read this about how and why to move all of that code out of your void loop:

Pete.

Another problem, if i turn the wi-fi off everything stop running. I would like the sound sensor part to be still functionaleven if the app is offline.