Sleep for 300ms

Sleep for 300ms
« on: July 22, 2019, 11:04:41 AM »
Hey Tiny... Duiners!?

I'm doing a fairly simple virtual pet "game" using TinyScreen+ (Video Player kit). It's mostly short processed videos of our pet in an interactive shell so that it's kind of "alive" and has "needs" (tamagotchi like). Still WIP, I have a strict deadline for a gift to my wife :))

Would be neat to make it less power hungry. How do you make TinyScreen+ sleep for, say, 300ms (consuming very minimal power)? I think I figured out how to do deep sleep interrupted by press of a button:

    attachInterrupt(TSP_PIN_BT1, blink, LOW);
    attachInterrupt(TSP_PIN_BT2, blink, LOW);
    attachInterrupt(TSP_PIN_BT3, blink, LOW);
    attachInterrupt(TSP_PIN_BT4, blink, LOW);
    delay(500); // Just to ignore the already pressed button for 0.5s
I'm pretty new at C++ and embedded. Looks as if this works (does it look so to you?), but still measuring the battery life with and without this mode on. What I cannot figure out though, is how to make it wake up by itself, say in 300ms? I know I need to crank up some magic clocks like TC5 and fiddle with some register, but I decided to not read the entire datasheet for SAMD21 just yet and ask if maybe some one has a routine handy for sleeping? And all of that is assuming that the built-in "delay" function is not good. I looked at its source code and it looks like it's just a `while` loop, so must be burning through these mAh pretty quick.

Willing to keep doing my research, but hit a roadblock for now, tough to get oriented in the world of C++ in a couple of weeks (I'm a .NET dev)

Re: Sleep for 300ms
« Reply #1 on: July 22, 2019, 01:58:14 PM »
Hmm... I think I found a good sample on how to set up an RTC clock alarm here:

Not sure if it's useful for my SAMD21 as it mentions AVR... But can be useful in more ways than one as I can also benefit from being able to tell time to regulate the life of the creature. Will try to incorporate the RTC alarm interrupt into my code and post my own answer with the snippet of what I end up with, if it's working.

Re: Sleep for 300ms
« Reply #2 on: July 27, 2019, 03:51:47 PM »
Found it. The sleep() function can be found in this Arduino library:

Works perfectly on my TinyScreen+ (Atmel SAM D21). Also depends on a very handy RTCZero library that has all the methods needed to manipulate the clock and set alarms. That's how it wakes the device up, using the RTC clock, which limits the minimum deep sleep duration to 1 second. Can't guarantee waking up faster than 1 second using this method.
« Last Edit: August 03, 2019, 04:53:14 PM by evilkos »