element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Need to learn how to code hour meter.
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Verified Answer
  • Replies 18 replies
  • Answers 2 answers
  • Subscribers 397 subscribers
  • Views 2807 views
  • Users 0 members are here
  • meters
  • help
  • hour
  • arduino
Related

Need to learn how to code hour meter.

Former Member
Former Member over 11 years ago

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("<!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("<br />");
            client.print(" Hours:  ");
            //client.print(runtime)
            client.println("<br />");     
          }
        
         
          client.print("Pump is ");
          client.print(state);
          client.print("<br><br>");
         
          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();
  }

 

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

Thanks.

  • Sign in to reply
  • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 11 years ago

    What do you get on the web page, provide a screen shot

     

    as for measuring the hours, when you turn on the motor you capture the current mills() to a variable, when you turn of the motor you subtract start mills from end mills and then add the difference to a run time counter (A "Double Runtime = 0;" declaration should do the trick), divide the difference by 1000 in order to simply track seconds if you want a bigger count.

     

    the extra thing you can do after that is to write the value to eeprom in order to survive a reboot of the PI, your start up code would read the eeprom value back into memory in the startup function

     

    http://arduino.cc/en/Tutorial/EEPROMWrite

     

    this is a wrapper that makes using the eeprom easier http://playground.arduino.cc/Code/EEPROMWriteAnything

     

    peter

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • neilk
    0 neilk over 11 years ago

    As Peter says, what does your web page actually look like? Can you provide a screen shot?

     

    Neil

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to neilk

    Thank you guy for your replies,

    I am going to tinker with the mills() as a variable this week and see what i can come up with.

    Here are two screenshots of the web page one is of the pump off and and one of it on.

    imageimage

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago

    Are you looking to run a cumulative counter, or just keep track of the time that the pump runs in one "session"?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to Former Member

    jbix,

    I am looking to run a cumulative hour meter.

    I have modified my code below. but I am still having problems. all that I am getting is 0 for the hour reading.

    Not sure what I am doing wrong.

     

     

     

     

    [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;
        //Declarations for the hour meter
        int startTime = millis();
        int stopTime = millis();
        int runTime =((stopTime) - (startTime))/1000;
        int onTime =+ runTime;

          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);
        //Sets the STATUSpin as an input
        //STATUSpin reads a signal to tell whether the pump is on or off


          //reset the pump state
          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";
              }
             
              if (digitalRead (LEDpin) == HIGH){
                startTime = millis();
              }
             
        if (digitalRead (LEDpin) == LOW){
                stopTime = millis();  
       
        }
            
            
            
            
            
            
            
              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("<br />");
                client.print(" Hours:  ");
               
                client.print(onTime);
                client.println("<br />");     
              }
            
             
              client.print("Pump is ");
              client.print(state);
              client.print("<br><br>");
             
              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();
      }
        }

    [/code]

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to Former Member

    I am newbie here but is the problem that you have put this line  " int onTime =+ runTime;" in the declaration section.  It is only ever read once by the program.  Should it not go into the loop()? 

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to Former Member

    I am still a newbie too,

    my theory was to declare "int onTime =+ runTime ;" in the begin so I could use "client.print(onTime);" when I want to out put the run hours on to the webpage.


    I tried " client.print(onTime =+ runTime);" in the loop but I still get a 0 reading .

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to Former Member

    Scott,

     

    When you declare onTime in the beginning of your code, you are setting it to zero:

     

    //Declarations for the hour meter

        int startTime = millis();  <-- setting start time to current millis()

        int stopTime = millis();  <-- setting stop time (probably only a few microseconds later, depending on clock speed)

        int runTime =((stopTime) - (startTime))/1000;  <-- setting runTime to pretty much zero

        int onTime =+ runTime;  <-- declaring onTime and incrementing it by pretty much zero (also, I believe the increment should be += as opposed to =+

     

    After which, you do not do anything with onTime, other then printing it to the client, so it remains at zero.  You would need to do the math for calculating the current run time somewhere in the loop, probably after turning the pump off, or at a point in the code, you could get the millis() and subtract start time from that and set runTime to equal that, then increment the onTime after shutting the pump off.  In order for this to be a true cumulative timer, however, you will need to store this is some sort of non-volatile memory, otherwise every time you re-connect to the Arduino, or every time it loses power, you will re-set the variables.  I would also suggest getting rid of:


    if (digitalRead (LEDpin) == HIGH){ startTime = millis();  }

    if (digitalRead (LEDpin) == LOW){  stopTime = millis();  }

     

    And moving the startTime = millis() statement to the same if statement where you turn the pump state to on, and the same with the stopTime, except moving it to where you turn it off:

     

    if (readString.substring(LED,LED+5) == "LED=T") {
      digitalWrite(LEDpin,HIGH);
    state = "ON";

    startTime = millis();
    }
    else if (readString.substring(LED,LED+5) == "LED=F") {
    digitalWrite(LEDpin,LOW);
    state = "OFF";

    stopTime = millis()
    }


    This is just to help get moving forward.  You may want to hard-code values into some of your variables for debugging, just to see if you can get a value to display for time.  One important question though, do you plan on ONLY running the pump while connected to an Ethernet client?


    Hope this helps

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 11 years ago

    there is one thing your missing (There may be others)

     

    every time through your loop our setting start time to millis() if the output is on so of course the instant it goes off your only counting the time of the loop, not the time it has been running

     

    pseudo logic following is what you need to implement

     

    boolean pumpRunning = false

     

    if pump is on and pumpRunning = false

         {

        pumpRunning = true

        startTime = millis()

         }

    else if pump is off and pumpRunning = true

         {

         pumpRunning=false

         endTime = millis()

         runTime = endTime - startTime / 1000

         onTime += runTime // += or =+ in this case makes no difference as your not using the variable within the same statement

         }

     

    Hope this is understood

     

    enjoy

     

    Peter

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to Robert Peter Oakes

    this is what I came up with:

     

    [code]

    if (digitalRead (LEDpin) == LOW ){

                startTime = millis();

              }

             

              else if (digitalRead (LEDpin) == HIGH){

                stopTime = millis();  

             runTime =((stopTime) - (startTime))/1000;

             onTime =+ runTime;

              }

    [/code]

     

    This has seemed to make it work, "kind of"

    when it turns on it starts counting and when it is off it stops. that part is perfect, how ever it will only count up to 30 and then count backwards to -30 and then back to 30.

    I am really confused on this one lol

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube