clap on/off light not working w/ vibration sensor


i trying create program uses piezo vibration sensor turn light on/off. wall light i  using servo flip switch. when clap want turn light on , wait clap. want turn light off. vibration sensor works , when goes above threshold triggers servo, in first if statement. seems happens takes same reading , applies second if statement, have servo move when clap once. tried fiddling timing go below threshold before sets go = 2, won't work. ideas?


#include <servo.h>
int groundpin= 0;
int sensepin = 1;
int go = 1;
servo light;


int threshold= 1000;


void setup()
{
serial.begin(9600);
pinmode(groundpin, output);
digitalwrite(groundpin, low);
light.attach(9);
}


void loop()
{
int reading = analogread(sensepin);
serial.println(sensepin);
if((reading > threshold) && (go == 1))
 {   
   light.write(180);
   delay(2000);
   go = 2;
}
serial.println(sensepin);
if((reading > threshold) && (go == 2))
 {   
   light.write(0);
   delay(2000);
   go = 1;
   delay(2000);
}
delay(2000);
}

with 12 posts, should know better. go , read nick gammon's post @ top of forum on proper way post source code using code tags. you'll more responses if do.

edit: can edit post


Arduino Forum > Using Arduino > Programming Questions > clap on/off light not working w/ vibration sensor


arduino

Comments

Popular posts from this blog

Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode - Raspberry Pi Forums

class MPU6050 has no member named begin

missing filename after '-o'