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 397 subscribers
  • Views 2829 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

    OK, sorry I had assumed a bit higher level of coding expertise

     

    you still where not implementing the additional chacks I was sugesting and or was not understanding the pseudo code I provided, so in good faith I wrote the basic timer for you

     

    it uses the LED on pin 13 to pretend to be the pump, uses input 8 to control the pump (This can be replaced with your ethernet code) and will print out to the serial port every time the pump starts or stops.

    This is tested and working. BTW, previously someone was saying that =+ was more correct than +=.

    in the line 51 " totTime += runTime;", change it to totTime =+ runTime; and see what happens ?. Let me know your finding

     

    here is the code

    // Pin 13 has an LED connected on most Arduino boards.
    double startTime = 0;
    double stopTime = 0;
    double runTime = 0;
    double totTime = 0;
    int pumpLed = 13; // this could be a relay to turn on the pump
    
    // for this example we will use a high on the pumpcommand pin to mean run the pump
    int pumpCommand = 8; // input pin to indicate pump running
    boolean pumpLastState = false; // if it was in the main loop it would be reset every time thorough the code
    boolean pumpRunning = false; // if it was in the main loop it would be reset every time thorough the code
    // the setup routine runs once when you press reset:
    
    void setup()
    {               
      Serial.begin(9600); // initialize serial port to 9600 baud
      // initialize the digital pin as an output.
      pinMode(pumpLed, OUTPUT);
      pinMode(pumpCommand, INPUT);
      digitalWrite(pumpCommand, HIGH); // turn on pull up resistor
      totTime = 0 ; // this is where you could read from an EEPROM to continue after a restart
      Serial.println("Pump Control at Your Command on pin 8");
      Serial.println("Gnd to Stop, High/Open to run");
      Serial.println("no debounce on input so you might get extra triggers");
    }
    
    // the loop routine runs over and over again forever:
    void loop()
    {
      pumpLastState = digitalRead(pumpCommand); // read if the pump should be on, this could also come from a command
      // MUST use double == in order to do a comparison vs an assignment
      if (pumpRunning == false && pumpLastState==HIGH)
      {
        // turn on the pump / we will only do this once as the pumpRunning will prevent entering here again
        // until it is reset by the command telling us to stop the pump
         digitalWrite(pumpLed, HIGH);
         pumpRunning = true;
         runTime = 0; // reset as were just turning on again and its already added to accumilator
         startTime = millis();
         Serial.print("Pump on, Starting Timer. ");
       }
       else if (pumpRunning == true && pumpLastState ==LOW) // sense LOW prevents entering this more than once
       {
         //Turn off the pump but only do the sequence once to prevent messing up the counters
         digitalWrite(pumpLed, LOW);
         pumpRunning = false;
         stopTime = millis();
         runTime +=(stopTime - startTime)/1000; // last run time in seconds
         totTime += runTime; // addd to accumilator
         Serial.print("Pump off after ");Serial.print(runTime);Serial.print(" seconds. ");
         Serial.print("Total time = ");Serial.println(totTime);
       }
    }

     

     

    and this is the output (I was just using a length of wire to gnd the pin to it was glitchy and caused the extra fast runs. This is not an issue with a debounced switch either through code or hardware

     

    Pump Control at Your Command on pin 8

    Gnd to Stop, High/Open to run

    no debounce on input so you might get extra triggers

    Pump on, Starting Timer. Pump off after 6.73 seconds. Total time = 6.73

    Pump on, Starting Timer. Pump off after 0.00 seconds. Total time = 6.73

    Pump on, Starting Timer. Pump off after 0.06 seconds. Total time = 6.79

    Pump on, Starting Timer. Pump off after 0.01 seconds. Total time = 6.79

    Pump on, Starting Timer. Pump off after 0.19 seconds. Total time = 6.98

    Pump on, Starting Timer. Pump off after 0.01 seconds. Total time = 6.99

    Pump on, Starting Timer. Pump off after 0.08 seconds. Total time = 7.07

    Pump on, Starting Timer. Pump off after 0.02 seconds. Total time = 7.09

    Pump on, Starting Timer. Pump off after 0.07 seconds. Total time = 7.15

    Pump on, Starting Timer. Pump off after 6.09 seconds. Total time = 13.24

    Pump on, Starting Timer. Pump off after 0.03 seconds. Total time = 13.27

    Pump on, Starting Timer. Pump off after 0.00 seconds. Total time = 13.27

    Pump on, Starting Timer. Pump off after 1.05 seconds. Total time = 14.32

    Pump on, Starting Timer. Pump off after 0.06 seconds. Total time = 14.38

    Pump on, Starting Timer. Pump off after 0.02 seconds. Total time = 14.40

    Pump on, Starting Timer. Pump off after 0.00 seconds. Total time = 14.40

    Pump on, Starting Timer. Pump off after 0.25 seconds. Total time = 14.65

    Pump on, Starting Timer. Pump off after 0.31 seconds. Total time = 14.96

    Pump on, Starting Timer. Pump off after 0.01 seconds. Total time = 14.97

    Pump on, Starting Timer. Pump off after 0.17 seconds. Total time = 15.15

    Pump on, Starting Timer. Pump off after 0.00 seconds. Total time = 15.15

    Pump on, Starting Timer. Pump off after 0.61 seconds. Total time = 15.76

    Pump on, Starting Timer. Pump off after 0.01 seconds. Total time = 15.77

    Pump on, Starting Timer. Pump off after 0.00 seconds. Total time = 15.77

    Pump on, Starting Timer. Pump off after 0.26 seconds. Total time = 16.03

    Pump on, Starting Timer. Pump off after 0.00 seconds. Total time = 16.03

    Pump on, Starting Timer. Pump off after 0.29 seconds. Total time = 16.33

    Pump on, Starting Timer. Pump off after 0.32 seconds. Total time = 16.65

    Pump on, Starting Timer. Pump off after 0.01 seconds. Total time = 16.66

    Pump on, Starting Timer. Pump off after 0.00 seconds. Total time = 16.66

    Pump on, Starting Timer. Pump off after 0.28 seconds. Total time = 16.94

    Pump on, Starting Timer. Pump off after 0.34 seconds. Total time = 17.28

    Pump on, Starting Timer. Pump off after 0.29 seconds. Total time = 17.56

    Pump on, Starting Timer. Pump off after 0.00 seconds. Total time = 17.56

    Pump on, Starting Timer. Pump off after 0.22 seconds. Total time = 17.78

    Pump on, Starting Timer. Pump off after 9.41 seconds. Total time = 27.19

    Pump on, Starting Timer. Pump off after 0.00 seconds. Total time = 27.20

    Pump on, Starting Timer. Pump off after 0.00 seconds. Total time = 27.20

    Pump on, Starting Timer. Pump off after 5.42 seconds. Total time = 32.62

    Pump on, Starting Timer. Pump off after 0.01 seconds. Total time = 32.63

    Pump on, Starting Timer. Pump off after 8.77 seconds. Total time = 41.40

    Pump on, Starting Timer. Pump off after 0.01 seconds. Total time = 41.41

    Pump on, Starting Timer. Pump off after 0.00 seconds. Total time = 41.41

    Pump on, Starting Timer. Pump off after 13.43 seconds. Total time = 54.84

    Pump on, Starting Timer. Pump off after 23.27 seconds. Total time = 78.10

    Pump on, Starting Timer. Pump off after 0.01 seconds. Total time = 78.11

    Pump on, Starting Timer. Pump off after 8.31 seconds. Total time = 86.42

    Pump on, Starting Timer. Pump off after 0.00 seconds. Total time = 86.43

     

    Now its up to you to learn what I have shown you and add in your networking and other code

     

    Have fun

     

    If this is the right answer then click Right Answer please,

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

    OK, sorry I had assumed a bit higher level of coding expertise

     

    you still where not implementing the additional chacks I was sugesting and or was not understanding the pseudo code I provided, so in good faith I wrote the basic timer for you

     

    it uses the LED on pin 13 to pretend to be the pump, uses input 8 to control the pump (This can be replaced with your ethernet code) and will print out to the serial port every time the pump starts or stops.

    This is tested and working. BTW, previously someone was saying that =+ was more correct than +=.

    in the line 51 " totTime += runTime;", change it to totTime =+ runTime; and see what happens ?. Let me know your finding

     

    here is the code

    // Pin 13 has an LED connected on most Arduino boards.
    double startTime = 0;
    double stopTime = 0;
    double runTime = 0;
    double totTime = 0;
    int pumpLed = 13; // this could be a relay to turn on the pump
    
    // for this example we will use a high on the pumpcommand pin to mean run the pump
    int pumpCommand = 8; // input pin to indicate pump running
    boolean pumpLastState = false; // if it was in the main loop it would be reset every time thorough the code
    boolean pumpRunning = false; // if it was in the main loop it would be reset every time thorough the code
    // the setup routine runs once when you press reset:
    
    void setup()
    {               
      Serial.begin(9600); // initialize serial port to 9600 baud
      // initialize the digital pin as an output.
      pinMode(pumpLed, OUTPUT);
      pinMode(pumpCommand, INPUT);
      digitalWrite(pumpCommand, HIGH); // turn on pull up resistor
      totTime = 0 ; // this is where you could read from an EEPROM to continue after a restart
      Serial.println("Pump Control at Your Command on pin 8");
      Serial.println("Gnd to Stop, High/Open to run");
      Serial.println("no debounce on input so you might get extra triggers");
    }
    
    // the loop routine runs over and over again forever:
    void loop()
    {
      pumpLastState = digitalRead(pumpCommand); // read if the pump should be on, this could also come from a command
      // MUST use double == in order to do a comparison vs an assignment
      if (pumpRunning == false && pumpLastState==HIGH)
      {
        // turn on the pump / we will only do this once as the pumpRunning will prevent entering here again
        // until it is reset by the command telling us to stop the pump
         digitalWrite(pumpLed, HIGH);
         pumpRunning = true;
         runTime = 0; // reset as were just turning on again and its already added to accumilator
         startTime = millis();
         Serial.print("Pump on, Starting Timer. ");
       }
       else if (pumpRunning == true && pumpLastState ==LOW) // sense LOW prevents entering this more than once
       {
         //Turn off the pump but only do the sequence once to prevent messing up the counters
         digitalWrite(pumpLed, LOW);
         pumpRunning = false;
         stopTime = millis();
         runTime +=(stopTime - startTime)/1000; // last run time in seconds
         totTime += runTime; // addd to accumilator
         Serial.print("Pump off after ");Serial.print(runTime);Serial.print(" seconds. ");
         Serial.print("Total time = ");Serial.println(totTime);
       }
    }

     

     

    and this is the output (I was just using a length of wire to gnd the pin to it was glitchy and caused the extra fast runs. This is not an issue with a debounced switch either through code or hardware

     

    Pump Control at Your Command on pin 8

    Gnd to Stop, High/Open to run

    no debounce on input so you might get extra triggers

    Pump on, Starting Timer. Pump off after 6.73 seconds. Total time = 6.73

    Pump on, Starting Timer. Pump off after 0.00 seconds. Total time = 6.73

    Pump on, Starting Timer. Pump off after 0.06 seconds. Total time = 6.79

    Pump on, Starting Timer. Pump off after 0.01 seconds. Total time = 6.79

    Pump on, Starting Timer. Pump off after 0.19 seconds. Total time = 6.98

    Pump on, Starting Timer. Pump off after 0.01 seconds. Total time = 6.99

    Pump on, Starting Timer. Pump off after 0.08 seconds. Total time = 7.07

    Pump on, Starting Timer. Pump off after 0.02 seconds. Total time = 7.09

    Pump on, Starting Timer. Pump off after 0.07 seconds. Total time = 7.15

    Pump on, Starting Timer. Pump off after 6.09 seconds. Total time = 13.24

    Pump on, Starting Timer. Pump off after 0.03 seconds. Total time = 13.27

    Pump on, Starting Timer. Pump off after 0.00 seconds. Total time = 13.27

    Pump on, Starting Timer. Pump off after 1.05 seconds. Total time = 14.32

    Pump on, Starting Timer. Pump off after 0.06 seconds. Total time = 14.38

    Pump on, Starting Timer. Pump off after 0.02 seconds. Total time = 14.40

    Pump on, Starting Timer. Pump off after 0.00 seconds. Total time = 14.40

    Pump on, Starting Timer. Pump off after 0.25 seconds. Total time = 14.65

    Pump on, Starting Timer. Pump off after 0.31 seconds. Total time = 14.96

    Pump on, Starting Timer. Pump off after 0.01 seconds. Total time = 14.97

    Pump on, Starting Timer. Pump off after 0.17 seconds. Total time = 15.15

    Pump on, Starting Timer. Pump off after 0.00 seconds. Total time = 15.15

    Pump on, Starting Timer. Pump off after 0.61 seconds. Total time = 15.76

    Pump on, Starting Timer. Pump off after 0.01 seconds. Total time = 15.77

    Pump on, Starting Timer. Pump off after 0.00 seconds. Total time = 15.77

    Pump on, Starting Timer. Pump off after 0.26 seconds. Total time = 16.03

    Pump on, Starting Timer. Pump off after 0.00 seconds. Total time = 16.03

    Pump on, Starting Timer. Pump off after 0.29 seconds. Total time = 16.33

    Pump on, Starting Timer. Pump off after 0.32 seconds. Total time = 16.65

    Pump on, Starting Timer. Pump off after 0.01 seconds. Total time = 16.66

    Pump on, Starting Timer. Pump off after 0.00 seconds. Total time = 16.66

    Pump on, Starting Timer. Pump off after 0.28 seconds. Total time = 16.94

    Pump on, Starting Timer. Pump off after 0.34 seconds. Total time = 17.28

    Pump on, Starting Timer. Pump off after 0.29 seconds. Total time = 17.56

    Pump on, Starting Timer. Pump off after 0.00 seconds. Total time = 17.56

    Pump on, Starting Timer. Pump off after 0.22 seconds. Total time = 17.78

    Pump on, Starting Timer. Pump off after 9.41 seconds. Total time = 27.19

    Pump on, Starting Timer. Pump off after 0.00 seconds. Total time = 27.20

    Pump on, Starting Timer. Pump off after 0.00 seconds. Total time = 27.20

    Pump on, Starting Timer. Pump off after 5.42 seconds. Total time = 32.62

    Pump on, Starting Timer. Pump off after 0.01 seconds. Total time = 32.63

    Pump on, Starting Timer. Pump off after 8.77 seconds. Total time = 41.40

    Pump on, Starting Timer. Pump off after 0.01 seconds. Total time = 41.41

    Pump on, Starting Timer. Pump off after 0.00 seconds. Total time = 41.41

    Pump on, Starting Timer. Pump off after 13.43 seconds. Total time = 54.84

    Pump on, Starting Timer. Pump off after 23.27 seconds. Total time = 78.10

    Pump on, Starting Timer. Pump off after 0.01 seconds. Total time = 78.11

    Pump on, Starting Timer. Pump off after 8.31 seconds. Total time = 86.42

    Pump on, Starting Timer. Pump off after 0.00 seconds. Total time = 86.43

     

    Now its up to you to learn what I have shown you and add in your networking and other code

     

    Have fun

     

    If this is the right answer then click Right Answer please,

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

    Peter,

       I definitely have a way to go before I am proficient in coding for Arduino. Thank you so much for breaking down your code for me. That was a huge help in understanding how this worked.

     

        After implementing your code it runs great, if I change line 51. to  totTime =+ runtime It throws my count off, it looks like it starts to count up and down again.

     

    Thanks again for all your help

    • 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