DVDdoug:
P.S.
You might want to START with a simple program that turns-on the LED whenever sound is present (with no timing). Then reverse the logic to turn the LED off when sound is present. Then add the timing to ignore short periods of silence.
That's what I already have in the original code. I can get stuff to activate with sound (or without it)
Wawa:
That's AFAIK a sensor with digital output only, so you should use a digital pin and digitalRead.
The pot on the module sets the sound/nosound threshold (sensitivity).
Leo..
Yes I realized that later.
A friend was helping me and got me a little further. The problem I am now having is the microphone is not sensitive enough. With a very basic code, the mic can hear me across the room. Now it can barely hear me right next to it. I either need a better module or something in the code is affecting the sensitivity.
byte soundSensor = 2;
byte LED = LED_BUILTIN;
// timer
const unsigned long period = 12000;
unsigned long currentTime;
unsigned long startTime;
unsigned long finishTime;
bool running = true;
// debouncing
int buttonState;
int lastButtonState;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 5;
void setup() {
pinMode(LED, OUTPUT);
pinMode(soundSensor, INPUT);
buttonState = digitalRead(soundSensor);
lastButtonState = buttonState;
Serial.begin(9600);
finishTime = millis() + period;
}
// Timer Code
void timer_start() {
// get current time
startTime = millis();
running = true;
Serial.print("Timer started... ms remaining: ");
Serial.println(period);
}
void debounce() {
int reading = digitalRead(soundSensor);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
}
}
lastButtonState = reading;
}
// MAIN LOOP
void loop() {
currentTime = millis();
debounce();
if (buttonState == HIGH) {
running = true;
}
if (running) {
// timer is running
if (buttonState == HIGH) {
Serial.print("Button Pressed.");
finishTime = millis() + period;
}
if (finishTime > currentTime) {
Serial.print("Timer ms remaining: ");
Serial.println(finishTime - currentTime);
} else {
Serial.println("Time Elapsed.");
running = false;
}
}
}