DrDiettrich:
It's better in loop(), because there it will not block further interrupts.
Would that be the case if I disabled the interrupts?
I have this preliminary code. It works, sends it to sleep and all. But the problem is that when I press the button (it does the right thing and displays the activity) and keep it pressed for more than a second it wakes up the arduino. I have no ideas why though. Any tips?
//Library for sleeping
#include <avr/sleep.h>
#include <EnableInterrupt.h>
//==========================START OF IGNORE============================
//Library for Display - Can skip it if you want!
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH 16
static const unsigned char PROGMEM logo16_glcd_bmp[] =
{ B00000000, B11000000,
B00000001, B11000000,
B00000001, B11000000,
B00000011, B11100000,
B11110011, B11100000,
B11111110, B11111000,
B01111110, B11111111,
B00110011, B10011111,
B00011111, B11111100,
B00001101, B01110000,
B00011011, B10100000,
B00111111, B11100000,
B00111111, B11110000,
B01111100, B11110000,
B01110000, B01110000,
B00000000, B00110000 };
#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
String activity1 = "Work";
//____________________________END OF IGNORE______________________________
//Variable setup fot Debounce
int debounceCounter111 = 0;
int buttonState = HIGH;
long lastDebounceTime = 0;
long debouncedelay = 50;
// Interrupt Service Routine
void wake ()
{
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)
{
sleep_disable();
disableInterrupt(8);
}
last_interrupt_time = interrupt_time;
}
void sleep()
{
// disable ADC
ADCSRA = 0;
//set_sleep_mode (SLEEP_MODE_STANDBY);
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
sleep_enable();
// Do not interrupt before we go to sleep, or the
// ISR will detach interrupts and we won't wake.
noInterrupts ();
// will be called when pin D2 goes low
////attachInterrupt (0, wake, FALLING);//
pinMode(8, INPUT_PULLUP);
enableInterrupt(8, wake, FALLING);
// turn off brown-out enable in software
// BODS must be set to one and BODSE must be set to zero within four clock cycles
MCUCR = bit (BODS) | bit (BODSE);
// The BODS bit is automatically cleared after three clock cycles
MCUCR = bit (BODS);
// We are guaranteed that the sleep_cpu call will be done
// as the processor executes the next instruction after
// interrupts are turned on.
interrupts (); // one cycle
sleep_cpu (); // one cycle
}
//===================================================================
//Display text on screen fucntion - You can skip.
void displayText(String activityName)
{
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(10,0);
display.clearDisplay();
display.println(activityName);
display.display();
delay(200);
display.clearDisplay();
display.display();
}
//=========================================================================
//Button Varibale int
int buttonTwo = 8;
///////++++++++++++++++++++SETUP++++++++++++++++++++++///
void setup ()
{
pinMode(buttonTwo, INPUT_PULLUP);
//DISPLAY SETUP - You can skip
// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
// init done
// Show image buffer on the display hardware.
// Since the buffer is intialized with an Adafruit splashscreen
// internally, this will display the splashscreen.
display.display();
delay(2000);
// Clear the buffer.
display.clearDisplay();
display.display();
}
//+++++++++++++++++++++++++++++LOOP+++++++++++++++++++++++++++//
void loop()
{
buttonpress();
}
void buttonpress(){
buttonState = digitalRead (buttonTwo);
if ((millis() - lastDebounceTime) > debouncedelay) {
if (buttonState == LOW) {
++debounceCounter111;
if (debounceCounter111 > 3){
debounceCounter111 = 1;
lastDebounceTime = millis();
sleep();
}
if (debounceCounter111 == 1){
displayText(activity1);
lastDebounceTime = millis();
sleep();
}
if (debounceCounter111 == 2){
displayText(activity1);
lastDebounceTime = millis();
sleep();
}
if (debounceCounter111 == 3){
displayText(activity1);
lastDebounceTime = millis();
sleep();
}
}
}
Serial.println(buttonState);
}