Thank you sterretje as a rookie that was something I never noticed.
Also thanks for the tip about scripts in text. Was unaware of that.
odometer
many thanks. That is exactly what I was looking for.
I have been trying to tweak it to include day and month output.
The attached revisal of your code does work but gives 2 outputs which would have to be compared with the
"&&" (eg If (output A==???? ) && (output B==????) {Pin,??????? HIGH}
Is there a better way to integrate both outputs into one number?
#include <Wire.h>
// variables for storing the time
// second minute hour weekday date month year
byte ss=0, mi=0, hh=0, wd=6, dd=1, mo=1, yy=0;
void setup()
{
Wire.begin();
Serial.begin(9600);
// clear /EOSC bit
// Sometimes necessary to ensure that the clock
// keeps running on just battery power. Once set,
// it shouldn't need to be reset but it's a good
// idea to make sure.
// Wire.beginTransmission(0x68); // address DS3231
// Wire.write(0x0E); // select register
// Wire.write(0b00011100); // write register bitmap, bit 7 is /EOSC
// Wire.endTransmission();
}
void loop()
{
// read the time from the RTC, if we can
boolean gotTheTime = grabTime();
if (gotTheTime) {
// if we are here, then the time has been successfully read
// and stored in global variables (ss, mi, hh, wd, dd, mo, yy)
Serial.print("Got the time: ");
printTime();
}
else {
// if we are here, then we tried to read the time but couldn't
Serial.println("Unable to read time from RTC");
}
Serial.print(" Value of month variable (mo) is: ");
Serial.println(mo, DEC);
Serial.print(" Value of days variable (dd) is: ");
Serial.println(dd, DEC);
Serial.print(" Value of hours variable (hh) is: ");
Serial.println(hh, DEC);
Serial.print(" Value of minutes variable (mi) is: ");
Serial.println(mi, DEC);
int packedTime = (100 * ((int)hh)) + ((int)mi);
int packedTime2 = ( 100* (( int)dd) + ((int)mo)); // Pack day and month
Serial.print(" Value of packedTime is: ");
Serial.println(packedTime, DEC);
Serial.println("");
Serial.print(" Value of packedTime2 is: ");
Serial.println(packedTime2, DEC); // Print pack value of day and month
Serial.println("");
delay(1000);
}
boolean grabTime() {
// get time from the RTC and put it in global variables
// send request to receive data starting at register 0
Wire.beginTransmission(0x68); // 0x68 is DS3231 device address
Wire.write((byte)0); // start at register 0
Wire.endTransmission();
Wire.requestFrom(0x68, 7); // request seven bytes (ss, mi, hh, wd, dd, mo, yy)
// check for a reply from the RTC, and use it if we can
if (Wire.available() >= 7) {
// if we're here, we got a reply and it is long enough
// so now we read the time
ss = bcd2bin(Wire.read()); // get seconds
mi = bcd2bin(Wire.read()); // get minutes
hh = bcd2bin(Wire.read()); // get hours
wd = bcd2bin(Wire.read()); // get day of week
dd = bcd2bin(Wire.read()); // get day of month
mo = bcd2bin(Wire.read()); // get month
yy = bcd2bin(Wire.read()); // get year (two digits)
// indicate that we successfully got the time
return true;
}
else {
// indicate that we were unable to read the time
return false;
}
}
byte bcd2bin(byte x) {
// converts from binary-coded decimal to a "regular" binary number
return ((((x >> 4) & 0xF) * 10) + (x & 0xF)) ;
}
void printTime() {
// just like it says on the tin
Serial.print ("\'");
if (yy<10) Serial.print("0"); Serial.print(yy,DEC); Serial.print("-");
if (mo<10) Serial.print("0"); Serial.print(mo,DEC); Serial.print("-");
if (dd<10) Serial.print("0"); Serial.print(dd,DEC); Serial.print(" ");
if (hh<10) Serial.print("0"); Serial.print(hh,DEC); Serial.print(":");
if (mi<10) Serial.print("0"); Serial.print(mi,DEC); Serial.print(":");
if (ss<10) Serial.print("0"); Serial.print(ss,DEC); Serial.println("");
}
/[code]
Thank you for your help