Hi all,
Caveat: I'm a biologist trying to learn this stuff, so apologies for my ignorance. I'm building a low-light logger for underwater. I've got to the point where the unit works fine, but (unsurprisingly I gather) uses a lot of power. Ideally I'd like it to log for 5 minutes, then power down for 25, log again, etc so that it significantly reduces power consumption to the point where I can put it out for a week at least. I've explored options, and the simplest (but probably least elegant) is to loop an 8s low-power repeatedly. I can't get it off the ground though.
I'm using the low-power library from here: Sparkfun low power
This basic code works fine:
#include "LowPower.h"
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
// Enter idle state for 8 s with the rest of peripherals turned off
// Each microcontroller comes with different number of peripherals
// Comment off line of code where necessary
// ATmega328P, ATmega168
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
LowPower.idle(SLEEP_8S, ADC_OFF, TIMER2_OFF, TIMER1_OFF, TIMER0_OFF,
SPI_OFF, USART0_OFF, TWI_OFF);
}
And this is the basis of the code that I'm using for the logger:
volatile unsigned long cnt = 0;
unsigned long oldcnt = 0;
unsigned long t = 0;
unsigned long last;
void irq1()
{
cnt++;
}
void setup()
{
Serial.begin(115200);
Serial.println("START");
pinMode(2, INPUT);
digitalWrite(2, HIGH);
attachInterrupt(0, irq1, RISING);
}
void loop()
{
if (millis() - last >= 1000)
{
last = millis();
t = cnt;
unsigned long hz = t - oldcnt;
Serial.print("FREQ: ");
Serial.print(hz);
Serial.print("\t = ");
Serial.print((hz+50)/100); // +50 == rounding last digit
Serial.println(" mW/m2");
oldcnt = t;
}
}
Which works fine as well.
But if I add in the LowPower command I get gibberish, and no sleep, or just no sleep. For example this one give me gibberish (as in random symbols) without even any line breaks:
#include <LowPower.h>
volatile unsigned long cnt = 0;
unsigned long oldcnt = 0;
unsigned long t = 0;
unsigned long last;
void irq1()
{
cnt++;
}
void setup()
{
Serial.begin(115200);
Serial.println("START");
pinMode(2, INPUT);
digitalWrite(2, HIGH);
attachInterrupt(0, irq1, RISING);
}
void loop()
{
if (millis() - last >= 1000)
{
last = millis();
t = cnt;
unsigned long hz = t - oldcnt;
Serial.print("FREQ: ");
Serial.print(hz);
Serial.print("\t = ");
Serial.print((hz+50)/100); // +50 == rounding last digit
Serial.println(" mW/m2");
oldcnt = t;
}
LowPower.idle(SLEEP_8S, ADC_OFF, TIMER2_OFF, TIMER1_OFF, TIMER0_OFF,
SPI_OFF, USART0_OFF, TWI_OFF);
}
I assume I'm doing something pretty basic wrong, but am out of my depth. The gibberish seems similar to when I get the serial baud rate wrong?
Any help would be greatly appreciated
Thanks