Sound sensor not working in whole project

I have arduino project - House automation

When I put this code only on arduino, sound sensor works:

int soundSensor=A1;
int LED=5;


boolean LEDStatus=false;
   

void setup() {

Serial.begin(9600);

  pinMode(soundSensor,INPUT);
  pinMode(LED,OUTPUT);

}

void loop() {


  int SensorData=analogRead(soundSensor); 

  
  if(SensorData>600){

    if(LEDStatus==false){
        LEDStatus=true;
        digitalWrite(LED,HIGH);
    }
    else{
        LEDStatus=false;
        digitalWrite(LED,LOW);
    }

    delay(50);
  }

 

 }

When I upload same code in big project, even when I upload little part of project on arduino, sound sensor doesnt work.. whats happening?
Thanks

Whole code:

#include <Servo.h>
#include "DHT.h"
#include <SPI.h>
#include <MFRC522.h>
#include <Wire.h>

#define SERVO_PIN 8
#define SS_PIN 10
#define RST_PIN 9
#define lightingOutput A2
#define ldrPin A0
#define PIR 7
#define ledGrn 6
#define heatingOutput A3
#define coolingOutput A4
#define DHTPIN 2
#define DHTTYPE DHT11

MFRC522 mfrc522(SS_PIN, RST_PIN); // Create instance of our reader
Servo myservo; // Create instance of our motor /// create servo object to control a servo
DHT dht(DHTPIN, DHTTYPE);     //// Initialize DHT sensor for normal 16mhz Arduino

int pos;
int light;
float h, t;
int soundSensor=A1;
int LED=5;
boolean LEDStatus=false;
const int buzzer = 4;
long zadnje_ocitanje=0;  

void setup()
{

  Serial.begin(9600);
  dht.begin();
  SPI.begin();
  mfrc522.PCD_Init();
  myservo.attach(SERVO_PIN);
  myservo.write(0);

  pinMode(heatingOutput, OUTPUT);
  pinMode(coolingOutput, OUTPUT);
  pinMode(ledGrn, OUTPUT);
  pinMode(soundSensor,INPUT);
  pinMode(LED,OUTPUT);
  pinMode(lightingOutput, OUTPUT);

  zadnje_ocitanje=millis();
}
void loop() {

  Serial.println("\nHOME AUTOMATION\n");

  readSOUND();
  readRFID();
  readDHT();
  readLIGHT();

delay(1000);

}

////////////////////////////////////////////
void readRFID()

{
  //Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent() ) {
    return;
  }
  if ( ! mfrc522.PICC_ReadCardSerial() ) {
    return;
  }
  // If a card is detected, execute the following:
  Serial.println("Time to open");
  // Print the card's ID
  String content = "";
  byte letter;
  for ( byte i = 0; i < mfrc522.uid.size; i++ ) {
    content.concat(String(mfrc522.uid.uidByte[i], HEX));
    if ( i < mfrc522.uid.size - 1 ) content += "-";
  }
  content.toUpperCase();
  Serial.println();
  Serial.println("UID tag :’" + content + "‘");

  if (content == "C0-E5-86-25") {

    Serial.println("Authorized access");
     digitalWrite(ledGrn, HIGH);

    for (pos = 0; pos <= 90; pos += 1) {
      myservo.write(pos);
      delay(25);
    }
    for (pos = 90; pos >= 0; pos -= 1) {
      myservo.write(pos);
      delay(70);
      digitalWrite(ledGrn, LOW);
    }
  }
  else {
    Serial.println("Access Denied");
      tone(buzzer, 1000, 6000);
  }
}

//////////////////////////////////////////////////////

void readDHT()
{

  h = dht.readHumidity();
  t = dht.readTemperature();
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.println();

  if (t < 18) //if temperature is less than 18 celcius
  {
    digitalWrite(heatingOutput, HIGH);
    Serial.print("Heating activated\n");
  }
  else
  {
    digitalWrite(heatingOutput, LOW);
  }

  if (t > 22) //if temperature is more than 22 celcius
  {
    digitalWrite(coolingOutput, HIGH);
    Serial.print("Cooling activated\n");
  }
  else
  {
    digitalWrite(coolingOutput, LOW);
  }
}

//////////////////////////////////////////////////////

