This is my code, I have a potentiometer to control the threshold, and I have a 2 way switch (which controls the switch case statement). When I have the switch in one direction it lights an LED so I can test out the threshold and get things just right, when I'm satisfied with the threshold, I switch the switch the other way to activate the flash.
int threshold;
int current;
int setting;
int potpin = 0;
int flashpin = 13;
int mic = 5;
int switch1 = 3;
int testpin = 12;
void setup() {
// put your setup code here, to run once:
}
void loop()
{
threshold = analogRead(potpin);
threshold = map(threshold, 0, 1023, 0, 2000);
current = analogRead(mic);
setting = digitalRead(switch1);
switch (setting) {
case LOW:
threshold = analogRead(potpin);
threshold = map(threshold, 0, 1023, 0, 2000);
current = analogRead(mic);
if (current > threshold) {digitalWrite(testpin, HIGH);
delay(100);}
else digitalWrite(testpin, LOW);
break;
case HIGH:
threshold = analogRead(potpin);
threshold = map(threshold, 0, 1023, 0, 2000);
current = analogRead(mic);
if (current > threshold) {digitalWrite(flashpin, HIGH);
delay(100);
digitalWrite(flashpin, LOW);}
break;
}
}