 # Hello World 12c code help

In this code, there is a portion that counts seconds and I was wondering if anyone could help me edit that portion to keep track of the hour and the minutes as well as seconds. Any help with this code would be greatly appreciated.

#include <Wire.h>
#include <LiquidCrystal.h>

// Connect via i2c, default address #0 (A0-A2 not jumpered)
LiquidCrystal lcd(0);

void setup() {
// set up the LCD’s number of rows and columns:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print(“hello, world!”);
}

void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(millis()/1000);

lcd.setBacklight(HIGH);
delay(500);
lcd.setBacklight(LOW);
delay(500);
}

your best bet would be to get an rtc...as millis is so unreliable in the event of a power failure....

If you want to print the "time" as hours:minutes:seconds, you will need to store the value of millis() in a variable:

``````unsigned long elapsed = millis();
``````

so that you can use the same value in all future calculations. Now, elapsed represents milliseconds, so convert that to seconds:

``````elapsed /= 1000;
``````

Then, determine how many hours that represents. There are 3600 seconds in an hour:

``````int hours = elapsed/3600;
elapsed -= hours * 3600;
``````

You can see that elapsed is now less than 1 hour, so it represents some minutes and seconds.

You should be able to figure out the rest.

Woud repeating this portion of the code work? I was thinking of doing it 3 times but changing the variables for the other two. Since 3600 represents the hour and 1000 represents seconds, what would minutes be? When I altered the code in this way, it counted up by 15 but didn't show three different portions (hour, minute, seconds)

lcd.setCursor(0, 1); // print the number of seconds since reset: lcd.print(millis()/1000);

Does what PaulS said would work. Is there some reason you chose not to?

This is the current code with the bits from the code given in one of the replies. However, I am not sure where the lcd.print part goes because that is what gets the time to appear on the actual lcd.

#include <Wire.h>
#include <LiquidCrystal.h>

// Connect via i2c, default address #0 (A0-A2 not jumpered)
LiquidCrystal lcd(0);

void setup() {
// set up the LCD’s number of rows and columns:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print(“hello, world!”);
}

void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:

unsigned long elapsed = millis();
elapsed /= 1000;
int hours = elapsed/3600;
elapsed -= hours * 3600;

lcd.setBacklight(HIGH);
delay(500);
lcd.setBacklight(LOW);
delay(500);

Can you post your complete code? Don’t just stop half-way through. And use [ code ] tags? Select the code and hit the “#” button on the toolbar above where you post.

That is the whole code, I accidentally to copy the } at the end.

This is the same code as the 2 codes given above, but the bolded parts are what I altered quite a bit. When uploaded to the arduino, the first number that shows up is a 0. It stays on zero for about a minute and then goes up to 1 and goes on with this pattern. I essentially wanted it to work like a clock with the hour, minute, and second but it just appears as a single digit. I have no idea how to fix/change it but that is the best I can do…

#include <Wire.h>
#include <LiquidCrystal.h>

// Connect via i2c, default address #0 (A0-A2 not jumpered)
LiquidCrystal lcd(0);

void setup() {
// set up the LCD’s number of rows and columns:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print(“hello, world!”);
}

void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:

unsigned long elapsed = millis();
int seconds = elapsed/1000;
elapsed /= 1000;
int hours = elapsed/3600;
elapsed -= hours * 3600;
int minutes = elapsed/60;
lcd.print(minutes);
elapsed -= minutes * 60;

lcd.setBacklight(LOW);
delay(500);
lcd.setBacklight(LOW);
delay(500);
}

[quote author=Nick Gammon link=topic=81458.msg617914#msg617914 date=1323309287] And use [ code ] tags? Select the code and hit the "#" button on the toolbar above where you post. [/quote]

You decided to ignore that request?