Hi: I have been working trying to built a data logger, I want to record the date and time when a device was turn on and off. I currently have the Arduino Uno with the data logger card (SD 4Gb card) configured and it stores data, I have connected a 12 volt device through a voltage regulator tha supplies an output of 5 volts and it starts recording the information when this is connected to a digital input on the Arduino (digital input 7). After the device is off it stops recording and when it is on it starts recording again. The problem that I have is that records for certain time and then it stops even though the device is on again, also in some cases it stores garbage.
I dont know what causes this, but any help is very well apreciated.
Regards
CarlosH.
Here is the code that I am using:
/* Programa para detectar un voltaje y Grabar con Tiempo y Hora la Actividad hasta que se el voltaje sea cero
*/
//Rutina para mandar llamar las librerias
#include "Wire.h"
#include <SD.h>
#define DS1307_ADDRESS 0x68
// Definicion de la variable del puerto Analogo para lectura del disparador
int Arranque = 7;
int chipSelect = 8;
int CS_pin = 10;
void setup ()
{
// habilitar el puerto serial para ver pantalla
Serial.begin(9600);
Serial.print("Initializing SD card...");
// Poner el pin de la tarjeta como salida
pinMode(CS_pin, OUTPUT);
Wire.begin();
// poner el pin 0 como entrada Analoga
pinMode(Arranque, INPUT);
if (!SD.begin(chipSelect))
{
Serial.println("");
Serial.println("Card Failed");
return;
}
Serial.println("");
Serial.println("Card Ready");
}
void loop()
{
if (digitalRead(Arranque) == HIGH)
{
printDate();
}
else
if (digitalRead(Arranque) == LOW)
{
Serial.println ("Motor Apagado ");
}
}
byte bcdToDec(byte val)
{
// Convert binary coded decimal to normal decimal numbers
return ( (val/16*10) + (val%16) );
}
void printDate()
{
// Reset the register pointer
Wire.beginTransmission(DS1307_ADDRESS);
byte zero = 0x00;
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
int second = bcdToDec(Wire.read());
int minute = bcdToDec(Wire.read());
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());
int year = bcdToDec(Wire.read());
//Formato de la fecha y hora 3/1/11 23:59:59
Serial.print(month);
Serial.print("/");
Serial.print(monthDay);
Serial.print("/");
Serial.print(year);
Serial.print(" ");
Serial.print(hour);
Serial.print(":");
Serial.print(minute);
Serial.print(":");
Serial.println(second);
// String para grabar info a archivo
String dataString = String(month) + "/" + String (monthDay) + "/" + String (year) + " " + String (hour) + ":" + String (minute) + ":" + String (second);
// Apertura y Nombre del archivo
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile)
{dataFile.println(dataString);
dataFile.close();}
}