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 & Tria 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 398 subscribers
  • Views 2816 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
Parents
  • 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
Reply
  • 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
Children
  • 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
  • Robert Peter Oakes
    0 Robert Peter Oakes over 11 years ago in reply to Former Member

    you still don't have the control logic to stop the code from repeatedly initializing startTime while the pump is running, this should only do it once

     

    you still don't have the control logic in the stopTime area so as long as the pump is stopped it will keep re-calculating the run time even though it is not been running.

     

    look at my pseudo code and you will see what i mean

     

    here it is again

    boolean pumpRunning = false  <-------- control flag

     

    if pump is on and pumpRunning = false <----- two checks here, on for the pump on and one for the control flag so we only do this once.

         {

        pumpRunning = true

        startTime = millis()

         }

    else if pump is off and pumpRunning = true <----- two checks here, on for the pump off and one for the control flag so we only calculate time once.

         {

         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

         }

     

    what your currently seeing may be a by product of the loop time between changing values and not a real problem with the code

    apply the additional control logic and it should clear up, if not then I will implement on my own device and show you

    Peter

    • 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 Robert Peter Oakes

    Peter,

    I have put in:

         [code]

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

                }

           

              boolean pumpRunning = false;

              if (pumpRunning = false)

              {

                pumpRunning = true;

                startTime = millis();

              }

           

             else if (pumpRunning = true)

               {

               pumpRunning = false;

               stopTime = millis();

               runTime =(stopTime - startTime)/1000;

               onTime =+ runTime;

                } 

    [/code]

    for some reason timer just wants to keep counting up and down from 30 to -30 when the pump is on and when it is off.

    • 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