void readLIGHT()
{
 
 int value_pir = digitalRead(PIR);
 light = map(analogRead(ldrPin) , 0 , 1024 , 1 , 100);
 
  Serial.print("Lighting percentage: ");
  Serial.print(light);
  Serial.print("\nMotion detector: ");
  Serial.print(digitalRead(PIR));

  
if ((millis()-zadnje_ocitanje)>6000)   
  {    
   
  if ((light < 50) && ( value_pir == HIGH) )                            
  {
    digitalWrite(lightingOutput, HIGH);
    Serial.println("\nLighting ON");
  }
  else
  {
    digitalWrite(lightingOutput, LOW);
    Serial.println("\nLighting OFF");
  }
    zadnje_ocitanje=millis(); 
  }
}


//////////////////////////////////////////////////////////////

void readSOUND()
{
  int SensorData=analogRead(soundSensor); 

  if(SensorData>600){

    if(LEDStatus==false){
        LEDStatus=true;
        digitalWrite(LED,HIGH);
    }
    else{
        LEDStatus=false;
        digitalWrite(LED,LOW);
    }

    delay(50);
  }

}

*Edited thanks to septillion

First thing I notice.

All blank lines between everything.

Does not make it readable.

Correct indentations does :wink:

  String string1 = "Lighting percentage: ";

  String string2 = string1 + light;

  int value_pir = digitalRead(PIR);

  String string3 = "Motion detector: ";

  String string4 = string3 + value_pir;

  light = map(analogRead(ldrPin) , 0 , 1024 , 1 , 100);

  Serial.println(string2);

  Serial.println(string4);

Wow, that is a lot of (stupid named) use of String! Trying to make Swiss cheese of your memory? Know it does the same as:

  light = map(analogRead(ldrPin) , 0 , 1024 , 1 , 100);
  Serial.print("Lighting percentage: ");
  Serial.print(light);
  Serial.print9"Motion detector: ");
  Serial.print(digitalRead(PIR));

Only not completely, because here I send the last 'light' value instead of the previous...

But up to the delay(). Don't think in terms or delaying an action. Think of it as constantly checking if something needs to be done. And the condition can include time. So if you want to turn off a light after x time, don't include other conditions in that check.

Same, but the other way around, applies to turning it on. Does time play a roll in turning the light on? If not, don't check it :wink:

Aka, see turning on and turning off the light as two complete different tasks!

Pseudo code

if(dark && movement){
  turnOnLight();
}

if(timeSinceLightWasTurnedOn > TimeYouWantTheLightOn){
  turnOffLight();
}

another question:

milis instead delay

for example LED via ldr and PIR motion sensor - because when I use delay led is ON for few seconds, but in the meantime I cant use my other sensors ( I cant use RFID card reader when LED is on)

I tried few things, but nothing worked so far

This down is code I came up with:

Problem is now: I can go through whole code even when my LED is on ( I can read RFID while LED is on) but reading of ldr is every 8 seconds.. I can now only reading ldr sensor every 8 seconds

zadnje_ocitanje=millis();

void readLIGHT()

{

if ((millis()-zadnje_ocitanje)>8000)

{

zadnje_ocitanje=millis();

String string1 = "Lighting percentage: ";

String string2 = string1 + light;

int value_pir = digitalRead(PIR);

String string3 = "Motion detector: ";

String string4 = string3 + value_pir;

light = map(analogRead(ldrPin) , 0 , 1024 , 1 , 100);

Serial.println(string2);

Serial.println(string4);

if ((light < 60) && ( value_pir == HIGH) )

{

digitalWrite(lightingOutput, HIGH);

Serial.println("Lighting ON");

}

else

{

digitalWrite(lightingOutput, LOW);

Serial.println("Lighting OFF");

}

}

}

Was the sound sensor issue resolved?

If not, I'd leave the whole code and comment out all the code but the sound sensor and one other thing do, such as the servo thing do. Does the Sound sensor work? If not the issue may be with the servo thing do, if it does then add (uncomment) in another thing do and continue on till you find which thing do causes the sound sensor to stop working.

I'd not trip out about getting millis working till the code works with delay.

Thanks for reply Idahowalker.

I checked soundsensor with everything. I leave whole code till void loop () and sound sensor works.

But if I put even delay(1000) in void loop after, sound sensor doesnt work.

this works:

void loop() {

 readSOUND();

 } 

 ///////////////////////////////////////////////////////

 void readSOUND()
{
  int SensorData=analogRead(soundSensor); 

  if(SensorData>500){

    if(LEDStatus==false){
        LEDStatus=true;
        digitalWrite(LED,HIGH);
    }
    else{
        LEDStatus=false;
        digitalWrite(LED,LOW);
    }

    delay(50);
  }

}

