Hoping someone can help me figure out why the Hall Sensor (water flow) I have isn't able to wake the Arduino Micro from sleep. The below is a test sketch and the interrupt works fine during the program. I can shut the Arduino down, but it never wakes back up. Have tried Pins 7 and 3, and experimented with "RISING", "LOW", and "CHANGE". Could it be the Hall Sensor is changing state too quickly such that the wake up sequence doesn't have time to complete?
Here is the code:
#include "LowPower.h"
int flow;
/*
* Hall Effect Sensor Setup
*/
volatile int NbTopsFan;
int hallsensor = 3; //The pin location of the sensor (not all pins can be used as 'interrupts'
long int ds = 1000; //Number of Milliseconds to sample for during flow event
void rpm ()
{ //This is the function that the interupt calls
NbTopsFan++; //This function counts the number of revolutions hall effect sensors signal
}
void setup() {
Serial.begin(9600);
while(!Serial);
Serial.println("Initializing... ");
pinMode(hallsensor, INPUT);
attachInterrupt(0, rpm, LOW); //and the interrupt is attached
Serial.println("Flow Sensor Initialized>>>");
Serial.println("Setup complete!");
}
void loop() {
digitalWrite(LED_BUILTIN, LOW);
NbTopsFan = 0; //Set NbTops to 0 ready for calculations
flow = 0; //Clear the flow variables for next cycle
if(NbTopsFan > 0){
do{
digitalWrite(LED_BUILTIN, HIGH); //Status LED to show when flow is being recorded
fncReadSensor();
digitalWrite(LED_BUILTIN, LOW);
}while(NbTopsFan > 0);
}else{
fncSleep();
}
}
///////////////////////////////////////////////////////////////////////////////
// This function puts the arduino to sleep
void fncSleep()
{
Serial.println("fnc_sleep");
delay(500);
Serial.println("...entering sleep mode...");
delay(500);
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
///////
// Arduino seems to sleep fine but doesn't wake up.
//////
Serial.println("...waking up...");
}
///////////////////////////////////////////////////////////////////////////////
// This function reads the sensor for 1 second
void fncReadSensor()
{
NbTopsFan = 0; //Set NbTops to 0 ready for calculations
delay (ds); // period of time to count pulses
sei(); // Enables interrupts
flow = NbTopsFan; // Transfer value to a variable for future use.
Serial.print("Count: "); Serial.println(flow);
}
If you comment out the call for fncSleep() in the "Else" statement, everything runs just fine.
My goal is have the Micro sleep until flow is detected, then using an RTC for datestamp, log the flow counts to an SD Card while the flow is present. When flow stops, the units shuts down again to save battery power.