I’m still unsure why the loop() runs so slowly but this indicates some other things to look at …
This code semi-works. I say semi because if you close the serial monitor window when sleeping and then re-open it after awakening (listen for Windows to beep), then all looks good. You’ll see the awake message and the 10 sec count down begins again and looks ot be 10 secs. Yea ! But if you don’t open the serial monitor window and just do repeated sleep/awaken cycles you’ll see that the onboard LED does not blink on the 2’nd awakening. It waits until you open the serial monitor window and then starts the 10 sec count down. I’ll GUESS this is due to the while(!Serial){} loop statement but why does the 1’st re-awakening just run w/no window open. Odd :?
#include <avr/sleep.h>
int wakePin = 3; // pin used for waking up
int ledPin = 13; //onboard LED pin
bool blinkState = false; // variable to store a request for sleep
int count = 0; // counter
void wakeUpNow() // here the interrupt is handled after wakeup
{
//routine to properly wake up
//sleep_disable();
//delay(100);
//USBDevice.attach(); // keep this
//delay(100);
//Serial.begin(9600);
//delay(100);
//Serial.println("I am awake !");
}
void setup()
{
pinMode(wakePin, INPUT_PULLUP);
Serial.begin(9600);
delay(10000);
Serial.println("Immediately before attachInterupt");
attachInterrupt(0, wakeUpNow, LOW);
Serial.println("Immediately after attachInterupt");
}
void sleepNow() // here we put the arduino to sleep
{
// disable the USB
USBCON |= _BV(FRZCLK); //freeze USB clock
PLLCSR &= ~_BV(PLLE); // turn off USB PLL
USBCON &= ~_BV(USBE); // disable USB
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
//attachInterrupt(0, wakeUpNow, LOW);
sleep_mode();
//when awakened, code returns to here
//routine to properly wake up
sleep_disable();
delay(100);
USBDevice.attach(); // keep this
delay(100);
Serial.begin(9600);
delay(100);
while (!Serial) { }
Serial.println("I am awake !");
}
void loop()
{
Serial.print("Awake for ");
Serial.print(count);
Serial.println(" sec");
Serial.print("WakePin state is ");
Serial.println(digitalRead(wakePin));
Serial.print("Blink state is ");
Serial.println(blinkState);
count++;
blinkState = !blinkState;
digitalWrite(ledPin, blinkState);
delay(1000);
if (count >= 10) {
Serial.println("Timer: Entering Sleep mode");
delay(100);
count = 0;
sleepNow();
}
}
EDIT: My guess above is correct. If you comment out the while waiting for the serial port to become available, you’ll see the code runs w/o hesitation every time it is awakened.
I downloaded your code and it seems to be working. Goes to sleep, wakes up blinks the LED and goes to sleep again. It not not serious as my project doesn’t need the serial port, but once it goes to sleep the firs time, The com port usually doesn’t reappear when the arduino wakes up so no serial output, even with the while statement commented out. I have to reset the arduino to reprogram the pro micro. I am curious as to why the serial port doesn’t appear but it is not required for my project.
And so the next question is … what’s the current draw off the “battery” ? I’m not sure you have the circuitry to measure it but even a reduction in USB 5V power draw might be interesting.
To be honest … I’ve lost track of whatever “vision” I had some months ago for this project, esp re: power options. It’ll take me a day to get “spooled up” again.
I am trying to package up a version that I can use to test power consumption when in operation and in sleep mode. Hope to have to running this weekend. I have a voltmeter that is capable of measuring milliamp so i should be able to get some reasonable measurements. I have some minor tweaking on the blinking leds to match the existing handle more closely but nothing major. The last time I had the circuit assembled on the breadboard, I was quite happy with the test results.
Other tasks:
-
disassemble an existing stem assembly so I can get the dimensions of the small circuit board that will hold the hall sensor.
-
sketch out the dimensions of the ciruit board that will hold the arduino, LEDs and capacitance. I was thinking of adding a small step-up dc-dc converter. One option was rather than having the battery last 1-2 season, was to have the battery last for an event or two. We might be able to use a much smaller battery for that, If it was easy to replace and cheap that should be ok. I have played around with the CR123 we started off with. As we discussed, it was fine for development purposes but I can not fit it in the curling stone handle . I might be able to have a couple of coin batteries mounted to the back side of the circuit board though where they would be easy to replace.
The software mods you proposed to the get the pro micro to go to sleep worked as designed. I integrated them into my code for the handle and I think I have a software solution that meets the need. Now I have to focus on getting the power consumption down. Later this week I will convert over to the battery to power the pro micro so I can measure the power consumption when running vs sleep.
As I understand it the power regulator and the LEDs on the Pro micro need to be disabled to get the power consumption down. Is there an arduino design that would be a good starting point?