Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Variables
- volatile unsigned long times[] = {0, 0}; // Variable to hold last two times
- boolean state = false; // Variable to hold the state of the light
- void knock(){
- times[0] = times[1]; // Push out the old time,
- times[1] = millis(); // and insert a new one
- }
- void setup(){
- pinMode(2, INPUT); // Make the interrupt pin an input
- pinMode(13, OUTPUT); // Make the lamp pin an output
- attachInterrupt(0, knock, RISING); // If a clap is sensed, knock()
- }
- void loop(){
- if ( (times[0] != 0) && (times[1] != 0) ){ // If not default times
- if ((times[1] - times[0]) < 1000){ // And the times are close
- state = !state; // Flip the states
- times[0] = 0;
- times[1] = 0;
- }
- digitalWrite(13, state);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment