Go Down

Topic: add hours and days on timecounter (Read 2123 times) previous topic - next topic


I am running this code copied from here http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1227406778/all
Thiw code just count time since reset
I need to insert button for changing hours  on counter.
Fow example if time is 3 hours and 32 min ,by pressing the button 2 times the new time will be 5 hours and 32 min.
I was tring to find command of change  DateTime.Hour and DateTime.Day   (etc DateTime.Hour++) but nothing happend
Anyone knows those commands (-- and ++) please help

Code: [Select]
#include <DateTime.h>
#include <LiquidCrystal.h>

int resetPin = 3; // pin 3 resets the time

//create object to control an LCD GMD1602K.
LiquidCrystal lcd(12, 11, 2, 7, 8, 9, 10);

void setup(){
digitalWrite(resetPin,HIGH);  // this line enables pull-up
pinMode(13,OUTPUT);          // we flash the LED each second
pinMode(resetPin, INPUT);    // a button on this pin resets the time
DateTime.sync( 0 );             // set time to zero

void loop(){
if(digitalRead(resetPin) == LOW)
     DateTime.sync( 0 );      // reset time to zero if button pressed

/* --------- Prints time in hours, minutes and seconds ----------- */
DateTime.available(); // needed to refresh the clock time
time_t timeNow = DateTime.now();
lcd.print(timeNow/60);        // total elapsed minutes
lcd.print(timeNow/3600);    //  total elapsed seconds

lcd.print(DateTime.Hour,DEC);      // prints total hours (up to 24)
lcd.print(DateTime.Minute,DEC);   // and minutes since reset
lcd.print(DateTime.Second,DEC);  // and seconds since reset
lcd.print(" ");                               // clears second digit every minute

digitalWrite(13, LOW);                  //light the LED every second
delay (1000);
digitalWrite(13, HIGH);                 //


It isn't clear what you are trying to. On every pass through loop, you call the DateTime.now() function, which obtains the current time, again. Incrementing the Hour and Minute variables, and then resetting them nanoseconds later does not seem useful.
The art of getting good answers lies in asking good questions.


Apr 06, 2011, 01:14 pm Last Edit: Apr 06, 2011, 01:16 pm by dalai Reason: 1
No I dont want to reset them
pin for increase Hour and Minute variables will be in other pins  (not in pin 3 (reset pin) )

I want to add a code like this
void loop(){
if(digitalRead(increaseHourPin) == LOW)
    hour++;      // increase hour by one (this is the command i dont know)

if(digitalRead(increaseMinPin) == LOW)
  minute ++ ;      // increase min by one (this is the command i dont know)


Code: [Select]
     hour++;      // increase hour by one (this is the command i dont know)
That command is correct, assuming you do something like this, first:

Code: [Select]
int hour = DateTime.Hour;

You can not, the way the code you posted initially is structured, increment DateTime.Hour, because DateTime.Hour is set back to the current time on every pass through loop.
The art of getting good answers lies in asking good questions.



When you are stuck at the beginning, the thing to do is to define what you want to do, with no thought as to how you would do it (whatever it is). So, what is it you want to do?
The art of getting good answers lies in asking good questions.


Apr 06, 2011, 09:09 pm Last Edit: Apr 06, 2011, 09:25 pm by dalai Reason: 1
My project is an egg hatcher.Its a project that involves  all basic electronics (sensors ,timers, moters, buttons etc) so i thote that is a good start for learning.
Until now my code ,reads temperature and huminity.Then makes a desition for opening airflow  heaters boilers etc.
The method is that 2 temperature sensors reads temp and by the method of wet bumb i get huminity.

My next step is to insert time in project. The reason is that chickens  egg  need to be at 37 C / 80 humidity  for 18 days  and then  for the last 3 days needs 36 C/ 90 humidity and no moving .
There is some problems in that idea. First I think that I want to avoid external timer.I want by a button choice ,a timer of days start up.
In that point by an internal timer I think that I solved that problem. But if power lost ,program will start from day 1.
So I want by a press of button to correct time counter (1 button for hours , one for days)

Later I will try to backup time elapsed to eeprom every 1 hour but for now I need to be able to correct time counter ....

A litle dificult for newbie ,but I needed motivation :)

Here is the code I copied/write  until now

Code: [Select]

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int pin0 = 0; // analog pin 0
int pin1 = 1; // analog pin 1
int heater = 13;
int cooler = 10;
float tempF = 0,tempC=0,wetF=0,wetC=0,etemp=0,ewet=0,eall=0,humidity=0 ; // temperature variables
float samples[8]; // variables to make a better precision (dry sensor)
float wetsamples[8]; // variables to make a better precision (wet sensor)
int i;

void setup()
  Serial.begin(9600); // start serial communication
  lcd.begin(16, 4);  // set up the LCD's number of columns and rows
  pinMode(cooler, OUTPUT);
  pinMode(heater, OUTPUT);

void loop()

for(i = 0;i<=7;i++){ // gets 8 samples of temperature

  samples[i] = ( 5.0 * analogRead(pin0) * 100.0) / 1024.0;
  wetsamples[i] = ( 5.0 * analogRead(pin1) * 100.0) / 1024.0;
  tempF = tempF + samples[i];
  wetF = wetF + wetsamples[i];


tempF = tempF/8.0; // better precision
wetF = wetF/8.0;

tempC = (tempF - 32.0)/ 1.8 ; // converts to celsius
wetC = (wetF - 32)/ 1.8 ; //converts to celsius

//follows up code to calculate humidity from 2 temp sensor and wet bumb method. Air presure preset to 1013 .
etemp=(17.67*tempC)/(tempC+243.50) ; //
etemp=exp(etemp); //log
ewet=(17.67*wetC)/(wetC+243.50) ; //
ewet=exp(ewet); //log
ewet=6.112 * ewet ;
eall= ewet - ((1013.25*(tempC-wetC)) * (0.00066*(1+(0.00115*wetC))));
humidity=(eall/etemp)*100; //humidity

// cooling and heating examples
if (tempC <= 37.6) {digitalWrite(heater, HIGH);}
else {digitalWrite(heater,LOW);}
if (tempC>= 38.2 ){digitalWrite(cooler,HIGH);  }
else {digitalWrite(cooler,LOW);}

//data on LCD or serial

Serial.print(" Celsius, ");
lcd.setCursor(0, 0);
lcd.print(" Celsius, ");

Serial.print(" wetC -> ");
lcd.setCursor(0, 1);
lcd.print(" wetC -> ");

Serial.print(" humidity -> ");
lcd.setCursor(0, 3);
lcd.print(" humidity -> ");

tempF = 0; //reset variables



Is it possible to see how datetime.h is made so I can manipulate it ?

Go Up