Need Help, Teensy 3.2 Snooze library. Can't wake from sleep with pushbutton.

Status
Not open for further replies.

Waterme11on

Active member
Hey there!
So i'm trying to wrestle the functionality I want out of the snooze library, but I can't for the life of me figure out how it works.

I've replaced the bounce1 library & code with the bounce2 library equivalent, and that seems to work in the examples.
I have a pushbutton connected to pin 22, the "3 second hold" example works, but any modification I make to the button functionality just doesn't want to work.

Here's the code I would like to work, there are 3 'modes' that pressing the button cycles through, and I want the last mode to be off/sleep, and pressing the button while in sleep will wake up and go back to mode 1.
However as it stands the teensy will stay asleep and not wake up after reaching the sleep mode.

Code:
#include <Snooze.h>

#include <Bounce2.h>
#define BUTTON_PIN_1 22

// Instantiate a Bounce object
Bounce debouncer1 = Bounce();


// Load drivers
SnoozeDigital digital;// this is the pin wakeup driver
// configures the lc's 5v data buffer (OUTPUT, LOW) for low power
Snoozelc5vBuffer lc5vBuffer;

// install driver into SnoozeBlock
SnoozeBlock config_teensy3x(digital);

int pushCounter = 1;
bool printed = false;

void setup() {
  Serial.begin(9600);

  //bounce2 stuff
  // Setup the first button with an internal pull-up :
  pinMode(BUTTON_PIN_1, INPUT_PULLUP);
  // After setting up the button, setup the Bounce instance :
  debouncer1.attach(BUTTON_PIN_1);
  debouncer1.interval(5); // interval in ms
  
  digital.pinMode(BUTTON_PIN_1, INPUT_PULLUP, RISING);

  Serial.println("start...");
  delay(20);
}

void loop() {

  //get button state
  debouncer1.update();
  
  //if button pressed increment counter
  if (debouncer1.rose()) { 
    printed = false;
    pushCounter++;
  }
  
  if (pushCounter > 3) { //3 mode rollover
    pushCounter = 1;
  }
  
  //select modes
  switch (pushCounter) {
    case 1:
      mode1();
      break;
    case 2:
      mode2();
      break;
    case 3:
      sleepTime();
      break;
  }



}//end loop

void mode1() {
  if (!printed) { //message play once check
    Serial.println("mode1");
    printed = true;
    delay(100);
  }
  //stuff can go here
}

void mode2() {
  if (!printed) {
    Serial.println("mode2");
    printed = true;
    delay(100);
  }
  //stuff can go here
}

void sleepTime() { //i.e offMode
  if (!printed) {
    Serial.println("SleepTime");
    printed = true;
    delay(100);
  }
  debouncer1.update();
  //this is the sleep function
  Snooze.hibernate( config_teensy3x );
}

I know there's some kind of interaction between Millis being turned off, and the snooze library. Is that what the problem is?


I modified the 3 second Hold example, with bounce 2, and the switch for changing modes. This one confused me even more, it will wake from sleep once, cycle through modes until asleep again, and then not wake up.
not sure what's happening here either. It proves that the teensy CAN be woken up, but something is stopping the same from happening twice.

Code:
#include <Snooze.h>
#include <Bounce2.h>

int buttonPin = 22;
#define BUTTON_PIN_1 22

// Instantiate a Bounce object
Bounce debouncer1 = Bounce();

// Load drivers
SnoozeDigital digital;// this is the pin wakeup driver
// configures the lc's 5v data buffer (OUTPUT, LOW) for low power
Snoozelc5vBuffer lc5vBuffer;
SnoozeUSBSerial usb;

SnoozeBlock config_teensy3x(usb, digital);


int pushCounter = 0;
bool printed = false;

void setup() {
  //bounce2
  // Setup the first button with an internal pull-up :
  pinMode(BUTTON_PIN_1, INPUT_PULLUP);
  // After setting up the button, setup the Bounce instance :
  debouncer1.attach(BUTTON_PIN_1);
  debouncer1.interval(5); // interval in ms
  
  digital.pinMode(BUTTON_PIN_1, INPUT_PULLUP, FALLING);

  // debug led
  pinMode(LED_BUILTIN, OUTPUT);
  while (!Serial);
  delay(100);
  Serial.println("start...");
  delay(20);

}

void loop() {
  // if not held for 3 sec go back here to sleep.
SLEEP:
  // you need to update before sleeping.
  //get button state
  debouncer1.update();

  //int value1 = debouncer1.read();

  //sleep here
  Snooze.hibernate( config_teensy3x );

  // indicate the button woke it up, hold led high for as long as the button
  // is held down.

  elapsedMillis timeout = 0;
  // bounce needs to call update longer than the debounce time = 5ms,
  // which is set in constructor.
  while (timeout < 6) debouncer1.update();

  // now check for 3 second button hold
  bool awake = buttonPress();

  // if not held for 3 seconds go back to sleep
  if (!awake) goto SLEEP;  //if awake, then go past this point.


  while (1) {
    debouncer1.update();

    //if button pressed
    if (debouncer1.rose()) { //digitalRead(BUTTON_PIN_1) == LOW //value1 == LOW
      printed = false;
      pushCounter++;
    }

    if (pushCounter > 3) { //3 modes
      pushCounter = 1;
    }

    //select modes
    switch (pushCounter) {
      case 1:
        mode1();
        break;
      case 2:
        mode2();
        break;
      case 3:
        if (!printed) {
          Serial.println("SleepTime");
          printed = true;
          delay(100);
        }
        goto SLEEP;
        break;
    }
  }//end while
}//end loop

bool buttonPress() {
  // this is the button press check
  while (debouncer1.duration() < 50) {

    // get the current pin state, must have this!
    debouncer1.update();

    if (debouncer1.read() != 0) {
      // let go of button before 50ms up
      return false;
    }
  }
  // button was pressed so now we are awake
  return true;

}//end check

void mode1() {
  //Serial.begin(9600);
  Serial.println("mode1");
}

void mode2() {
  // Serial.begin(9600);
  Serial.println("mode2");
}

Also is there anything i need to look out for when using the Audio library with snooze?
 
I managed to get the second code I posted to work, by resetting pushCounter to 0 before returning true in the buttonPress boolean. this wakes the teensy and resets to mode 1.
However the code is still weirdly formatted and I don't understand why it wakes whilst my first example doesn't.

I'm currently trying to implement a software restart in order to get the bootloader working after sleep.
 
Status
Not open for further replies.
Back
Top