Finally successfully wrote a sketch that logs the date and time to my microSD card when a button is pressed (usingĀ Real Time Clock from Adafruit).
The setup – microSD breakout board to left, RTC to the right
The results, printed to the Serial Monitor:
Right now, the sketch detects button states so only one timestamp is recorded when a button is pressed. Otherwise, multiple timestamps are taken for the duration the button is pressed down. However, now that I have added the RTC, I can’t get the sketch to work with more than one button, although I can get all my buttons to log data when I take out the button state code. So close, yet so far.
Include Real Time Clock and SD Libraries at the top. Declare RTC and dataFile as global variables. Write data to dataFile.
Initialize button and declare two button states. This sketch uses code from Arduino example statechangedetection by Tom Igoe.
// Date and time functions using DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include “RTClib.h”
#include <SPI.h>
#include <SD.h>
RTC_DS1307 rtc;
File dataFile;
const int buttonWater = 2; // pushbutton connected to digital pin 2
// Water – Button One
int buttonPushCounter = 0;
int buttonStateWater = 0; // current state of the water button
int lastButtonStateWater = 0; // previous state of the button
Initialize MicroSD card and RTC in Void Setup(). Set button as Input.
void setup()
Serial.begin(9600);
Wire.begin();
rtc.begin();
// Check that the microSD card exists and can be used
Serial.print(“Initializing SD card…”);
pinMode(10, OUTPUT);
if (!SD.begin(10)) {
Serial.println(“Card failed, or not present”);
while (1) ;
}
Serial.println(“card initialized.”);
if (! rtc.isrunning()) {
Serial.println(“RTC is NOT running!”);
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(__DATE__, __TIME__));
// Set my digital pins as inputs or outputs
pinMode(buttonWater, INPUT);
}
}
In Void Loop(), make a string to assemble the data. Read the button as an input. Compare the button states. If the button state goes from off to on, write the data to the dataFile once. Each piece of data that is needed is put into a string (separate strings for month, year, etc.) Date and time data is obtained using Real Time Clock functions. Put all the strings into dataString and write to microSD card. Close the microSD card. Compare button states and repeat if button state changes from previous state.
void loop () {
// Make a string for assembling the data to log:
String dataString = “”;
// RTC Time Function
DateTime now = rtc.now();
// Read the water pushbutton input pin
buttonStateWater = digitalRead(buttonWater);
// compare the buttonState to its previous state
if (buttonStateWater != lastButtonStateWater) {
if (buttonStateWater == HIGH) {
Serial.println(“Button one is pressed”);
// Create the file for writing the data
dataFile = SD.open(“datalog.txt”, FILE_WRITE);
// Assemble Strings to log data
String theyear = String(now.year(), DEC);
String mon = String(now.month(), DEC);
String theday = String(now.day(), DEC);
String thehour = String(now.hour(), DEC);
String themin = String(now.minute(), DEC);
//Put all the time and date strings into one String
dataString = String(“water, ” + theyear + “/” + mon + “/” + theday + “, ” + thehour + “:” + themin);
// Open the datafile and write the dataString to the microSD card
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
Serial.println(dataString);
}
// if the file isn’t open, register an error:
else {
Serial.println(“error opening datalog.txt”);
}
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonStateWater = buttonStateWater;
}
So happy to see all the progress! And all of it documented! Great job! As I suspected, you need multiple state change detection routines for all the buttons so you need to declare variables like you did here: int buttonStateWater = 0; // current state of the water button
int lastButtonStateWater = 0; // previous state of the button
for all your buttons and then add the state change detection routine for all of them! Hope this helps and enjoy the break! B_
Howdy Michelle,
I came across this project and became extremely excited as I am looking to do something nearly exactly the same. However I have been unsuccessful in my attempts at researching a solution to my problem. Because of this I was hoping you could tell me if you had made any changes and gotten the code to work or if I could have the original code that worked for a single button?
I would greatly appreciate any help you could give me and look forward to hearing from you!
Thank you!