This doesnt work:

void loop() {

 readSOUND();
 delay(1000);

 } 

 ///////////////////////////////////////////////////////

 void readSOUND()
{
  int SensorData=analogRead(soundSensor); 

  if(SensorData>500){

    if(LEDStatus==false){
        LEDStatus=true;
        digitalWrite(LED,HIGH);
    }
    else{
        LEDStatus=false;
        digitalWrite(LED,LOW);
    }

    delay(50);
  }

}

Also, if I put every other thing : servo, dht, lighting in code with sound sensor.. with or without delay... sound sensor doesnt work!

This is the code you indicate does not run.

void loop() {

 readSOUND();
 delay(1000);

 } 

 ///////////////////////////////////////////////////////

 void readSOUND()
{
  int SensorData=analogRead(soundSensor); 

  if(SensorData>500){

    if(LEDStatus==false){
        LEDStatus=true;
        digitalWrite(LED,HIGH);
    }
    else{
        LEDStatus=false;
        digitalWrite(LED,LOW);
    }

    delay(50);
  }

}

I reckon your issue is with this readSOUND() function.

I'd start with changing the function readSOUND() to:

void loop() {

 readSOUND();
 delay(1000);

 } 

 ///////////////////////////////////////////////////////

 void readSOUND()
{
  int SensorData=analogRead(soundSensor); 

//  if(SensorData>500){
//
//    if(LEDStatus==false){
//        LEDStatus=true;
//        digitalWrite(LED,HIGH);
//    }
//    else{
//        LEDStatus=false;
//        digitalWrite(LED,LOW);
//    }
//
//    delay(50);
//  }
Serial.println( "Boo." );

}

Does it print "Boo" every 1000mS?
If it does then you have a direction to go and the next thing I'd try is

void loop() {

 readSOUND();
 delay(1000);

 } 

 ///////////////////////////////////////////////////////

 void readSOUND()
{
  int SensorData=analogRead(soundSensor); 

  if(SensorData>500){
    Serial.println( "SensorData>500" );
//    if(LEDStatus==false){
//        LEDStatus=true;
//        digitalWrite(LED,HIGH);
//    }
//    else{
//        LEDStatus=false;
//        digitalWrite(LED,LOW);
//    }
//
//    delay(50);
  }
Serial.println( "Boo." );

}

If you get to this point what does it print?

1st code: It does print "Boo" every 1000mS.

2nd code: Its print "Boo" every 1000mS.

But if I change if statement to this:

if(SensorData>200)

Its printed :

SensorData>500
Boo

So what happens if you change the code to

void loop() {

 readSOUND();
 delay(1000);

 } 

 ///////////////////////////////////////////////////////

 void readSOUND()
{
  int SensorData=analogRead(soundSensor); 

Serial.println( SensorData );

  if(SensorData>500){
//    Serial.println( "SensorData>500" );
//    if(LEDStatus==false){
//        LEDStatus=true;
//        digitalWrite(LED,HIGH);
//    }
//    else{
//        LEDStatus=false;
//        digitalWrite(LED,LOW);
//    }
//
//    delay(50);
  }
// Serial.println( "Boo." );

}

?

print values from sensor

287
284
286

ajmemeni:
print values from sensor

287
284
286

What's that tell you about using SensorData>500?

Idahowalker:
What's that tell you about using SensorData>500?

thats too big?
Hm, problem here is reading of sound sensor every 1000 ms? Because if I blow for 2 seconds in it, LED will turn on.. and I have values then about 800,900 if I blow.. When I clap hands I have more then 600

When I upload now whole code, values are about 550 without noise in background expect PC

ajmemeni:
thats too big?
Hm, problem here is reading of sound sensor every 1000 ms? Because if I blow for 2 seconds in it, LED will turn on.. and I have values then about 800,900 if I blow.. When I clap hands I have more then 600

When I upload now whole code, values are about 550 without noise in background expect PC

What's the action like to reduce the delay to 900, 800, 700, 600, 500, 400, 300, 200, 100, 50, and so on and so forth?

How long is a clap or other such moment? If the sound is gone before 1 second is up you may lose all or part of the sound. The sensor may be asleep more time then it is awake. How long does your code take to execute? You can learn millis by trying to time the code execution.

Something like

iStartMillis = millis();
a bunch of code
Serial.println( millis() - iStartMillis ); and poof you know how many millis the code is taking. If you get 0's then use micros().