How to trigger Engine hour meter using EEPROM/ arduino UNO

I have made a program for to record my vehicle engine run hour.Here is the code

#include <EEPROM.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // 

double startTime = 0;  
double stopTime = 0;  
double runTime = 0;  
double totTime ;
int EngineLed = 13; 
int EngineCommand = 8;   
boolean EngineLastState = false; 
boolean EngineRunning = false; 

int addr_secnd=0;
int addr_minute=1;
int addr_hrs1=2;
int addr_hrs2=3;
int addr_hrs3=4;
int addr_hrs4=5;
int addr_hrs5=6;
int addr_hrs6=7;

int secnd = 0;
int minute = 0;
int hrs1 = 0;
int hrs2 = 0;
int hrs3 = 0;
int hrs4 = 0;
int hrs5 = 0;
int hrs6 = 0;

int value_secnd;
int value_minute;
int value_hrs1;
int value_hrs2;
int value_hrs3;
int value_hrs4;
int value_hrs5;
int value_hrs6;


void setup() {
  lcd.begin(16,2);
  lcd.backlight();
  Serial.begin(9600);
  value_secnd = EEPROM.read(addr_secnd);
  value_minute = EEPROM.read(addr_minute);
  value_hrs1 = EEPROM.read(addr_hrs1);
  value_hrs2 = EEPROM.read(addr_hrs2);
  value_hrs3 = EEPROM.read(addr_hrs3);
  value_hrs4 = EEPROM.read(addr_hrs4);
  value_hrs5 = EEPROM.read(addr_hrs5);
  value_hrs6 = EEPROM.read(addr_hrs6);
 Serial.println("================================");
 Serial.print("secnd:");
 Serial.println(value_secnd);
 Serial.print("minute:");
 Serial.println(value_minute);
 Serial.print("hrs1:");
 Serial.println(value_hrs1); 
 Serial.print("hrs2:");
 Serial.println(value_hrs2);
 Serial.print("hrs3:");
 Serial.println(value_hrs3);
 Serial.print("hrs4:");
 Serial.println(value_hrs4);
 Serial.print("hrs5:");
 Serial.println(value_hrs5);
 Serial.print("hrs6:");
 Serial.println(value_hrs6);
 Serial.println("================================");
 pinMode(EngineLed, OUTPUT);  
 pinMode(EngineCommand, INPUT);

}

