With Arduino Uno, I start.
I clap, sound sensor activates (gives low value), led turns on.
Light on sound sensor turns off (successful reset).
Second time clapping, led does not turn off.
I'm doing something wrong in my code.
Thanks in advance for the help!
E
const int kSensorPower = 7;
const int kPinLed = 9;
const int kSensorSound = 12;
I'm puzzled about how it could work at all. I guess you must time your first clap very carefully to avoid it coinciding with one of the delay() statements in your code.
You have not told us much about the sound sensor so I can only make guesses here.
I hope that you realize that this:
digitalWrite (kSensorPower, LOW);
is going to be very quickly followed by this:
digitalWrite (kSensorPower, HIGH);
Is that what you want? If the sound sensor does not get reset, then this:
if (digitalRead (kSensorSound) == LOW)
// if sound is detected
{
//change state
if (digitalRead (kPinLed) == LOW)
{
digitalWrite (kPinLed, HIGH);
}
else
{
digitalWrite (kPinLed, LOW);
}
}
is going to be executed very quickly, and it is going to be executed two or more times, causing the LED to appear to blink.
6v6gt was properly concerned about the delay(...) statements in your code. I agree, but the solution involves knowing your sensor and using millis(), not just getting rid of the delay(...) statements.
easaad10:
It's "kind of" working...
There are moments where the led blinks;
other times, I successfully turn it on and off.
When you clap, how long does the clap last for? I mean - it won't be instantaneous: it will take some fraction of a millisecond. Without the delays, there's a chance that a clap will last for two 'loops' of the loop - that it will straddle one execution and the next. This will cause the led to be turned on and then back off again immediately.
Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.