Hi guys,
I´m trying to use an code that will be used with sound sensor.
The sensor will be positioned in front of an speaker. I need to activate an relay when the sound sensor capture any sound. But this relay needs to still activated until there is no sound in 1 minute.
The code that I have, the relay start and stop at any moment according to the sound variable. I need to adjust this code to start the relay at first sound signal, but stop the relay only at 1 minute of total silent.
Could you guys help me with this code ?
Thanks
Here is the code that I´m trying:
======================
int relay = 12;
int threshold = 100;
int volume;
void setup() {
Serial.begin(9600); // Serial port begin
pinMode(relay, OUTPUT);
}
void loop() {
volume = analogRead(A4); // Reads the value from the Analog PIN A4
//Serial print level
Serial.println(volume);
delay(100);
if(volume>=threshold){
digitalWrite(relay, HIGH); //Turn ON Relay
}
else{
delay(10000);
digitalWrite(relay, LOW); // Turn OFF Relay
}
}
=================================
afp1394:
Hi guys,
I´m trying to use an code that will be used with sound sensor.
The sensor will be positioned in front of an speaker. I need to activate an relay when the sound sensor capture any sound. But this relay needs to still activated until there is no sound in 1 minute.
The code that I have, the relay start and stop at any moment according to the sound variable. I need to adjust this code to start the relay at first sound signal, but stop the relay only at 1 minute of total silent.
Could you guys help me with this code ?
Thanks
Here is the code that I´m trying:
======================
int relay = 12;
int threshold = 100;
int volume;
void setup() {
Serial.begin(9600); // Serial port begin
pinMode(relay, OUTPUT);
}
void loop() {
volume = analogRead(A4); // Reads the value from the Analog PIN A4
//Serial print level
Serial.println(volume);
delay(100);
if(volume>=threshold){
digitalWrite(relay, HIGH); //Turn ON Relay
}
else{
delay(10000);
digitalWrite(relay, LOW); // Turn OFF Relay
}
}
=================================
Welcome to the Arduino forum.
Start by checking "volume" being 0 and reporting that on the Serial.println(volume);
Then once you can see there are really zero volume happening, add code to save the millis value when that happens.
And once that is working, add code so you save the millis one time only if the volume is zero and compare the new millis value to the saved value and if 10 minutes worth of millis has passed, you can turn the relay off.
Paul