Hello Mem,
I am building a Hour Counter for my Air Compressor which works eventually. I need to keep a track on the running time in order to do a proper service.
I have the RTC module and it works fine with Time.h. I have developed the program upto some level which gives the elapsed time after 5 sec delay (That is 5 Sec) . but when I tried to do the same subtraction with a if condition corresponding to a button switch it gives 0 duration.
Can you please help me to over come this.
/*
* TimeRTC.pde
* example code illustrating Time library with Real Time Clock.
*
*/
#include <Time.h>
#include <Wire.h>
#include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int se;
int minu;
int hr;
const int buttonPin = 8; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
Serial.begin(9600);
lcd.begin(20, 4);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
setSyncProvider(RTC.get); // the function to get the time from the RTC
if(timeStatus()!= timeSet)
lcd.println("Unable to");
else
lcd.println("RTC has set");
}
void loop()
{
int timse1;
int timse2;
int durse;
int timmin1;
int timmin2;
int durmin;
int timhr1;
int timhr2;
int durhr;
int timsev1;
int timsev2;
int timminv1;
int timminv2;
int timhrv1;
int timhrv2;
buttonState = digitalRead(buttonPin);
//setTime(12,44,0,3,6,14);
digitalClockDisplay();
delay(1000);
lcd.clear();
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
timsev1= getse();
timminv1= getminu();
timhrv1= gethr();
lcd.print("ON");
//
// if (buttonState == LOW){
//
//
// digitalWrite(ledPin, LOW);
// timsev2 = getse();
// timminv2 = getminu();
// timhrv2 = gethr();
// lcd.print("OFF");
//
// }
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
timsev2 = getse();
timminv2 = getminu();
timhrv2 = gethr();
lcd.print("OFF");
}
lcd.print(timse1);
durse = timse2-timse1;
durmin = timmin2-timmin1;
durhr = timhr2-timhr1;
lcd.setCursor(1, 2);
lcd.print(durhr);
lcd.print(':');
lcd.print(durmin);
lcd.print(':');
lcd.print(durse);
delay(1000);
}
int getse(){
time_t t= now();
se = second(t);
return se;
}
int getminu(){
time_t t= now();
minu = minute(t);
return minu;
}
int gethr(){
time_t t= now();
hr = hour(t);
return hr;
}
void digitalClockDisplay(){
// digital clock display of the time
lcd.print(hour());
printDigits(minute());
printDigits(second());
/*Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println(); */
}
/*int duration (int ms, int mp){
//function to find the varience b/w start time n stop time
int hd;
int md;
// hd= hp-hs;
md= mp-ms;
//lcd.print(hd);
printDigits(md);
}
*/
void printDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
lcd.print(":");
if(digits < 10)
lcd.print('0');
lcd.print(digits);
}