Commit 306d5e96 authored by Patrick's avatar Patrick

and with sleep mode and less frequent sending

parent 1673159f
// set > 0 to get debug serial output
#define DEBUG_MODE 1
// choose processor sleep mode (1 = on)
#define SLEEP_MODE_ON 1
#include <avr/interrupt.h>
#include <avr/power.h>
#include <avr/sleep.h>
#include <red_peg.h>
red_peg RP;
#include <teal_mallet_gsm.h>
......@@ -35,7 +41,7 @@ state_machine_e g_current_state = SLEEP_IDLE;
t_SensorData last_reading_time;
#define ORDINAL_INTERVAL 1
#define NUM_MMNT 5 // measurement buffer length
#define NUM_MMNT 10 // measurement buffer length
// then create a pre-sized array of these structs to hold a set of timestamped measurements
uint8_t num_mmnt = NUM_MMNT;
t_measurement measurements[NUM_MMNT] = {};
......@@ -46,9 +52,9 @@ uint8_t readings_tail = 0;
// sends happen after a fixed number of ordinals
t_SensorData last_sending_time;
// how many readings should be collected for sending (<NUM_MMNT)
#define NUM_TO_SEND 3
#define NUM_TO_SEND 10
#if NUM_TO_SEND >= NUM_MMNT
#if NUM_TO_SEND > NUM_MMNT
#error "Can't hold all the readings, reduce NUM_TO_SEND below NUM_MMNT"
#endif
......@@ -96,7 +102,27 @@ void loop() {
void sleep_idle() {
// is it time to take a reading?
delay(4000); // simulate sleep
#if SLEEP_MODE_ON > 0
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // select the watchdog timer mode
MCUSR &= ~(1 << WDRF); // reset status flag
WDTCSR |= (1 << WDCE) | (1 << WDE); // enable configuration changes
WDTCSR = (1<< WDP0) | (1 << WDP1) | (1 << WDP2); // set the prescalar = 7
WDTCSR |= (1 << WDIE); // enable interrupt mode
sleep_enable(); // enable the sleep mode ready for use
#if DEBUG_MODE > 0
Serial.print(F("sleep..."));
delay(50);
#endif
// sleep is only for 4 seconds-ish
sleep_mode(); // trigger the sleep
/* ...time passes ... */
sleep_disable(); // prevent further sleeps
#if DEBUG_MODE > 0
Serial.println(F("wake!"));
#endif
#else
delay(4000); // simulate sleep
#endif
// get the time from the RTC
t_SensorData current_time = get_reading(RTC);
......@@ -115,10 +141,6 @@ void sleep_idle() {
}
void take_reading() {
#if DEBUG_MODE > 0
Serial.print(F("take_reading() at: "));
#endif
// create the distance variable to hold the stage reading
float temp = -50.0f; // start with OOR
t_SensorData current_reading = get_reading(TMP);
......@@ -126,6 +148,7 @@ void take_reading() {
temp = RP.degC(current_reading);
}
#if DEBUG_MODE > 0
Serial.print(F("take_reading() at: "));
RP.print_data(current_reading);
Serial.print(F("STAGE: "));
Serial.println(temp, 3);
......@@ -187,7 +210,7 @@ void send_reading() {
Serial.print(((readings_head + NUM_MMNT) - readings_tail) % NUM_MMNT);
Serial.print(F(" readings ("));
Serial.print(readings_tail);
Serial.write('');
Serial.write(':');
Serial.print(readings_head);
Serial.println(F(")"));
delay(100);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment