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");
}
}