Reading from 2 different sensor

hi there,
I'm trying to make a light sensor using a pir motion sensor and a sound sensor module.
I'm making it so that the pir motion sensor will give the command to turn on the the light for a few second and at the same time activating the sound module after it manage to detect a person. In that few second after the light turned on, the sound module will try detect the person entering the room. if it's success on detecting that person, then the sound module will overwrite the previous command where the light turned on for a few second with turning it on permanently. Basically I'm trying to make both sensor able to detect and give their own command after detection.

So far I'm trying to make them work individually first and merge them with an if()
the pir works fine but I still can't make the sound module to detect anything,
I also tried using millis() instead of delay(), but it has the same result so I changed it back to delay for now.

int irmotionPin = 4
int relayPin = 9

int soundDetectedVal = LOW

int soundDetectedPin = 10
unsigned long detectTime;

boolean bAlarm = false;

unsigned long lastSoundDetectTime;


int soundAlarmTime = 500; 


void setup(){
  Serial.begin(9600);
  pinMode(relayPin, OUTPUT);  
  digitalWrite(relayPin, LOW);  
  pinMode (soundDetectedPin, INPUT) ; 
}

void loop(){
  
   
  if (digitalRead(irmotionPin) == HIGH) {  
       digitalWrite(relayPin, HIGH);  
       Serial.println("Relay is ON");
      
       delay(5000);  

      soundDetectedVal = digitalRead (soundDetectedPin) ;

  if(soundDetectedVal == HIGH) 
  {
    lastSoundDetectTime = millis(); 
    if (!bAlarm){   

      Serial.println("LOUD, LOUD");
      digitalWrite(relayPin,HIGH);
       bAlarm = true;      
      }
    }
  }
  
  else{       
      digitalWrite(relayPin, LOW);
      Serial.println("Relay is OFF");  
      delay(500);
  }
  
}
       delay(5000);  

soundDetectedVal = digitalRead (soundDetectedPin) ;

So close your ears for 5 seconds, then check if there is currently a sound?

Have you tested the sound sensor works by itself?

I have tested both sensor before putting them together, both works normally.

Also, after some more reading, I probably should use millis() instead of delay() since it will stop the sound sensor from trying to detect anything. I'll test things with this while waiting for more advice

So, the PIR sensor is going to detect a large, warm-blooded creature moving, and the Arduino is going to turn a pin on, and off again later.

Then, the sound sensor is going to listen for the person tripping over furniture in the dark, and cursing. The Arduino will then turn the pin (lights) back on.

Got it.

So, what does the code actually do, and how does that differ from what you want?

what I'm thinking is that since the PIR sensor can only detect movement but can't detect the direction of movement ( in this case moving in or out of the room ), I'm making it so that the sound sensor would be the one to tell if a person is moving in or out by listening to any sound that person made when entering, like their footstep sound.

After the PIR sensor detect a person moving, arduino will turn on a pin for a duration of time and let the sound sensor try to listen to any sound that person make.

If the sound sensor fail to detect any sound in that duration of time then the arduino will turn off the pin like it supposed to since it's only stay on for a certain duration. But if the sound sensor able to detect any sound in that duration of time, instead of turning the pin off, it will give arduino a new command to keep the pin on.

Basically, the sound sensor doesn't wait for the pir to complete it's entire on/off sequence. If the sound sensor can detect any sound, it will interrupt the off sequence from the PIR sensor, and change it to on, or make a new on sequence

I'm making it so that the sound sensor would be the one to tell if a person is moving in or out by listening to any sound that person made when entering, like their footstep sound.

You might be able to tell which way a person is moving, by listening to the increasing or decreasing volume. I doubt that the microphone will be sensitive enough for the Arduino to do that, though.

What you are more likely doing is getting different data from each ear, and using that difference data to determine direction. The Arduino will NOT be able to do that with a single microphone.

If all you are doing is, once the pin is turned on, listening for ANY sound, and, if it exceeds some threshold, clear the flag that indicates that the pin should (eventually) be turned off, then that is pretty simple.

It sounds to me like the “pretty simple” option for now.

Try this code below. This replaces the two lines I quoted above.

  const unsigned long Listen_Period =5000; //milliseconds, listen for this long
  unsigned long listenStart = millis();
  soundDetectedValue = false;
  while(!soundDetectedValue && millis()-listenStart < Listen_Period) {
    soundDetectedValue = digitalRead (soundDetectedPin) ;
  }
    soundDetectedValue = digitalRead (soundDetectedPin) ;

What kind of sensor is going to say “Yes, I heard that” or “No, nothing to hear here”?

The one that the OP has tested and says is working. Seems like magic to me but we all know Clarke's Law don't we?

PaulS:
I doubt that the microphone will be sensitive enough for the Arduino to do that, though.

Will it work if I can increase the sensitivity? I’m currently using a sound module with an adjustable sensitivity but I haven’t tested how much I can increase it though.

PaulS:
If all you are doing is, once the pin is turned on, listening for ANY sound, and, if it exceeds some threshold, clear the flag that indicates that the pin should (eventually) be turned off, then that is pretty simple.

yeah, I 'm trying to do something like this but instead of exceeding a threshold, I want it to somehow clear the flag after listen to any sound.

MorganS:
It sounds to me like the “pretty simple” option for now.

Try this code below. This replaces the two lines I quoted above.

  const unsigned long Listen_Period =5000; //milliseconds, listen for this long

unsigned long listenStart = millis();
 soundDetectedValue = false;
 while(!soundDetectedValue && millis()-listenStart < Listen_Period) {
   soundDetectedValue = digitalRead (soundDetectedPin) ;
 }

thanks for this, I’ll test it ASAP and inform the result.

PaulS:

    soundDetectedValue = digitalRead (soundDetectedPin) ;

What kind of sensor is going to say “Yes, I heard that” or “No, nothing to hear here”?

is this part wrong?
This is where I get the value from the sound sensor.

I want it to somehow clear the flag after listen to any sound.

So, a flea farts in the room, and you want the lights to stay on. Got it.

is this part wrong?

A sound sensor would typically be an analog device. It would be connected to an analog pin, and tell you how much sound it detected. Since you haven't told us what sensor you are using, it may be that your sensor is a digital sensor, and only tells you that it heard enough sound (for some definition of enough) or that it didn't.

PaulS:
So, a flea farts in the room, and you want the lights to stay on. Got it.

I’m trying to make it so that movement first, listening second. So as long as the PIR sensor doesn’t detect any movement then the sound sensor can’t start doing it’s job yet.

PaulS:
A sound sensor would typically be an analog device. It would be connected to an analog pin, and tell you how much sound it detected. Since you haven’t told us what sensor you are using, it may be that your sensor is a digital sensor, and only tells you that it heard enough sound (for some definition of enough) or that it didn’t.


that’s the sound sensor I’m currently using.

that's the sound sensor I'm currently using.

That picture is just about useless. It does, though, show that the device HAS an analog output, so you can tell whether it was an elephant sitting on it that farted, or a flea across the room that farted.

Frankly, I think your idea of listening for someone in the room is fundamentally flawed. You will not be able to tell that there is someone in the room, if they are quietly reading a book.

You won't be able to tell that there is no one in the room if the person left the TV on when leaving.