//declare the sound sensor pin connection
#define SOUND_PIN A0
//eight leds in total
#define LED_COUNT 8
//declare LED pin connections
int LED_PINS[] = {4,5,6,7,8,9,10,11};
//LED index
int thisLED = 0;
//voltage of the sound sensor
int sound_voltage = 0;
//number of the lit LEDS
int lit_LED = 0;
void setup() {
// put your setup code here, to run once:
//led pin-mode initialization
for(thisLED = 0; thisLED < LED_COUNT; thisLED++)
{
pinMode(LED_PINS[thisLED], OUTPUT);
}
}
void loop() {
// put your main code here, to run repeatedly:
//Read the voltage of the sound sensor
sound_voltage = analogRead(SOUND_PIN);
//quantify sound_voltage
lit_LED = map(sound_voltage, 0, 1023, 0, LED_COUNT);
//light the leds
for(thisLED = 0; thisLED < LED_COUNT; thisLED++)
{
if(thisLED < lit_LED)
digitalWrite(LED_PINS[thisLED],HIGH);
else
digitalWrite(LED_PINS[thisLED],LOW);
}
}