i using cheap ds3231 rtc.
reading ct current
using emon openenergymonitor
using sd card level shifter
lost 75 seconds overnight.
been searching , found 1 reference in 1 post said reading rtc effect it's timing. post said reads once day. i assume uses millis() , adjusts discrepancy when evaluating data.
i happy second day. or two....
my second problem have no display , if sd card fails, no joy, no notification, , pin13 keeps blinking writing.
reading ct current
using emon openenergymonitor
using sd card level shifter
lost 75 seconds overnight.
been searching , found 1 reference in 1 post said reading rtc effect it's timing. post said reads once day. i assume uses millis() , adjusts discrepancy when evaluating data.
i happy second day. or two....
my second problem have no display , if sd card fails, no joy, no notification, , pin13 keeps blinking writing.
code: [select]
// * analog sensors on analog ins 0, 1, , 2
// * i2c bus rtc on a4 , a5
// * sd card attached spi bus follows:
// ** mosi - pin 11
// ** miso - pin 12
// ** clk - pin 13
// ** cs - pin 10
// libraries
#include <sd.h>
#include <spi.h>
#include <emonlib.h>
energymonitor emon1; // create instance
// rtc initialization *********************************
#include "wire.h"
#define ds3231_i2c_address 0x68
byte dectobcd(byte val) // convert normal decimal numbers binary coded decimal
{
return ( (val / 10 * 16) + (val % 10) );
}
// convert binary coded decimal normal decimal numbers
byte bcdtodec(byte val)
{
return ( (val / 16 * 10) + (val % 16) );
}
// end rtc initialization ******************************
// pin assignments
// a1 assigned emon
int led50 = 6;
int led100 = 7;
const int chipselect = 10;
// global variables
int amps = 0;
double irms ;
int datalog = 0;
int old_datalog = 0;
float irmsdisplay ;
// fixed variables
void setup()
{
wire.begin();
serial.begin(9600);
pinmode(10, output);
pinmode(13, output);
emon1.current(1, 30); // 1 = pin a1. 30 = fixed, ct has internal resistor.
serial.print("initializing sd card...");
if (!sd.begin(chipselect)) { // see if card present , can initialized:
serial.println("card failed, or not present");
digitalwrite(13, high); // turn led on (high voltage level)
delay(2000);
return; // don't more:
}
serial.println("card initialized.");
}
// rtc stuff ********************************
void readds3231time(
byte *second,
byte *minute,
byte *hour,
byte *dayofweek,
byte *dayofmonth,
byte *month,
byte *year)
{
wire.begintransmission(ds3231_i2c_address);
wire.write(0); // set ds3231 register pointer 00h
wire.endtransmission();
wire.requestfrom(ds3231_i2c_address, 7);
// request 7 bytes of data ds3231 starting register 00h
*second = bcdtodec(wire.read() & 0x7f);
*minute = bcdtodec(wire.read());
*hour = bcdtodec(wire.read() & 0x3f);
*dayofweek = bcdtodec(wire.read());
*dayofmonth = bcdtodec(wire.read());
*month = bcdtodec(wire.read());
*year = bcdtodec(wire.read());
}
void displaytime()
{
byte second, minute, hour, dayofweek, dayofmonth, month, year;
// retrieve data ds3231
readds3231time(&second, &minute, &hour, &dayofweek, &dayofmonth, &month,
&year);
// concatenate writing sd card
string datastring = ""; // make string assembling data log:
datastring += string(hour, dec );
datastring += string(":");
if (minute < 10) datastring += string(0);
datastring += string(minute, dec);
datastring += string(":");
if (second < 10) datastring += string(0);
datastring += string(second, dec);
datastring += string(" " );
datastring += string(dayofmonth, dec );
datastring += string("/" );
datastring += string(month, dec );
datastring += string( "/");
datastring += string( year, dec);
datastring += string(", - " );
// add value ct sensor
datastring += (irmsdisplay);
datastring += " amps,";
file datafile = sd.open("datalog.txt", file_write);
// if file available, write it:
if (datafile) {
datafile.println(datastring);
datafile.close();
// print serial port too:
serial.println(datastring);
}
}
// end rtc stuff ********************************
void loop() {
// ***** emon
// double irms = emon1.calcirms(1480); // calculate irms // declared in inilitalization
irms = emon1.calcirms(1480); // calculate irms only
irmsdisplay = irms;
if (irms <= .25) { // added 0 dropout
irmsdisplay = 0;
}
// ******************************************************************************************
// following how pick time create flag on when write sd card.
if (amps >= 50) {
datalog = high;
digitalwrite(led50, low);
}
if (amps <= 40) {
datalog = low;
digitalwrite(led50, high);
}
if (amps >= 100) {
datalog = high;
digitalwrite(led100, low);
}
if (amps <= 80) {
datalog = low;
digitalwrite(led100, high);
}
if (datalog != old_datalog)
// data log routine
// final bits of code go here
old_datalog = datalog;
// ****** end emon *************************************************
displaytime(); // display real-time clock data on serial monitor,
delay(1000); // every second
serial.println();
// end rtc =======================================================
}
you bought either fake ds3231, or out of spec reject.
the genuine ones keep time 1-2 seconds/month.
the genuine ones keep time 1-2 seconds/month.
Arduino Forum > Using Arduino > Project Guidance > DS3231 loosing time
arduino
Comments
Post a Comment