Create variable:
state
This stores the sound sensor reading.
When Arduino starts up
Add:
forever
Inside forever add:
set state to digital read pin D2
Arduino continuously checks:
“Did the sensor detect clap sound?”
if state = LOW then
When clap sound occurs:
Inside IF block:
set digital pin 8 HIGH
LED turns ON when clap detected.
else
Inside:
set digital pin 8 LOW
Without sound:
LED remains OFF.
When Arduino starts up
forever
set state to digital read pin D2
if state = LOW then
set digital pin 8 HIGH
else
set digital pin 8 LOW