void loop() {
  value_secnd = value_secnd+1;
  EEPROM.update(addr_secnd,value_secnd);

if(value_secnd==60){
  secnd=0;
  EEPROM.update(addr_secnd,secnd);
  value_minute = value_minute+1;
  EEPROM.update(addr_minute,value_minute);
}
value_secnd = EEPROM.read(addr_secnd);
Serial.print("secnd:");
 Serial.println(value_secnd);
Serial.print("\t");

if(value_minute==60){
  minute = 0;
  EEPROM.update(addr_minute,minute);
  value_hrs1 = value_hrs1 + 1;
  EEPROM.update(addr_hrs1,value_hrs1);
}
value_minute = EEPROM.read(addr_minute);
Serial.print("minute:");
 Serial.println(value_minute);
Serial.print("\t");

if(value_hrs1==10){
  hrs1 = 0;
  EEPROM.update(addr_hrs1, hrs1);
  value_hrs2 = value_hrs2 + 1;
  EEPROM.update(addr_hrs2,value_hrs2);
}
value_hrs1 = EEPROM.read(addr_hrs1);
Serial.print("hrs (1):");
 Serial.println(value_hrs1);
Serial.print("\t");

if(value_hrs2==10){
  hrs2 = 0;
  EEPROM.update(addr_hrs2, hrs2);
  value_hrs3 = value_hrs3 + 1;
  EEPROM.update(addr_hrs3,value_hrs3);
}
value_hrs2 = EEPROM.read(addr_hrs2);
Serial.print("hrs (2):");
 Serial.println(value_hrs2);
Serial.print("\t");

if(value_hrs3==10){
  hrs3 = 0;
  EEPROM.update(addr_hrs3, hrs3);
  value_hrs4 = value_hrs4 + 1;
  EEPROM.update(addr_hrs4,value_hrs4);
}
value_hrs3 = EEPROM.read(addr_hrs3);
Serial.print("hrs (3):");
 Serial.println(value_hrs3);
Serial.print("\t");

if(value_hrs4==10){
  hrs4 = 0;
  EEPROM.update(addr_hrs4, hrs4);
  value_hrs5 = value_hrs5 + 1;
  EEPROM.update(addr_hrs5,value_hrs5);
}
value_hrs4 = EEPROM.read(addr_hrs4);
Serial.print("hrs (4):");
 Serial.println(value_hrs4);
Serial.print("\t");

if(value_hrs5==10){
  hrs5 = 0;
  EEPROM.update(addr_hrs5, hrs5);
  value_hrs6 = value_hrs6 + 1;
  EEPROM.update(addr_hrs6,value_hrs6);
}
value_hrs5 = EEPROM.read(addr_hrs5);
Serial.print("hrs (5):");
 Serial.println(value_hrs5);
Serial.print("\t");

if(value_hrs6==10){
  hrs6 = 0;
  EEPROM.update(addr_hrs6, hrs6);
  
}
value_hrs6 = EEPROM.read(addr_hrs6);
Serial.print("hrs(6):");
 Serial.println(value_hrs6);
Serial.print("\t");

Serial.print("HM:");
Serial.print(value_hrs6);
Serial.print(value_hrs5);
Serial.print(value_hrs4);
Serial.print(value_hrs3);
Serial.print(value_hrs2);
Serial.print(value_hrs1);
Serial.print(",");
Serial.print(value_minute);
Serial.print(":");
Serial.print(value_secnd);

lcd.clear();
lcd.setCursor(0,0);
lcd.print(value_hrs6);
lcd.print(value_hrs5);
lcd.print(value_hrs4);
lcd.print(value_hrs3);
lcd.print(value_hrs2);
lcd.print(value_hrs1);
//Serial.print(",");
lcd.setCursor(0,1);
lcd.print(value_minute);
lcd.print(":");
lcd.print(value_secnd);
digitalWrite(EngineLed,HIGH);
delay(1000);
digitalWrite(EngineLed,LOW);
}

its working fine.

But problem is that when i power on the arduino meter will start to record the hour in spite of engine start.So i want to trigger the hour meter only when engine start, I will take signals from the engine once engine start and this command will assigned on Arduino pin “8”.(Enginecommand)

Please help me for find out solution.expecting favorable and valuable reply.

Start here: IDE/file/examples/digital/state change detection.

You do realize that the EEPROM has a finite write endurance count of 100,000? You can not write to it forever. If you update the seconds every second, you reach 100K in just over 24 hours.

dougp:
Start here: IDE/file/examples/digital/state change detection.

sir
i using toggle switch for giving engine command to arduino pin 8.here is my code

#include <EEPROM.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // 

/*double startTime = 0;  
double stopTime = 0;  
double runTime = 0;  
double totTime ;*/
int EngineLed = 13; 
int EngineCommand = 8;   
//boolean EngineLastState = false; 
//boolean EngineRunning = false; 

int addr_secnd=0;
int addr_minute=1;
int addr_hrs1=2;
int addr_hrs2=3;
int addr_hrs3=4;
int addr_hrs4=5;
int addr_hrs5=6;
int addr_hrs6=7;

int secnd = 0;
int minute = 0;
int hrs1 = 0;
int hrs2 = 0;
int hrs3 = 0;
int hrs4 = 0;
int hrs5 = 0;
int hrs6 = 0;

int value_secnd;
int value_minute;
int value_hrs1;
int value_hrs2;
int value_hrs3;
int value_hrs4;
int value_hrs5;
int value_hrs6;


