Can't turn off led with second clap

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;

void setup() {
pinMode(kSensorPower, OUTPUT);
pinMode(kPinLed, OUTPUT);
pinMode(kSensorSound, INPUT);
}

void loop()
{
// power up Sensor Power
digitalWrite (kSensorPower, HIGH);
delay (2000);

if (digitalRead(kSensorSound) == LOW)
// if sound is detected
{
//change state
if (digitalRead(kPinLed == LOW))
{
delay(1000);
digitalWrite (kPinLed, HIGH);
}
else
{
delay(1000);
digitalWrite (kPinLed, LOW);
}
}

// reset SensorPower
digitalWrite (kSensorPower, LOW);
delay (2000);
// turns on at beginning
}

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.

I commented out all the delays.

Works better, in that sense.

Original problem still pending.

Thanks!
E

This is how NOT to

    if (digitalRead(kPinLed == LOW))

do a digitalRead.

I do not want to waste time explaining what this actually does, but it is probably not what was intended.
Perhaps what was intended was closer to

    if (digitalRead(kPinLed) == LOW)

You are right!

This is my latest version...
(loop routine).

It's "kind of" working...
There are moments where the led blinks;
other times, I successfully turn it on and off.

Thanks!

void loop()
{
// power up Sensor Power
digitalWrite (kSensorPower, HIGH);
//delay (2000);

if (digitalRead (kSensorSound) == LOW)
// if sound is detected
{
//change state
if (digitalRead (kPinLed) == LOW)
{
// delay(1000);
digitalWrite (kPinLed, HIGH);
}
else
{
// delay(1000);
digitalWrite (kPinLed, LOW);
}
}

// reset SensorPower
digitalWrite (kSensorPower, LOW);
//delay (2000);
// turns on at beginning
}

It kind of works.
I have a 100ms delay after sensor power on, and same after off.

Seems to require more attempts to turn off.


const int kSensorPower = 7;
const int kPinLed = 9;
const int kSensorSound = 12;

void setup() {
pinMode(kSensorPower, OUTPUT);
pinMode(kPinLed, OUTPUT);
pinMode(kSensorSound, INPUT);
}

void loop()
{
// power up Sensor Power
digitalWrite (kSensorPower, HIGH);
delay (100);

if (digitalRead(kSensorSound) == LOW)
// if sound is detected
{
//change state
if (digitalRead(kPinLed) == LOW)
{
digitalWrite (kPinLed, HIGH);
}
else
{
digitalWrite (kPinLed, LOW);
}
}

// reset SensorPower
digitalWrite (kSensorPower, LOW);
delay (100);
// turns on at beginning
}

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.

I’m using the last version of the code, but with 1000 ms delay vs. 100.
It seems to be working.

I think there’s an issue with the sensitivity of the sensor.
(My hands are starting to hurt from clapping, LOL).

I adjusted the potentiometer to the most sensitive setting.
(Before it starts blinking).

I’m enclosing an image of the sensor.

I think the code might be ok now.

Thanks… bests!
E

EE-SHT114.jpg

Why do you need to reset the sensor in every loop()? Is it some kind of debounce to prevent flashing of the light?

Using millis() you could easily have it on full time and have a minimum on/off-time of 5, 10, 20 seconds or whatever suits your need.

Why reset in every loop...

When I activate it with sound, its led stays on.
I then saw that power off, then on, resets it.
Thus, ready to listen again.

Bests.
E

Working nicely, now!

1000 ms delay in the two chosen points.
Pointed the microphone better, for it to better receive sound.

Thanks for the valuable feedback and tips.

Have a great evening.
E

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.

Hi,

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.

Thanks.. Tom... :slight_smile: