I have made a data logger, that is supposed to record the date and time when a button is pressed, to an SD card as a CSV file.
The time and date a reading correctly off the Sparkfun RTC circuit.
I have used a micro SD, that is 100% logging the files no problem.
It just logs them every time the program loops instead of when the button is "HIGH".
Here is the code:
#include <SD.h>
#include <SPI.h>
#include <Time.h>
#include "Wire.h"
#define DS1306_ADDRESS 0x68
byte zero = 0x00; //workaround for issue #527
/*
SD card read/write
The circuit:
- SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 10
*/
int button = 7;
File myFile;
void setup()
{
pinMode(button, INPUT);
Wire.begin();
Serial.begin(9600);
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(10, OUTPUT);
if (!SD.begin(10)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
}
void loop()
{
if (digitalRead(button)== HIGH) // if the button is pressed, take the date/time and store it
{
myFile = SD.open("Asthma.csv", FILE_WRITE);
// Reset the register pointer
Wire.beginTransmission(DS1306_ADDRESS);
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1306_ADDRESS, 7);
int second = bcdToDec(Wire.read()); //seconds
int minute = bcdToDec(Wire.read()); // minutes
int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
int monthDay = bcdToDec(Wire.read());
int month = bcdToDec(Wire.read()); //month
int year = bcdToDec(Wire.read()); //year
myFile.print(month);
myFile.print("/");
myFile.print(monthDay);
myFile.print("/");
myFile.print(year);
myFile.print(", ");
myFile.print(hour);
myFile.print(":");
myFile.print(minute);
myFile.print(":");
myFile.println(second); // print date/time to file
// close the file:
myFile.close();
Serial.println("Dose Taken");
}
delay(500);
}
byte decToBcd(byte val){
// Convert normal decimal numbers to binary coded decimal
return ( (val/10*16) + (val%10) );
}
byte bcdToDec(byte val) {
// Convert binary coded decimal to normal decimal numbers
return ( (val/16*10) + (val%16) );
}