void setup() {
  lcd.begin(16,2);
  lcd.backlight();
  Serial.begin(9600);
  value_secnd = EEPROM.read(addr_secnd);
  value_minute = EEPROM.read(addr_minute);
  value_hrs1 = EEPROM.read(addr_hrs1);
  value_hrs2 = EEPROM.read(addr_hrs2);
  value_hrs3 = EEPROM.read(addr_hrs3);
  value_hrs4 = EEPROM.read(addr_hrs4);
  value_hrs5 = EEPROM.read(addr_hrs5);
  value_hrs6 = EEPROM.read(addr_hrs6);
 Serial.println("================================");
 Serial.print("secnd:");
 Serial.println(value_secnd);
 Serial.print("minute:");
 Serial.println(value_minute);
 Serial.print("hrs1:");
 Serial.println(value_hrs1); 
 Serial.print("hrs2:");
 Serial.println(value_hrs2);
 Serial.print("hrs3:");
 Serial.println(value_hrs3);
 Serial.print("hrs4:");
 Serial.println(value_hrs4);
 Serial.print("hrs5:");
 Serial.println(value_hrs5);
 Serial.print("hrs6:");
 Serial.println(value_hrs6);
 Serial.println("================================");
 pinMode(EngineLed, OUTPUT);  
 pinMode(EngineCommand, INPUT_PULLUP);

}

void loop() {
 if ( EngineCommand==LOW)//used a toggle switch for engine command,  switch in low means engine start meter will record the time 
 {
  void Meter();
 }
 else {
  Serial.print ("engine stop");
 loop();
 }}
  

 void Meter(){
  value_secnd = value_secnd+1;
  EEPROM.update(addr_secnd,value_secnd);

if(value_secnd==60){
  secnd=0;
  EEPROM.update(addr_secnd,secnd);
  value_minute = value_minute+1;
  EEPROM.update(addr_minute,value_minute);
}
value_secnd = EEPROM.read(addr_secnd);
Serial.print("\t");
Serial.print("secnd:");
 Serial.println(value_secnd);
Serial.print("\t");

if(value_minute==60){
  minute = 0;
  EEPROM.update(addr_minute,minute);
  value_hrs1 = value_hrs1 + 1;
  EEPROM.update(addr_hrs1,value_hrs1);
}
value_minute = EEPROM.read(addr_minute);
Serial.print("minute:");
 Serial.println(value_minute);
Serial.print("\t");
if ( EngineCommand==LOW){
if(value_hrs1==10){
  hrs1 = 0;
  EEPROM.update(addr_hrs1, hrs1);
  value_hrs2 = value_hrs2 + 1;
  EEPROM.update(addr_hrs2,value_hrs2);
}
value_hrs1 = EEPROM.read(addr_hrs1);
Serial.print("hrs (1):");
 Serial.println(value_hrs1);
Serial.print("\t");

if(value_hrs2==10){
  hrs2 = 0;
  EEPROM.update(addr_hrs2, hrs2);
  value_hrs3 = value_hrs3 + 1;
  EEPROM.update(addr_hrs3,value_hrs3);
}
value_hrs2 = EEPROM.read(addr_hrs2);
Serial.print("hrs (2):");
 Serial.println(value_hrs2);
Serial.print("\t");
}

if(value_hrs3==10){
  hrs3 = 0;
  EEPROM.update(addr_hrs3, hrs3);
  value_hrs4 = value_hrs4 + 1;
  EEPROM.update(addr_hrs4,value_hrs4);
}
value_hrs3 = EEPROM.read(addr_hrs3);
Serial.print("hrs (3):");
 Serial.println(value_hrs3);
Serial.print("\t");

if(value_hrs4==10){
  hrs4 = 0;
  EEPROM.update(addr_hrs4, hrs4);
  value_hrs5 = value_hrs5 + 1;
  EEPROM.update(addr_hrs5,value_hrs5);
}
value_hrs4 = EEPROM.read(addr_hrs4);
Serial.print("hrs (4):");
 Serial.println(value_hrs4);
Serial.print("\t");

if(value_hrs5==10){
  hrs5 = 0;
  EEPROM.update(addr_hrs5, hrs5);
  value_hrs6 = value_hrs6 + 1;
  EEPROM.update(addr_hrs6,value_hrs6);
}
value_hrs5 = EEPROM.read(addr_hrs5);
Serial.print("hrs (5):");
 Serial.println(value_hrs5);
Serial.print("\t");
if ( EngineCommand==LOW){
if(value_hrs6==10){
  hrs6 = 0;
  EEPROM.update(addr_hrs6, hrs6);
  
}
value_hrs6 = EEPROM.read(addr_hrs6);
Serial.print("hrs(6):");
 Serial.println(value_hrs6);
Serial.print("\t");}

Serial.print("HM:");
Serial.print(value_hrs6);
Serial.print(value_hrs5);
Serial.print(value_hrs4);
Serial.print(value_hrs3);
Serial.print(value_hrs2);
Serial.print(value_hrs1);
Serial.print(",");
Serial.print(value_minute);
Serial.print(":");
Serial.print(value_secnd);

lcd.clear();
lcd.setCursor(0,0);
lcd.print(value_hrs6);
lcd.print(value_hrs5);
lcd.print(value_hrs4);
lcd.print(value_hrs3);
lcd.print(value_hrs2);
lcd.print(value_hrs1);
//Serial.print(",");
lcd.setCursor(0,1);
lcd.print(value_minute);
lcd.print(":");
lcd.print(value_secnd);
digitalWrite(EngineLed,HIGH);
delay(1000);
digitalWrite(EngineLed,LOW);

    }

