/* Derived from Ed Mallon's starter logging code.
C. Fastie modified to run without sleeping anything.
added BME280 sensor, added seconds to time readout
added unixtime
https://publiclab.org/notes/cfastie/04-30-2017/data-logger-shield-for-nano
*/
/*
#include <SdFat.h>
#include <SPI.h>
#include <Wire.h>
#include <RTClib.h>
SdFat SD;
#define MOSIpin 11
#define MISOpin 12
RTC_DS1307 RTC;
#define DS1307_I2C_ADDRESS 0x68
char TmeStrng[] = "0000/00/00,00:00:00"; //19 ascii characters
void setup() {
Serial.begin(9600); // Open serial communications and wait for port to open:
Wire.begin(); // start the i2c interface
RTC.begin(); // start the RTC
RTC.adjust(DateTime((__DATE__), (__TIME__))); //sets the RTC to the computer time when the sketch is loaded
Serial.print("Find SD card: ");
if (!SD.begin(10)) {
Serial.println("Card failed");
return;
}
Serial.println(" SD card OK");
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) { // if the file is available, write to it:
dataFile.println("Nano Logger");
dataFile.close();
}
else {
Serial.println("file error"); // if the file isn't open, pop up an error:
}
} // end of setup
void loop() {
DateTime now = RTC.now(); //this reads the time from the RTC
sprintf(TmeStrng, "%04d/%02d/%02d,%02d:%02d:%02d", now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second()); // [added seconds]
Serial.print("RTC utc Time: ");
Serial.print(now.unixtime());
Serial.println();
Serial.print("RTC time: ");
Serial.println(TmeStrng);
Serial.println();
//Construct a data string to write to SD card
String dataString = ""; //this erases the previous string
dataString += TmeStrng;
dataString += ",";
dataString += String(now.unixtime());
dataString += ",";
//write the data to the SD card
File dataFile = SD.open("datalog.txt", FILE_WRITE); // if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
}
else {
Serial.println("file error"); // if the file isn't open, pop up an error:
}
delay(3000); // write data every 3 seconds
} // END of the MAIN LOOP
*/
/*
#include <SdFat.h> // https://github.com/greiman/SdFat/
#include <SPI.h>
#include <Wire.h>
#include <RTClib.h> // library from https://github.com/adafruit/RTClib
int ledPin = 3;
int wakePin = 2;
//sleepStatus is set up to keep track of the button input on pin 12.
int sleepStatus = 0;
RTC_DS1307 RTC; // The real time clock object is "RTC"
#define DS1307_I2C_ADDRESS 0x68
SdFat SD; // The SdFat object is "SD"
#define MOSIpin 11 // For SD card
#define MISOpin 12 // For SD card
const int chipSelect = 10; // CS pin for the SD card
char tmeStrng[ ] = "0000/00/00,00:00:00"; // a template for a data/time string
long utc;
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(wakePin, INPUT_PULLUP);
/* Next we have to enable an interrupt.
The function is set up like this attachInterrupt(pin, function, triggerMode)
PIN – can be either a 0 to call out digital pin 2 or 1 to call out digital pin 3.
FUNCTION – This is the function that will be run while in the interrupt
TRIGGER MODE – this will be the mode of the interrupt pin.
It can be one the following:
LOW – a low level trigger
CHANGE – a change in level trigger
RISING – a rising edge trigger
FALLING – a falling edge trigger
The IDLE sleep mode is the only mode that can use CHANGE, RISING, and FALLING modes.*/
/*
attachInterrupt(0, wakeUpNow, LOW);
wakeUpNow();
}
void sleepNow()
{
//print message to serial monitor to let the user know board has gone to sleep
Serial.println("going to sleep");
//delay is added to allow user to get the full message on the serial monitor before going to sleep
delay(500);
//enables the sleep mode
sleep_enable();
// This is where we enable the interrupt, the reason it is done here is so that if the button is pressed accidently it doesn’t interrupt the running program.
attachInterrupt(0,wakeUpNow, CHANGE); //LOW
/* The next line is where we choose the sleep mode we want to use for this code. There are a few options to choose from, each with their own uses. For more information on the sleep modes, please review the Atmega8 datasheet at [http://ww1.microchip.com/downloads/en/DeviceDoc/ATmega48A-PA-88A-PA-168A-PA-328-P-DS-DS40002061A.pdf](http://ww1.microchip.com/downloads/en/DeviceDoc/ATmega48A-PA-88A-PA-168A-PA-328-P-DS-DS40002061A.pdf)
The 5 different options for sleep modes, they are listed below from least power savings to most power savings:
SLEEP_MODE_IDLE
SLEEP_MODE_ADC
SLEEP_MODE_PWR_SAVE
SLEEP_MODE_STANDBY
SLEEP_MODE_PWR_DOWN
For this sketch, we will be using the most power savings possible so we choose SLEEP_MODE_PWR_DOWN */
/*
//sleep mode is set here
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
//This is where the device is actually put to sleep
sleep_mode();
//Here is where the device begins to wake up.
//First thing that is done is to disable the sleep mode
sleep_disable();
//disables the interrupt on pin 2 so the wakeUpNow code will not be executed during normal run time
detachInterrupt(0);
//wait 1 second so the user can notice the LED signaling the interrupt
delay(1000);
digitalWrite (ledPin, LOW);
}
void wakeUpNow() //This is the code that runs when the interrupt button is pressed and interrupts are enabled
{
digitalWrite(ledPin, HIGH);
delay(100);
//Ici faire les choses suivantes :
// - Se connecter au RTC
// - Se connecter à la SD
// - Créer le fichier s'il n'existe pas
// - Lire l'état de la pin d'interrupt
// - Si la ligne est créée et que la pin est sur high : terminer la ligne et retour chariot
// - Si la ligne est vide et que la pine est sur low : créer le début de la ligne
// - Si la ligne est créée et que la pin est sur low : ignorer et envoyer un message d'erreur
// - Si la ligne n'est pas créée et que la pine est sur high : ignorer et envoyer un message d'erreur
// - Se déconnecter de la carte SD et du RTC
// - Envoyer un message de statut
// - Attendre 500 ms
// - Repartir en veille
sleepNow();
}
void loop()
{
sleepNow();
}
*/
// ================================== Rajouter un software debounce =============================