Using RTC to get time on event

Hi All - I am new to Ardunio but am loving it. Have found out most answers by looking thru forums and looking at other programmers code, however I am a bit lost on this one. I have Arduino Uno, with a Ethernet module to help me record number of times a pump has run. However, I would like to add a RTC to show what time the pump was last run. Right now I am in beta/coding phase and am using a LED to represent the change in state (for a pump). What would the code be to record the RTC time upon a pin going HIGH, and then later recalling it for the ethernet look up - Hope this makes sense

I understand the need for the DateTime now = RTC.now() (see code) and also understand that it is calling upon the current time.

Also on a side note, is there a way to nest pinModes?

Using Analogue output for the LED’s

Code below

#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include “RTClib.h”

const int buttonPin = 7; // the pin that the pushbutton is attached to
int LED1 = A0;
int LED2 = A1;
int LED3 = A2;
int LED4 = A3;
int LED5 = 4;
int SW1 = 5;
int SW2 = 6;
int SW3 = 7;
int SW4 = 8;
int SW5 = 9;
int val1=0;
int val2=0;
int val3=0;
int val4=0;
int val5=0;

// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button

RTC_DS1307 RTC;

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 0, 177);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup() {
pinMode(LED1,OUTPUT);
pinMode(LED2,OUTPUT);
pinMode(LED3,OUTPUT);
pinMode(LED4,OUTPUT);
pinMode(LED5,OUTPUT);
pinMode(SW1, INPUT);
pinMode(SW2, INPUT);
pinMode(SW3, INPUT);
pinMode(SW4, INPUT);
pinMode(SW5, INPUT);

// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// Open serial communications and wait for port to open:
Serial.begin(9600);
Wire.begin();
RTC.begin();

if (! RTC.isrunning()) {
Serial.println(“RTC is NOT running!”);
}

// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}

void loop() {

word secs;
{
val1 = digitalRead(SW1); // read the input pin
digitalWrite(LED1, val1); // sets the LED to the button’s value
val2 = digitalRead(SW2);
digitalWrite(LED2, val2);
val3 = digitalRead(SW3);
digitalWrite(LED3, val3);
val4 = digitalRead(SW4);
digitalWrite(LED4, val4);

if (val1 == HIGH && val4 == HIGH) {

// Set time delay
for(secs=0; secs < 5; secs++)
delay(1000); // 1000 = 1sec

digitalWrite(LED5,HIGH);

else {
if (val1 == LOW && val4 == LOW) {
digitalWrite(LED5,LOW);
}
}
}

// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);

// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
// Serial.println(“on”);
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);

}
else {
// if the current state is LOW then the button
// wend from on to off:
// Serial.println(“off”);
}
}

lastButtonState = buttonState;

delay(200);

// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println(“new client”);
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you’ve gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == ‘\n’ && currentLineIsBlank) {
// send a standard http response header
client.println(“HTTP/1.1 200 OK”);
client.println(“Content-Type: text/html”);
client.println(“Connection: close”); // the connection will be closed after completion of the response
client.println(“Refresh: 5”); // refresh the page automatically every 5 sec
client.println();
client.println("");
client.println("");

client.print(“Test 1

Under House Pump

Pump has been on - “);
client.print(buttonPushCounter);
client.print(” times”);
client.print(" Last time pump was on ");

DateTime now = RTC.now()

client.print(now.day(), DEC);
client.print(’/’);
client.print(now.month(), DEC);
client.print(’/’);
client.print(now.year(), DEC);
client.print(’ ‘);
client.print(now.hour(), DEC);
client.print(’:’);
client.print(now.minute(), DEC);
client.print(’:’);
client.print(now.second(), DEC);
client.println();
client.println("");

break;
}
if (c == ‘\n’) {
// you’re starting a new line
currentLineIsBlank = true;
} else if (c != ‘\r’) {
// you’ve gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println(“client disconnected”);
}
}

I understand the need for the DateTime now = RTC.now()

What do you understand it is needed for?

What would the code be to record the RTC time upon a pin going HIGH

   byte pinState = digitalRead(pinNumber);
   if(pinState == HIGH && oldState == LOW)
   {
      pinWentHighAt = RTC.now();
   }

and then later recalling it for the ethernet look up

No idea what you mean by "the ethernet look up", but pinWentHighAt would contain the time and date that the pin went high. (Of course pinWentHighAt is a global variable of type DateTime.)

GranttNZ:
What would the code be to record the RTC time upon a pin going HIGH, and then later recalling it for the ethernet look up - Hope this makes sense

Data loggers that need to store big amounts of data typically store the data to a SD card.

So you could possibly use the onbloard micro-SD and the Arduino SD library to log data to log files. And operate the Arduino Ethernet as a webserver, from which you retrieve the data later.

Unfortunately an Arduino Ethernet is just based on the Atmega328 and therefore you will have to use advanced programming techniques to save RAM when doing all at the same time

  • operate RTC clock and timing
  • operate SD card for data logging
  • operate network for SD card webserver

But it should be possible for an experienced Arduino programmer.

Not sure about someone being “new to Arduno”, as those newbies tend to have difficulties in understanding many things needed for such programs like

  • writing non-blocking code doing “several things at the same time”
  • use RTC and time functions without using the biggest third-party time and RTC libraries
  • save as much RAM as possible
  • operate SD card webservers without wasting RAM using “String” class objects
  • and so on and so on

jurs:
Not sure about someone being "new to Arduno", as those newbies tend to have difficulties in understanding many things needed for such programs like

  • writing non-blocking code doing "several things at the same time"
  • use RTC and time functions without using the biggest third-party time and RTC libraries
  • save as much RAM as possible
  • operate SD card webservers without wasting RAM using "String" class objects
  • and so on and so on

There is more than one Time library? I think newbies are googling up the same one I did. It's an extremely compact piece of work. For example, the now() function is only 21 lines. Also the compiler only produces code for functions that are actually used. The whole thing including macros and constants (which don't use any memory) is barely a few pages. The only thing in it that might use much code is the external sync routine that belongs to the RTC code. It doesn't use any OO either.

Since you have done a comparison of RTC library RAM usage, perhaps you could share the specifics of that? Is there one that specifically attempts to minimize RAM usage?

Everything else I completely agree with.

If you just want to grab the time from the RTC, here is some “bare bones” code to do that:

#include <Wire.h>

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()
{
  // ask RTC for the time
  // 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());
    dd = bcd2bin(Wire.read());
    mo = bcd2bin(Wire.read());
    yy = bcd2bin(Wire.read());
    // show that we successfully got the time
    Serial.print("Got the time: ");
    printTime();
  }
  else {
    // if we're here, that means we were unable to read the time
    Serial.println("Unable to read time from RTC"); 
  }
  delay(500);
}

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("(");
  switch (wd) {
    case 1: Serial.print("Mon"); break;
    case 2: Serial.print("Tue"); break; 
    case 3: Serial.print("Wed"); break; 
    case 4: Serial.print("Thu"); break; 
    case 5: Serial.print("Fri"); break; 
    case 6: Serial.print("Sat"); break; 
    case 7: Serial.print("Sun"); break;
    default: Serial.print("Bad");  
  }
  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("");
}

This code reads the date and time, stores it in global variables (it should be obvious which variable is for what) and prints it to Serial.

Thanks to all that responded, there are a couple of responses I can use.

In regards to being a newbie, I apologize if I wasted anybodies time in looking at this request. I see that a few have posted some constructive criticism about 'newbies' hacking code we have found on the web without fully understanding it - sorry but we aren't all seasoned programmers.

Thanks PaulS - Look like what I needed