but its not working even switch state change.please advice me what are the corrections i have to made in this program.

sreekanthmp:
what are the corrections i have to made in this program..

Use the method of switch debouncing along with state change detection to tell when the switch _**goes **_low. This is not the same thing as the switch **is ** low.

This will insure that Meter() is only called once when the switch is used.

You have to read the value of the pin, not use the pin number by itself. You also call a function by just using the function name, not the type

if ( digitalRead(EngineCommand) == LOW ) {
  Meter();
} else {
  Serial.println( "engine stop" );
}

and you don't call loop() again. It will get called again automatically when it finishes.
Note that you will get your engine stop message every time through the loop when the engine is off.

blh64:
You have to read the value of the pin, not use the pin number by itself. You also call a function by just using the function name, not the type

if ( digitalRead(EngineCommand) == LOW ) {

Meter();
} else {
 Serial.println( “engine stop” );
}



and you don't call loop() again. It will get called again automatically when it finishes.
Note that you will get your engine stop message every time through the loop when the engine is off.

Thank you very much and now its working.

i have add some more functios in my hour meter project that i connected sim800l gsm module.when ever i send the sms to gsm module the module will reply the current hour meter reading.Here is the code:_

#include <stdlib.h >
#include <SoftwareSerial.h>
#include <EEPROM.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // 
SoftwareSerial GPRS(10, 11);
String textMessage;
String val;
int EngineLed = 13; 
const uint8_t ledPin1        = 7;
const uint8_t EngineCommand = 8;   


char outstr[15];
int addr_secnd=0;
int addr_minute=1;
int addr_hrs1=2;
int addr_hrs2=3;
int addr_hrs3=4;
int addr_hrs4=5;
int addr_hrs5=6;
int addr_hrs6=7;

int secnd = 0;
int minute = 0;
int hrs1 = 0;
int hrs2 = 0;
int hrs3 = 0;
int hrs4 = 0;
int hrs5 = 0;
int hrs6 = 0;

int value_secnd;
int value_minute;
int value_hrs1;
int value_hrs2;
int value_hrs3;
int value_hrs4;
int value_hrs5;
int value_hrs6;


