Hello everyone,
im fairly new to the topic of programming Boards, especially I'm new to C. Through research on this site and other sites I've managed to put my MKR WAN 1300 to Deep Sleep. At the moment, the Board sleeps for a minute, wakes up to blink 2 times and goes back to sleep afterwards for a minute till it wakes up again to blink 2 times again. Now, I want to realize a temperature reading inbetween the 2 blinks of the Board LED while it's awake. This will be some useful knowledge for me, because I want to use LoRaWAN later on to send temperature measurements to the The Things Network. I already managed to do that with my Board, but I want to realize a Deep Sleep inbetween it's readings.
My code for the temperature reading with the DS18B20 temperature sensor:
/********************************************************************/
// libraries
#include <OneWire.h>
#include <DallasTemperature.h>
/********************************************************************/
// Data wire is plugged into pin 6 on the Arduino
#define ONE_WIRE_BUS 6
/********************************************************************/
// Setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
/********************************************************************/
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
/********************************************************************/
void setup(void)
{
// start serial port
Serial.begin(9600);
Serial.println("Dallas Temperature IC Control Library Demo");
// Start up the library
sensors.begin();
}
void loop(void)
{
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
/********************************************************************/
Serial.print(" Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperature readings
Serial.println("DONE");
/********************************************************************/
Serial.print("Temperature is: ");
Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"?
// You can have more than one DS18B20 on the same bus.
// 0 refers to the first IC on the wire
delay(1000);
}
My code for realizing the Deep Sleep with the board (Reference 1; Reference 2)
#include <RTCZero.h>
/* Create an rtc object */
RTCZero rtc;
/* Change these values to set the current initial time */
const byte seconds = 0;
const byte minutes = 00;
const byte hours = 00;
/* Change these values to set the current initial date */
const byte day = 24;
const byte month = 9;
const byte year = 16;
bool matched = false;
void setup()
{
delay(10000); //delay so we can see normal current draw for a timespan e.g. 10 seconds
pinMode(LED_BUILTIN, OUTPUT); //set LED pin to output
digitalWrite(LED_BUILTIN, LOW); //turn LED off
rtc.begin(); //Start RTC library, this is where the clock source is initialized
rtc.setTime(hours, minutes, seconds); //set time
rtc.setDate(day, month, year); //set date
rtc.setAlarmTime(00, 00, 10); //set alarm time to go off in 10 seconds
//following two lines enable alarm, comment both out if you want to do external interrupt
rtc.enableAlarm(rtc.MATCH_HHMMSS); //set alarm
rtc.attachInterrupt(alarmMatch); //creates an interrupt that wakes the SAMD21
//puts SAMD21 to sleep
rtc.standbyMode(); //library call
//samSleep(); //function to show how call works
}
void loop() {
if (matched) {
matched = false;
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
int alarmMinutes = rtc.getMinutes();
alarmMinutes += 1;
if (alarmMinutes >= 60) {
alarmMinutes -= 60;
}
rtc.setAlarmTime(rtc.getHours(), alarmMinutes, rtc.getSeconds());
rtc.standbyMode(); // Sleep until next alarm match
}
}
//functions
void samSleep()
{
// Set the sleep mode to standby
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
// SAMD sleep
__WFI();
}
void alarmMatch() {
matched = true;
}
Now I've combined both codes like that, so that the serial monitor shows me some temperature readings (on paper):
//libraries
#include <OneWire.h>
#include <DallasTemperature.h>
#include <RTCZero.h>
// Data wire is plugged into pin 6 on the Arduino
#define ONE_WIRE_BUS 6
// Setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
/********************************************************************/
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
/********************************************************************/
/* Create an rtc object */
RTCZero rtc;
/* Change these values to set the current initial time */
const byte seconds = 0;
const byte minutes = 00;
const byte hours = 00;
/* Change these values to set the current initial date */
const byte day = 24;
const byte month = 9;
const byte year = 16;
bool matched = false;
void setup()
{
delay(10000); //delay so we can see normal current draw for a timespan e.g. 10 seconds
pinMode(LED_BUILTIN, OUTPUT); //set LED pin to output
digitalWrite(LED_BUILTIN, LOW); //turn LED off
// setup Alarm for waking the board up from it's deep sleep
rtc.begin(); //Start RTC library, this is where the clock source is initialized
rtc.setTime(hours, minutes, seconds); //set time
rtc.setDate(day, month, year); //set date
rtc.setAlarmTime(00, 00, 10); //set alarm time to go off in 10 seconds
//following two lines enable alarm, comment both out if you want to do external interrupt
rtc.enableAlarm(rtc.MATCH_HHMMSS); //set alarm
rtc.attachInterrupt(alarmMatch); //creates an interrupt that wakes the SAMD21 which is triggered by a FTC alarm
// setup temperature readings
// start serial port
Serial.begin(9600);
Serial.println("Dallas Temperature IC Control Library Demo");
// Start up the library
sensors.begin();
//puts SAMD21 to sleep
rtc.standbyMode(); //library call
//samSleep(); //function to show how call works
}
void loop() {
if (matched) {
matched = false;
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
/********************************************************************/
Serial.print(" Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperature readings
Serial.println("DONE");
/********************************************************************/
Serial.print("Temperature is: ");
Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"?
// You can have more than one DS18B20 on the same bus.
// 0 refers to the first IC on the wire
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
int alarmMinutes = rtc.getMinutes();
alarmMinutes += 1;
if (alarmMinutes >= 60) {
alarmMinutes -= 60;
}
rtc.setAlarmTime(rtc.getHours(), alarmMinutes, rtc.getSeconds());
rtc.standbyMode(); // Sleep until next alarm match
}
}
// functions
void samSleep()
{
// Set the sleep mode to standby
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
// SAMD sleep
__WFI();
}
void alarmMatch() {
matched = true;
}
This code doesnt work though. The LED draws current for 10 seconds in the beginning, then the LED goes out for a few ms and immediately lights up again and it stays like that. I appreciate any advise or help!