//Make red button do somthing. // And sleep in low power mode when not. #include #include // Pin 11 has an BRB LED // Pin 2 is BRB. int led = 11; int buttonpin = 2; volatile unsigned int count = 1; // How many times has the button been pressed + 1 to give inital flash. volatile char ledmode = 0; // Are we displaying, sleeping, waiting or what. // 0 = Normal. 1 = sleeping. 2 = Display count, unsigned int deadcount = 1; // How long to be 'dead' for. In watchdog intrupts. volatile char buttonwake = 0; // the setup routine runs once when you press reset: void setup() { // initialize the digital pin as an output. pinMode(led, OUTPUT); // digitalWrite (2, HIGH); // Pullup for button. Now has external resistor. // attachInterrupt(0, redpush, LOW); // This is now done before sleeping. } // watchdog interrupt ISR (WDT_vect) { wdt_disable(); // disable watchdog } // end of WDT_vect void myWatchdogEnable (const byte interval) { MCUSR = 0; // reset various flags WDTCSR |= 0b00011000; // see docs, set WDCE, WDE WDTCSR = 0b01000000 | interval; // set WDIE, and appropriate delay wdt_reset(); byte adcsra_save = ADCSRA; byte prr_save = PRR; ADCSRA = 0; // disable ADC PRR = 0xFF; // turn off various modules set_sleep_mode (SLEEP_MODE_PWR_DOWN); // sleep mode is set here attachInterrupt(0, redpush, LOW); sleep_mode (); // now goes to Sleep and waits for the interrupt // Should wake up from here no matter what happens. // Possably after first running the intrupt routine? sleep_disable(); ADCSRA = adcsra_save; // stop power reduction PRR = prr_save; } // end of myWatchdogEnable // Should run evert time it wakes. void loop() { if(buttonwake == 1) { count++; // Button pushes +1 ledmode = 2; // And we want to display the flashes. buttonwake = 0; } // What to do now we are awake. switch(ledmode) { case 1:// 'hide' mode ledmode = 0; // Will be an 'if'. deadcount--; // 'hide' mode // code - Not implimented yet. break; case 2: // Count mode: Fast Flashes. ledmode = 0; // deadcount=10; // How long to 'seep' for. for (unsigned int i=count; i != 0; i--){ flashled(50); } break; default: // Normal mode: Slow flash. ledmode = 0; flashled(100); break; } digitalWrite(13, HIGH); // delay(50); // Green 'pin 13' LED flash. digitalWrite(13, LOW); // while( digitalRead(buttonpin)==LOW){ digitalWrite(13, HIGH); delay(10); } digitalWrite(13, LOW); // // attachInterrupt(0, redpush, LOW); // delay(500); // Half second delay before sleeping again. myWatchdogEnable (0b100001); // 8 seconds // detachInterrupt (0); // sleep bit patterns: // 1 second: 0b000110 // 2 seconds: 0b000111 // 4 seconds: 0b100000 // 8 seconds: 0b100001 } void flashled(int flashspeed) { digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) delay(flashspeed); // wait digitalWrite(led, LOW); // turn the LED off by making the voltage LOW delay(flashspeed+flashspeed); // Wait twice as long } // When the big red button is pushed. This is what we are here for folks! void redpush() { detachInterrupt (0); wdt_disable(); // disable watchdog buttonwake = 1; } // Old code. May delete. /* // count++; // Button pushes +1 // ledmode = 2; // And we want to display the flashes. // // wake (); // Bring micro out of sleep. static unsigned long last_interrupt_time = 0; unsigned long interrupt_time = millis(); // If interrupts come faster than 200ms, assume it's a bounce and ignore if (interrupt_time - last_interrupt_time > 200) { count++; // Button pushes +1 ledmode = 2; // And we want to display the flashes. wake (); } last_interrupt_time = interrupt_time; */