void setup() {
  pinMode(EngineLed, OUTPUT);  
  pinMode(EngineCommand, INPUT_PULLUP);
  pinMode ( ledPin1, OUTPUT );
  GPRS.begin(9600);
  lcd.begin(16,2);
  lcd.backlight();
  Serial.begin(9600);
  value_secnd = EEPROM.read(addr_secnd);
  value_minute = EEPROM.read(addr_minute);
  value_hrs1 = EEPROM.read(addr_hrs1);
  value_hrs2 = EEPROM.read(addr_hrs2);
  value_hrs3 = EEPROM.read(addr_hrs3);
  value_hrs4 = EEPROM.read(addr_hrs4);
  value_hrs5 = EEPROM.read(addr_hrs5);
  value_hrs6 = EEPROM.read(addr_hrs6);
 Serial.println("================================");
 Serial.print("secnd:");
 Serial.println(value_secnd);
 Serial.print("minute:");
 Serial.println(value_minute);
 Serial.print("hrs1:");
 Serial.println(value_hrs1); 
 Serial.print("hrs2:");
 Serial.println(value_hrs2);
 Serial.print("hrs3:");
 Serial.println(value_hrs3);
 Serial.print("hrs4:");
 Serial.println(value_hrs4);
 Serial.print("hrs5:");
 Serial.println(value_hrs5);
 Serial.print("hrs6:");
 Serial.println(value_hrs6);
 Serial.println("================================");
 GPRS.print("AT+CNMI=2,2,0,0,0\r");
  delay(100);
 }

