// Set up pins etc. here.
int ldr_pin = A0;
int mic_pin = A1;
int green_led = 3;
int red_led = 4;
int time = 0;
int milliseconds_passed;
void setup() {
Serial.begin(9600); //Just in case you want a proper serial out to read for debug or whatever...get rid once works.
pinMode(green_led, OUTPUT);
pinMode(red_led, OUTPUT);
}
//If there is activity on the LDR (i.e. screen flashes), run the soudcheck() function to get a time in milliseconds for the delay after the flash on screen.
void loop() {
// While there is no reading from the LDR, do nothing (wait)
while (analogRead(ldr_pin) < 100) {}
//Once loop is broken by a screen flash, get the milliseconds from the soundcheck() function.
time = soundcheck();
// if the returned time is more than 80ms, then light the red LED for half a second, otherwise light the green led for half a second.
if (time > 80) {
digitalWrite(red_led, HIGH);
delay(500);
}
else {
digitalWrite(green_led, HIGH);
delay(500);
}
}
//set a variable to the current number of milliseconds.
//while there is no activity on the microphone, sit and wait.
//Once there is activity on the mic, the while loop will break and the number of milliseconds is returned.
int soundcheck() {
int start_milliseconds = millis();
while (analogRead(mic_pin) < 100) {}
milliseconds_passed = millis() - start_milliseconds;
return milliseconds_passed;
}