help coding an hour meter

Hello,

I am new to Arduino and I am having a hard time figuring out how to create a hour meter. I am using an uno with an Ethernet/SD shield. I have that hooked up to sump pump and a transducer. I am able to see the liquid level and control the pump via web page. However, I would like to create a hour meter that records the pump run-time hours, and prints it on to the web page. Here is my code,

#include <SPI.h>

#include <Ethernet.h>

// 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,1,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);

*/ LEDpin is used to control relay

int LEDpin = 1;

String readString = String(30);

String state = String(3);

void setup()

{

// start the Ethernet connection and the server:

Ethernet.begin(mac, ip);

server.begin();

//Sets the LEDpin as an output

//LEDpin is actually for relay control

pinMode(LEDpin,OUTPUT);

digitalWrite(LEDpin,LOW);

state = “OFF”;

}

void loop()

{

// listen for incoming clients

EthernetClient client = server.available();

if (client) {

// an http request ends with a blank line

boolean currentLineIsBlank = true;

while (client.connected()) {

if (client.available()) {

char c = client.read();

// 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 (readString.length() < 30) {

readString.concat(c);

}

if (c == ‘\n’ && currentLineIsBlank) {

// send a standard http response header

int LED = readString.indexOf(“LED=”);

if (readString.substring(LED,LED+5) == “LED=T”) {

digitalWrite(LEDpin,HIGH);

state = “ON”;

}

else if (readString.substring(LED,LED+5) == “LED=F”) {

digitalWrite(LEDpin,LOW);

state = “OFF”;

}

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: 3”); // refresh the page automatically every 3 sec

client.println();

client.println(“”);

client.println(“”);

// Pump Name

client.println(“

KO6

”);

// output the value of the analog input pin

for (int analogChannel = 0; analogChannel < 1; analogChannel++) {

int sensorReading = analogRead(analogChannel);

sensorReading = map(sensorReading, 0, 1024, 0, 300);

client.print(" Liquid Level ");

client.print(" is ");

client.print (“”);

client.print(sensorReading);

client.print (“</font color>”);

client.println("
");

client.print(" Hours: ");

//client.print(runtime)

client.println("
");

}

client.print("Pump is ");

client.print(state);

client.print("

");

if (state == “ON”) {

client.println(“<a href="./?LED=F">Turn Off”);

}

else {

client.println(“<a href="./?LED=T">Turn On”);

}

client.print(“”);

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

readString = “”;

// close the connection:

client.stop();

}

Sorry if my code is sloppy, I am still learning, if anyone could help me it would be greatly appreciated.

Thanks

What is the problem? Is it not working? If so, what are the symptoms, error messages and results found in your browser.

Please enter your code between [ code] [ /code] tags, or use the code button shown in the Full Message Editor. That makes your code easier to read.

I’ve found the following syntax errors so far that the compiler complains about:

*/ LEDpin is used to control relay

/ is the end-marker of a multi-line comment. So you should write / there, and */ at the end of the line (of the last comment). Or, since you likely only intended to make a single comment line, use // instead at the beginning of that line.

Also, somewhere you are missing a closing curly bracket. So the compiler thinks the code is unfinished. (i.e. void loop() is still open) It’s placement will define the execution of your sketch. If not placed in the right position then the code goes haywire. I have no experience in the interaction between html-server and html-client so I cannot suggest a place.

Indenting your code helps to make it readable which parts are at the same hierarchy level. Usually each code part inside a new set of curly brackets is indented a tab more on the next line. And an indentation is removed after the closing curly bracket. It’s not a requirement, as the compiler doesn’t care about spaces, tabs and new lines. But it helps you tremendously keeping an overview on your code.

See: http://en.wikipedia.org/wiki/Indent_style

[EDIT] My suggestion, without fixing the missing ending curly bracket, since I do not know where you intended to put it:

#include <SPI.h>
#include <Ethernet.h>

// 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,1,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);

// LEDpin is used to control relay
int LEDpin = 1;
String readString = String(30);
String state = String(3);

void setup()
{
	// start the Ethernet connection and the server:
	Ethernet.begin(mac, ip);
	server.begin();

	//Sets the LEDpin as an output
	//LEDpin is actually for relay control
	pinMode(LEDpin,OUTPUT);

	digitalWrite(LEDpin,LOW);
	state = "OFF";
}
void loop()
{
	// listen for incoming clients
	EthernetClient client = server.available();
	if (client) {
		// an http request ends with a blank line
		boolean currentLineIsBlank = true;
		while (client.connected()) {
			if (client.available()) {
				char c = client.read();
				// 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 (readString.length() < 30) {
					readString.concat(c);
				}
				if (c == '\n' && currentLineIsBlank) {
					// send a standard http response header
					int LED = readString.indexOf("LED=");
					if (readString.substring(LED,LED+5) == "LED=T") {
						digitalWrite(LEDpin,HIGH);
						state = "ON";
					}
					else if (readString.substring(LED,LED+5) == "LED=F") {
						digitalWrite(LEDpin,LOW);
						state = "OFF";
					} 
					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: 3"); // refresh the page automatically every 3 sec

					client.println();
					client.println("<!DOCTYPE HTML>");
					client.println("<html><body bgcolor='Gray'>");
					// Pump Name
					client.println("<center><H1>KO6</H1></center>");
					// output the value of the analog input pin
					for (int analogChannel = 0; analogChannel < 1; analogChannel++) {
						int sensorReading = analogRead(analogChannel);
						sensorReading = map(sensorReading, 0, 1024, 0, 300);
						client.print("<center> Liquid Level ");
						client.print(" is ");
						client.print ("<b><font color='blue'>");
						client.print(sensorReading);
						client.print ("</b></font color>");
						client.println("
");
						client.print(" Hours: ");
						//client.print(runtime)
						client.println("
"); 
					}


					client.print("Pump is ");
					client.print(state);
					client.print("

");

					if (state == "ON") {
						client.println("<a href=\"./?LED=F\">Turn Off<a>");
					}
					else {
						client.println("<a href=\"./?LED=T\">Turn On<a>");
					}
					client.print("</center>");
					client.println("</html>");
					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);
		readString = "";
		// close the connection:
		client.stop();
	}

// There is still a }-bracket missing somewhere.

I take it that “populating” the variable runtime is the issue ? Can’t the millis() function be used to calculate the pump on time ? And then accumulate those for a total runtime in every X hours/days/weeks ?

http://arduino.cc/en/Reference/Millis

//whenever pump is turned on
startTime = millis();
...
//whenever pump is turned off
stopTime = millis();
onTime = (stopTime - startTime)/3600000L;  //save pump on time as hours
runtime += onTime;                         //stores total pump run time in hours

Don’t forget to declare runtime, etc .

(click on to open)

Thank u guys for your responses,

I apologize for my posting edict. I copied and pasted the code in and lost my formatting.

I am really liking your thinking of just declaring mills() at start and stop and subtracting the to for total hours for the day. I could probably write that to the eprom. I will be playing around with that this week. I’ll let you guys know how it comes out.

Thanks