SHARE
TWEET

Untitled

a guest Aug 15th, 2015 16 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <avr/sleep.h>
  2. #include <avr/power.h>
  3. #include <EEPROM.h>
  4.  
  5. /* http://donalmorrissey.blogspot.cz/2011/11/sleeping-arduino-part-4-wake-up-via.html
  6.  * http://playground.arduino.cc/Main/TimerPWMCheatsheet
  7.  * http://www.avrbeginners.net/architecture/timers/timers.html#tcnt
  8.  */
  9.  
  10. #define LED_PIN (13)
  11.  
  12. volatile int f_timer = 0;
  13.  
  14. volatile int uptime = 0;
  15.  
  16. volatile char pass = 0;
  17.  
  18. volatile char alarm = 0;
  19.  
  20. int eepromptr = 0;
  21.  
  22. // history size = average over last 5 strokes
  23. #define hist 5
  24. // interrupt speed in Hz
  25. #define fhz 10
  26. // timer value for fhz
  27. #define tcnt 65535-25000
  28. // pin where siren is attached
  29. #define siren 5
  30.  
  31.  
  32. int prevpass[hist];
  33.  
  34. // timer interrupt vector, called fhz-times per second
  35. ISR(TIMER1_OVF_vect)
  36. {
  37.   if(f_timer == 0)
  38.   {
  39.     f_timer = 1;
  40.   }
  41.   TCNT1=tcnt;
  42. }
  43.  
  44. // suspend to low power mode
  45. void enterSleep(void)
  46. {
  47.   set_sleep_mode(SLEEP_MODE_IDLE);
  48.  
  49.   sleep_enable();
  50.  
  51.   /* Now enter sleep mode. */
  52.   sleep_mode();
  53.  
  54.   /* The program will continue from here after the timer timeout*/
  55.   sleep_disable(); /* First thing to do is disable sleep. */
  56. }
  57.  
  58. // interrupt handler - detected change
  59. void strobe()
  60. {
  61.   // debounce
  62.   if(prevpass[hist-1]+1 < uptime) {
  63.     pass = 1;
  64.   }
  65. }
  66.  
  67. void setup()
  68. {
  69.   Serial.begin(9600);
  70.  
  71.   pinMode(LED_PIN, OUTPUT);
  72.   pinMode(siren, OUTPUT);
  73.  
  74.   pinMode(3, OUTPUT);
  75.   digitalWrite(3, LOW);
  76.  
  77.   pinMode(2, INPUT_PULLUP);
  78.   attachInterrupt(0, strobe, FALLING);
  79.  
  80.   /* Normal timer operation.*/
  81.   TCCR1A = 0x00;
  82.  
  83.   // timer counter
  84.   TCNT1=tcnt;
  85.  
  86.   // prescaler
  87.   TCCR1B = 0x03;
  88.  
  89.   /* Enable the timer overlow interrupt. */
  90.   TIMSK1=0x01;
  91.  
  92.   power_adc_disable();
  93.   power_spi_disable();
  94.   power_timer0_disable();
  95.   power_timer2_disable();
  96.   power_twi_disable();  
  97.  
  98.  
  99.   /* Dump EEPROM. We assume computer might be connected, saving the data... */
  100.   Serial.println("---");
  101.   for(int i = 0; i<512; i++) {
  102.     byte b = EEPROM.read(i);
  103.     Serial.print(i, DEC);
  104.     Serial.print(" ");
  105.     Serial.println(b, DEC);
  106.   }
  107.   Serial.println("---");
  108.  
  109. }
  110.  
  111. char cond(int delta) {
  112.   return ( delta > fhz*hist && delta < 3*fhz*hist );
  113. }
  114.  
  115. void loop()
  116. {
  117.   if(f_timer==1)
  118.   {
  119.     f_timer = 0;
  120.     uptime++;
  121.    
  122.     /* Decide whether we want to trigger */
  123.     int delta = uptime - prevpass[0];
  124.     if(pass) {
  125.       if ( cond(delta) ) { // trigger on 20-60 RPM
  126.         alarm = 1;
  127.       }
  128.      
  129.       memmove(prevpass, prevpass+1, 2*(hist-1));
  130.       prevpass[hist-1] = uptime;
  131.       pass = 0;
  132.     }
  133.     if(!cond(delta)) {
  134.       alarm = 0;
  135.     }
  136.     if( (uptime-prevpass[hist-1]) > fhz*3 ) {
  137.       alarm = 0;
  138.     }
  139.    
  140.     /* Pulse siren on alarm=1 */
  141.    
  142.     if(alarm) {
  143.       if(uptime % 7 < 2) {
  144.         digitalWrite(siren, 1);
  145.       } else {
  146.         digitalWrite(siren, 0);
  147.       }
  148.     }
  149.     if(!alarm) {
  150.       digitalWrite(siren, 0);
  151.     }
  152.    
  153.     /* Save data for stats - say, every 10 seconds (about 1.5 hours of recording) */
  154.    
  155.     if(uptime % 100 == 99 && eepromptr < 512) {
  156.       EEPROM.write(eepromptr++, delta);
  157.     }
  158.    
  159.     digitalWrite(LED_PIN, !digitalRead(LED_PIN));
  160.    
  161. /*    for(int i=0; i<5; i++) {
  162.       Serial.print(prevpass[i], DEC);
  163.       Serial.print(" ");
  164.     }
  165.     Serial.print("delta ");
  166.     Serial.print(delta, DEC);
  167.     Serial.print(", alarm ");
  168.     Serial.print(alarm, DEC);
  169.     Serial.print(", up ");
  170.     Serial.println(uptime, DEC);*/
  171.     enterSleep();
  172.   }
  173. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top