AbstractBeliefs

Untitled

Sep 5th, 2011
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. // Variables
  2. volatile unsigned long times[] = {0, 0}; // Variable to hold last two times
  3. boolean state = false; // Variable to hold the state of the light
  4.  
  5. void knock(){
  6.   times[0] = times[1];  // Push out the old time,
  7.   times[1] = millis();  // and insert a new one
  8. }
  9.  
  10. void setup(){
  11.   pinMode(2, INPUT);    // Make the interrupt pin an input
  12.   pinMode(13, OUTPUT);  // Make the lamp pin an output
  13.   attachInterrupt(0, knock, RISING); // If a clap is sensed, knock()
  14. }
  15.  
  16. void loop(){
  17.   if ( (times[0] != 0) && (times[1] != 0) ){ // If not default times
  18.     if ((times[1] - times[0]) < 1000){ // And the times are close
  19.       state = !state; // Flip the states
  20.       times[0] = 0;
  21.       times[1] = 0;
  22.     }
  23.     digitalWrite(13, state);
  24.   }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment