Sound sesnor turns on after LED turns off


hi.
i bought sound sensor https://www.sparkfun.com/products/12642
i hooked this:
sensor    arduino
gnd   -> gnd
vcc    -> 5v
gate  -> digital pin 7

gate returns 0 or 1, simple push button - 0 when sound not reach treshold , 1 if does. worked espected, wanted add led on board. attached on digital pin 10 , gnd. , programmed of course without delay function escape additional problems.
problem: everytime when led turns off, sound detector somehow detects sound:

here code:
code: [select]
// sound sensor

const int sensorpin = 7;


int clapcount = 0;
int sensorstate = 0;
int lastsensorstate = 0;

unsigned long prevclap = 0;
unsigned long currentclap;
unsigned long timebetweenclaps = 0;


// led

const int ledpin =  10;
int ledstate = low;
unsigned long previousmillis = 0;

const long interval = 1000;


void setup() {

  pinmode(sensorpin, input);
    pinmode(ledpin, output);

  serial.begin(9600);

}


void loop() {
  
  // led

  unsigned long currentmillis = millis();

  if (currentmillis - previousmillis >= interval) {

    previousmillis = currentmillis;

    // if led off turn on , vice-versa:
    if (ledstate == low)
      ledstate = high;
    else
      ledstate = low;

    // set led ledstate of variable:
    digitalwrite(ledpin, ledstate);
    
  }
  

  // sound sensor

  sensorstate = digitalread(sensorpin);
  currentclap = millis();

  if (sensorstate != lastsensorstate) {

    if (sensorstate == high) { // if hands clapped

      timebetweenclaps = currentclap - prevclap;
      clapcount++;
      serial.print("clap: ");
      serial.println(clapcount);
      serial.println(timebetweenclaps);
      serial.println();

      prevclap = currentclap;

    }


  }

  lastsensorstate = sensorstate;

}


here in serial monitor if there silence in room:
code: [select]
clap: 1
2000

clap: 2
2000

clap: 3
2000

clap: 4
2000


can please help? problem? thanks.

update:
i changed voltage 5v 3.3 (specs say: 3.5 , 5.5 volts. 5 volts ideal) , works.
could mean there noise or after led turns off?
please explain me. curious.

your code looks fine me..


Arduino Forum > Using Arduino > Programming Questions > Sound sesnor turns on after LED turns off


arduino

Comments