Mic fails to read analog value in blynk edgent environment but reads in a standalone code

Here is the whole code below. The main idea is to control my motor with a mic same as controlling the fan with dht11. Both have override control logic. So I can switch both the fan and the motor from Blynk app anytime I want. The control logic is working with the fan no issue with that but the mic fails to read analog value to give the motor the HIGH state. Actually the control logic is working fine in a separate code but it fails after incorporating with main blynk edgent code.


#include <DHT.h>
#define DHTPIN 13
#define DHTYPE DHT11

#define FANPIN 14

DHT dht(DHTPIN, DHTYPE);

unsigned long previousMillisTemp = 0;
long intervalTemp = 2000;

unsigned long previousMillisMic = 0;
long intervalMic = 100;

bool manualOverride = false;
bool manualOverride2 = false;


float sensorValue = 0.0;
int micPin = 12;
int motor = 5;
int threshold = 600;

const unsigned long delayDuration = 10000; // 10 seconds delay
unsigned long soundDetectedTime = 0; 

float convertFan = analogRead(FANPIN) / 10;
//float convertSensor = (sensorValue) / 100;

#define BLYNK_TEMPLATE_ID "xxxxxxxxxxx"
#define BLYNK_TEMPLATE_NAME "xxxxxxxxx"

#define BLYNK_FIRMWARE_VERSION        "0.1.0"

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

#define APP_DEBUG

// Uncomment your board, or configure a custom board in Settings.h
#define USE_ESP32_DEV_MODULE
//#define USE_ESP32C3_DEV_MODULE
//#define USE_ESP32S2_DEV_KIT
//#define USE_WROVER_BOARD
//#define USE_TTGO_T7
//#define USE_TTGO_T_OI

#include "BlynkEdgent.h"

BLYNK_WRITE(V0){
  int pinValue = param.asInt();
  if (pinValue == 1){
    analogWrite(FANPIN, 255);
    manualOverride = true;
  } else {
    analogWrite(FANPIN, 0);
    manualOverride = false;
  }
}


void sendSensor() {

  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

  
  Serial.print(F("%  Temperature: "));
  Serial.println(t);

  Blynk.virtualWrite(V1, t);
  Blynk.virtualWrite(V5, h);

  if(!manualOverride){
    if (t < 30) {
    analogWrite(FANPIN, 0);  // Fan off
  } else if (t >= 30 && t < 32) {
    analogWrite(FANPIN, 100);  // Half speed
  } else if (t >= 32 && t < 34) {
    analogWrite(FANPIN, 146);
  } else if (t >= 34 && t < 36) {
    analogWrite(FANPIN, 187);
  } else if (t >= 36 && t < 38) {
    analogWrite(FANPIN, 219);
  } else if (t >= 38 && t < 40) {
    analogWrite(FANPIN, 255);
  } else {
    analogWrite(FANPIN, 255);  // Full speed
  }
  }
 
   Blynk.virtualWrite(V4, FANPIN);
}


BLYNK_WRITE(V2){
  int pinValue1 = param.asInt();
  if (pinValue1 == 1){
    digitalWrite(motor, 1);
    manualOverride2 = true;
  } else {
    digitalWrite(motor, 0);
    manualOverride2 = false;
  }
}

void micMotor(){
  sensorValue = analogRead(micPin);

  Serial.println(sensorValue);

  if (!manualOverride2){
    if (sensorValue > threshold) {
    if (soundDetectedTime == 0) {
      Serial.println("Sound Detected! HIGH");

      digitalWrite(5, HIGH);  // Turn on the DC motor
      digitalWrite(BUILTIN_LED, 1);  
      soundDetectedTime = millis();   // Record the time when sound is detected
    } else {
      soundDetectedTime = millis();   // Update the time as long as sound is detected
    }
  } else {
    Serial.println("No Sound. LOW");
    if (soundDetectedTime != 0 && millis() - soundDetectedTime >= delayDuration) {
      Serial.println("Motor turned off");
      digitalWrite(5, LOW);   // Turn off the DC motor
      digitalWrite(BUILTIN_LED, 0);  
      soundDetectedTime = 0;          // Reset the detection time
    }
  }
}
//Blynk.virtualWrite(V3, convertSensor);
}





void setup()
{ 
  pinMode(5, OUTPUT);
  pinMode(micPin, INPUT);
  pinMode(FANPIN, OUTPUT);
  pinMode(BUILTIN_LED, OUTPUT);
  Serial.begin(115200);
  delay(100);
  BlynkEdgent.begin();
}

void loop() {
  BlynkEdgent.run();
  unsigned long currentMillis = millis();
    if (currentMillis - previousMillisTemp >= intervalTemp){
      previousMillisTemp = currentMillis;
      sendSensor();
    }
    if (currentMillis - previousMillisMic >= intervalMic){
      previousMillisMic = currentMillis;
      micMotor();
    } 
   
}

Here is the standalone code, it works fine here.

     

const int micPin = 12;    // Analog pin connected to the microphone
const int motorPin = 5;   // Digital pin connected to the DC motor
const int threshold = 600; // Adjust this threshold based on your environment
const unsigned long delayDuration = 10000; // 10 seconds delay

unsigned long soundDetectedTime = 0; // Variable to store the time when sound is detected

void setup() {
  Serial.begin(9600);
  pinMode(micPin, INPUT);
  pinMode(motorPin, OUTPUT);
  pinMode(BUILTIN_LED, OUTPUT);
}

void loop() {
  int micValue = analogRead(micPin);

  if (micValue > threshold) {
    if (soundDetectedTime == 0) {
      Serial.println("Sound Detected! HIGH");

      digitalWrite(motorPin, HIGH);  // Turn on the DC motor
      digitalWrite(BUILTIN_LED, 1);  
      soundDetectedTime = millis();   // Record the time when sound is detected
    } else {
      soundDetectedTime = millis();   // Update the time as long as sound is detected
    }
  } else {
    Serial.println("No Sound. LOW");
    if (soundDetectedTime != 0 && millis() - soundDetectedTime >= delayDuration) {
      Serial.println("Motor turned off");
      digitalWrite(motorPin, LOW);   // Turn off the DC motor
      digitalWrite(BUILTIN_LED, 0);  
      soundDetectedTime = 0;          // Reset the detection time
    }
  }

  delay(100); // Adjust the delay based on your desired sampling rate
}


GPIO12 is attached to ADC2.
ADC2 can’t be used for analog readings when WiFi is in use, so you need to select a pin that is connected to ADC1 instead.

Read this link for more info…

Pete.

1 Like

It worked! Thanks so much

It worked out! Thanks so much

1 Like