Commit d2e0581f authored by Patrick's avatar Patrick

EOD: collecting measurements

parent 01cefbee
......@@ -25,6 +25,13 @@ const char usnd_dtype[] PROGMEM = "STAGE";
const char usnd_units[] PROGMEM = "m";
const char usnd_perid[] PROGMEM = "INSTANT";
// set the distance measurements to calculate STAGE from the ultrasound sensor
#define SENSOR_TO_BED 10000 // mm
// and warn on compile if OOR is possible
#if SENSOR_TO_BED > 10000
#warning "stage readings can't reach zero"
#endif
// store each char array for datastream info as progmem strings
const char temp_param[] PROGMEM = "Temperature";
const char temp_quali[] PROGMEM = "2 min";
......@@ -66,7 +73,7 @@ uint8_t readings_tail = NUM_MMNT-1;
// 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 5
#define NUM_TO_SEND 2
#if NUM_TO_SEND >= NUM_MMNT
#error "Can't hold all the readings, reduce NUM_TO_SEND below NUM_MMNT"
......@@ -123,7 +130,7 @@ void sleep_idle() {
WDTCSR |= (1 << WDIE); // enable interrupt mode
sleep_enable(); // enable the sleep mode ready for use
// sleep is only for 4 seconds-ish, so lets run round a few times
for (uint8_t i=0; i<= 5; i++) {
for (uint8_t i=0; i<= 0; i++) {
sleep_mode(); // trigger the sleep
/* ...time passes ... */
}
......@@ -140,10 +147,9 @@ void sleep_idle() {
&& (current_time.mm != last_reading_time.mm)) {
// if it's an ordinal time 0|15|30|45 min take a reading
g_current_state = TAKE_READING;
//} else if ((readings_head == NUM_MMNT-1)
//&& (current_time.hh - last_sending_time.hh % 24 > 1)) {
} else if ((readings_head - readings_tail) % NUM_MMNT >= NUM_TO_SEND) {
// the readings buffer is full, send them out
//g_current_state = SEND_READING;
g_current_state = SEND_READING;
}
}
......@@ -152,16 +158,40 @@ void take_reading() {
Serial.print(F("take_reading() at: "));
delay(100);
#endif
t_SensorData current_reading = get_reading(TMP);
// create the distance variable to hold the stage reading
float stage = -1.0f;
t_SensorData current_reading = get_reading(ANA);
if (current_reading.sensor == ANA) {
stage = SENSOR_TO_BED - RP.distance(current_reading, MB7366);
// convert to meters
stage = stage / 1000.0f;
}
#if DEBUG_MODE > 0
RP.print_data(current_reading);
Serial.print(F("distance: "));
Serial.println(RP.distance(current_reading, MB7366));
Serial.print(F("STAGE: "));
Serial.println(stage, 3);
delay(100);
#endif
// update the current time
if (current_reading.sensor == TMP) {
// stage stage should always be positive
if (stage < 0) {
// add the reading to the buffer
readings_head = (readings_head + 1) % NUM_MMNT;
#if DEBUG_MODE > 0
Serial.print((readings_head - readings_tail) % NUM_MMNT);
Serial.println(F(" readings stored"));
#endif
measurements[readings_head].reading = 0; // stage measurement
measurements[readings_head].y = current_reading.y;
measurements[readings_head].m = current_reading.m;
measurements[readings_head].d = current_reading.d;
measurements[readings_head].hh = current_reading.hh;
measurements[readings_head].mm = current_reading.mm;
measurements[readings_head].ss = current_reading.ss;
measurements[readings_head].level = stage;
// update the current time
last_reading_time.y = current_reading.y;
last_reading_time.m = current_reading.m;
last_reading_time.d = current_reading.d;
......@@ -178,7 +208,12 @@ void take_reading() {
t_SensorData get_reading(sensor_e target) {
// get a reading
RP.sensorsOn();
delay(100);
if (target == ANA) {
delay(160); // power up start ranging
delay(148); // alow analog reading to settle
} else {
delay(100);
}
t_SensorData sensorData = RP.get(target);
RP.sensorsOff();
return sensorData;
......@@ -191,8 +226,14 @@ void send_reading() {
#endif
t_SensorData send_time = get_reading(TMP);
#if DEBUG_MODE > 0
RP.print_data(send_time);
Serial.print(F("sending "));
Serial.print((readings_head - readings_tail) % NUM_MMNT);
Serial.println(F(" readings"));
//RP.print_data(send_time);
delay(100);
#endif
// reset the buffer length
readings_tail = readings_head;
}
ISR( WDT_vect ) {
......
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