Hello,
I discovered that if I have a global SoftwareSerial object with .begin called in setup() and a write() called in loop(), then LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
does not work But if I declare the SoftwareSerial object in loop(), then all works well. It seems that a live SoftwareSerial object is somehow triggering an interrupt to wake it up. Any thoughts? This is with a Nano.
Thank you.
// test.ino
#include "LowPower.h"
#include <SoftwareSerial.h>
#include <Wire.h>
#include "SSD1306Ascii.h"
#include "SSD1306AsciiWire.h"
// if global, powerDown returns almost immediately. But this is only
// a problem after I call HC12.write() at least once.
//SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin
// pins
#define PIR 2
#define MW 3
#define RX_BUFFER_SIZE 32
char buffer[RX_BUFFER_SIZE];
SSD1306AsciiWire display;
#define I2C_ADDRESS 0x3C
volatile int PIRcounter = 0;
volatile int MWcounter = 0;
int loopCounter = 0;
void Display(int x,int y,char *message)
{
display.setCursor((x*5)+1,y);
display.println(message);
}
void setup()
{
// HC12.begin(9600);
pinMode(PIR, INPUT);
pinMode(MW, INPUT);
Wire.begin();
Wire.setClock(400000L);
display.begin(&Adafruit128x32, I2C_ADDRESS);
display.setFont(Adafruit5x7);
display.clear();
delay(2000);
Serial.println("Starting...");
Display(0,0,buffer);
}
// two rising interrupt pin handlers
void HandleMWInterrupt()
{
MWcounter++;
}
void HandlePIRInterrupt()
{
PIRcounter++;
}
void loop()
{
attachInterrupt(digitalPinToInterrupt(PIR), HandlePIRInterrupt, RISING);
attachInterrupt(digitalPinToInterrupt(MW), HandleMWInterrupt, RISING);
// Enter power down state with ADC and BOD module disabled.
// Wake on interrupt.
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
// should only get here after an external pin interrupt
// Disable external pin interrupt on wake up pin.
detachInterrupt(digitalPinToInterrupt(PIR));
detachInterrupt(digitalPinToInterrupt(MW));
// with a global HC12 object, loop counter increments continuously and
// the other counters do not. This means the interrupts are NOT being
// called but powerDown is just returning after about a second.
loopCounter++;
sprintf(buffer,"%d - %d - %d ",loopCounter,PIRcounter, MWcounter);
Display(0,1,buffer);
// if I move the HC12 object declaration here, then all is well. If I use
// the global object, it does not work. This change will work for me but
// I am wondering why this is needed???
SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin
HC12.begin(9600);
HC12.write("TEST");
}