Ok. So I'm in a pickle. Also, I'm a newbie, so bear with me.
I'm trying to set a activation for a relay using a 10 second sound detection, using a microphone with digital outputs.
Issue being is that the arduino, even when the microphone is held at max volume and the sensor light is illuminated, doesn't consistently read that the digital output is at "1" instead of "0". The higher the volume, it seems to help, but I'd rather have it consistent at any volume. Long wires are not an issue, and I've got the pentameter right on the cusp of the threshold.
Any advice would be great, and thanks in advance. (I've included a picture of the microphone I'm using, as well as a picture of my board.)
int sensorPin = 3;
int ledPin=13;
int sound =0;
int pushPin=9;
int buttonPin=7;
float pressLength_milliSeconds = 0;
float pressLength_milliSecondsmic = 0;
boolean val =0;
unsigned long previousMillis = 0;
const long interval = 1000;
unsigned long time_1 = 0;
void print_time(unsigned long time_millis);
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(pushPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(sensorPin, INPUT);
}
void loop() {
sound =digitalRead(sensorPin);
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
Serial.println (sound);
if ((millis() - time_1) > 4000)
{pressLength_milliSecondsmic = 0;
digitalWrite(ledPin, LOW);}
if (sound==HIGH){
digitalWrite(ledPin, HIGH);
time_1 = millis();
print_time(time_1);
pressLength_milliSecondsmic = pressLength_milliSecondsmic + 100;
Serial.print("micms = ");
Serial.println(pressLength_milliSecondsmic);}
if (pressLength_milliSecondsmic >= 1000) {
digitalWrite(pushPin, HIGH);}
if (pressLength_milliSeconds >= 500) {
digitalWrite(ledPin, HIGH);
digitalWrite(pushPin, HIGH);}
if (digitalRead(buttonPin) == LOW ){
delay (1000);
pressLength_milliSeconds = pressLength_milliSeconds + 100;
Serial.print("buttonms = ");
Serial.println(pressLength_milliSeconds);}
}
}
void print_time(unsigned long time_millis){
Serial.print("Time: ");
Serial.print(time_millis/1000);
Serial.print("s - ");
}