void loop() {
 if ( digitalRead(EngineCommand) == LOW)//used a toggle switch for engine command,  switch in low means engine start meter will record the time 
 {
  digitalWrite(ledPin1,HIGH);
  Meter();
 }
else {
  //Serial.println ("engine stop");
  digitalWrite(ledPin1,LOW);
 }
if (GPRS.available() > 0) {
    delay(1000);
    textMessage = GPRS.readString();
    Serial.print(textMessage);
    if(textMessage.indexOf("hmr")>=0){
    //String message = (outstr);
    Serial.println(outstr);  
     GPRS.println("AT+CMGF=1");
      delay(1000);
      GPRS.println("AT+CMGS=\"+917987553768\""); 
      delay(500);
      GPRS.println(outstr);
     delay(1000);
      GPRS.println((char)26);
      delay(500);
      Serial.println("HMR REQUEST");
     delay(500);
    }
    
    delay(100);
  }
}
  
 void Meter(){
  value_secnd = value_secnd+1;
  EEPROM.update(addr_secnd,value_secnd);

if(value_secnd==60){
  secnd=0;
  EEPROM.update(addr_secnd,secnd);
  value_minute = value_minute+1;
  EEPROM.update(addr_minute,value_minute);
}
value_secnd = EEPROM.read(addr_secnd);
Serial.print("\t");
Serial.print("secnd:");
 Serial.println(value_secnd);
Serial.print("\t");

if(value_minute==60){
  minute = 0;
  EEPROM.update(addr_minute,minute);
  value_hrs1 = value_hrs1 + 1;
  EEPROM.update(addr_hrs1,value_hrs1);
}
value_minute = EEPROM.read(addr_minute);
Serial.print("minute:");
 Serial.println(value_minute);
Serial.print("\t");

if(value_hrs1==10){
  hrs1 = 0;
  EEPROM.update(addr_hrs1, hrs1);
  value_hrs2 = value_hrs2 + 1;
  EEPROM.update(addr_hrs2,value_hrs2);
}
value_hrs1 = EEPROM.read(addr_hrs1);
Serial.print("hrs (1):");
 Serial.println(value_hrs1);
Serial.print("\t");

if(value_hrs2==10){
  hrs2 = 0;
  EEPROM.update(addr_hrs2, hrs2);
  value_hrs3 = value_hrs3 + 1;
  EEPROM.update(addr_hrs3,value_hrs3);
}
value_hrs2 = EEPROM.read(addr_hrs2);
Serial.print("hrs (2):");
 Serial.println(value_hrs2);
Serial.print("\t");


if(value_hrs3==10){
  hrs3 = 0;
  EEPROM.update(addr_hrs3, hrs3);
  value_hrs4 = value_hrs4 + 1;
  EEPROM.update(addr_hrs4,value_hrs4);
}
value_hrs3 = EEPROM.read(addr_hrs3);
Serial.print("hrs (3):");
 Serial.println(value_hrs3);
Serial.print("\t");

if(value_hrs4==10){
  hrs4 = 0;
  EEPROM.update(addr_hrs4, hrs4);
  value_hrs5 = value_hrs5 + 1;
  EEPROM.update(addr_hrs5,value_hrs5);
}
value_hrs4 = EEPROM.read(addr_hrs4);
Serial.print("hrs (4):");
 Serial.println(value_hrs4);
Serial.print("\t");

if(value_hrs5==10){
  hrs5 = 0;
  EEPROM.update(addr_hrs5, hrs5);
  value_hrs6 = value_hrs6 + 1;
  EEPROM.update(addr_hrs6,value_hrs6);
}
value_hrs5 = EEPROM.read(addr_hrs5);
Serial.print("hrs (5):");
 Serial.println(value_hrs5);
Serial.print("\t");

if(value_hrs6==10){
  hrs6 = 0;
  EEPROM.update(addr_hrs6, hrs6);
  
}
value_hrs6 = EEPROM.read(addr_hrs6);
Serial.print("hrs(6):");
Serial.println(value_hrs6);
Serial.print("\t");

int x1=EEPROM.read(addr_hrs6);
int x2=EEPROM.read(addr_hrs5);
int x3=EEPROM.read(addr_hrs4);
int x4=EEPROM.read(addr_hrs3);
int x5=EEPROM.read(addr_hrs2);
int x6=EEPROM.read(addr_hrs1);
int x7=EEPROM.read(addr_minute);

String val = String(x1)+String(x2)+String(x3)+String(x4)+String(x5)+String(x6)+String(x7);

float value1 = (val.toFloat())/100;
Serial.println(value1);
char outstr[15];
dtostrf(value1,5, 2, outstr);
Serial.print(outstr);
Serial.print("\t");
/*if (GPRS.available() > 0) {
    delay(1000);
    textMessage = GPRS.readString();
    Serial.print(textMessage);
    if(textMessage.indexOf("hmr")>=0){
    //String message = (outstr);
    Serial.println(outstr);  
     GPRS.println("AT+CMGF=1");
      delay(1000);
      GPRS.println("AT+CMGS=\"+917987553768\""); 
      delay(500);
      GPRS.println(outstr);
     delay(1000);
      GPRS.println((char)26);
      delay(500);
      Serial.println("HMR REQUEST");
     delay(500);
    }
    
    delay(100);
  }*/
Serial.print("HM:");
Serial.print(value_hrs6);
Serial.print(value_hrs5);
Serial.print(value_hrs4);
Serial.print(value_hrs3);
Serial.print(value_hrs2);
Serial.print(value_hrs1);
Serial.print(",");
Serial.print(value_minute);
Serial.print(":");
Serial.print(value_secnd);

lcd.clear();
lcd.setCursor(0,0);
lcd.print(value_hrs6);
lcd.print(value_hrs5);
lcd.print(value_hrs4);
lcd.print(value_hrs3);
lcd.print(value_hrs2);
lcd.print(value_hrs1);
//Serial.print(",");
lcd.setCursor(0,1);
lcd.print(value_minute);
lcd.print(":");
lcd.print(value_secnd);
digitalWrite(EngineLed,HIGH);
delay(1000);
digitalWrite(EngineLed,LOW);

    }

hour meter function working properly.but gsm module send the sms only when the serial monitor open.
that means if i send the sms to gsm module the module will reply current reading only when serial monitor open other wise its didnot respond.

i need system will work external power supply also.please help me on this matter.

Could you please help me for solving this problem...My project is totally hampered!!!!