Trying to display the time with an arduino

I am following the code from the Arduino Cookbook and here is the code that I put it in order to have the serial monitor display the time.

/*
* Time sketch
*
*/
#include <Time.h>

void setup()
{
Serial.begin(9600);
setTime(12,0,0,1,1,11); // set time to noon Jan 1 2011
}
void loop()
{
digitalClockDisplay();
delay(1000);
}
void digitalClockDisplay(){
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
}
void printDigits(int digits){
// utility function for clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}

The error message that I received was that most things weren't declared.

sketch_dec24a.ino: In function 'void setup()':
sketch_dec24a:10: error: 'setTime' was not declared in this scope
sketch_dec24a.ino: In function 'void digitalClockDisplay()':
sketch_dec24a:19: error: 'hour' was not declared in this scope
sketch_dec24a:20: error: 'minute' was not declared in this scope
sketch_dec24a:21: error: 'second' was not declared in this scope
sketch_dec24a:23: error: 'day' was not declared in this scope
sketch_dec24a:25: error: 'month' was not declared in this scope
sketch_dec24a:27: error: 'year' was not declared in this scope

I am not sure what that means exactly and how would I fix that?

is the time library installed correctly?
details see - Arduino Playground - Time -

The sketch file uses an external library '#include <Time.h>' that is not part of the standard Arduino IDE. You need to find this library (I assume the book will tell you where) and save it to the right place (I also expect the book to explain where to save it) for the program to find it and compile okay.

Thanks for the replies.

I didn't realize that the TimeLibrary wasn't a native library - so thanks for that.

However, I have now installed it and still received the same error.

sketch_dec24a.ino: In function 'void setup()':
sketch_dec24a:10: error: 'setTime' was not declared in this scope
sketch_dec24a.ino: In function 'void digitalClockDisplay()':
sketch_dec24a:19: error: 'hour' was not declared in this scope
sketch_dec24a:20: error: 'minute' was not declared in this scope
sketch_dec24a:21: error: 'second' was not declared in this scope
sketch_dec24a:23: error: 'day' was not declared in this scope
sketch_dec24a:25: error: 'month' was not declared in this scope
sketch_dec24a:27: error: 'year' was not declared in this scope

Where did you get the library (link?) and where did you put the library? It should be in 'Where you installed Arduino\libraries' and the folder must be called 'Time'. You will need to close/open the Arduino IDE for it to recognize the new library

I got the library link from Arduino Playground - HomePage

I put the library under the following subsection:
arduino-1.0.2>>libraries>>Time>>Time>>Time.h

I did close the Arduino IDE.

Any other ideas?

Thanks.

You have gone a bit too deep with the folders structure. When you extract the zip file there are 3x folders called DS1307RTC, Time & TimeAlarms. You put the Time folder in arduino-1.0.2\libraries so you end up with
arduino-1.0.2>>libraries>>Time>>Time.h

Thanks.

It worked!

You are a champ.

Now I'd like to understand the "why" behind the "how".

Thanks

agrinshtein:
Now I'd like to understand the "why" behind the "how".

When you put a line like '#include <Time.h>' in a sketch it tells the compiler to look in the libraries folder (Arduino-1.0.2\Libraries in your case) for a folder called 'Time' and within that folder should be at least a couple of files that share a similar name 'Time.h' and 'Time.cpp'. Look in 'Libraries\Servo' folder and you will see the corresponding 'Servo.h' & 'Servo.cpp' files. The .h file is a header file and the .cpp is a program file.

It should be in 'Where you installed Arduino\libraries'

No it should NOT. That is where core libraries go, NOT user downloaded libraries. Stop telling people to put libraries there.

User downloaded libraries go in the sketchbook folder, in the libraries directory. If there isn't one there already, it isn't rocket science to figure out that you need to MAKE ONE!.

PaulS:

It should be in 'Where you installed Arduino\libraries'

No it should NOT. That is where core libraries go, NOT user downloaded libraries. Stop telling people to put libraries there.

ACCORDING TO THE ARDUINO GUIDE http://arduino.cc/en/Guide/Libraries IT'S WHERE THEY SHOULD GO.

How to Install a Library

Libraries are often distributed as a ZIP file or folder. The name of the folder is the name of the library. Inside the folder will be a .cpp file, a .h file and often a keywords.txt file, examples folder, and other files required by the library.

To install the library, first quit the Arduino application.

Then uncompress the ZIP file containing the library. For example, if you're installing a library called "ArduinoParty", uncompress ArduinoParty.zip. It should contain a folder called ArduinoParty, with files like ArduinoParty.cpp and ArduinoParty.h inside. (If the .cpp and .h files aren't in a folder, you'll need to create one. In this case, you'd make a folder called "ArduinoParty" and move into it all the files that were in the ZIP file, like ArduinoParty.cpp and ArduinoParty.h.)

Drag the ArduinoParty folder into this folder (your libraries folder). Under Windows, it will likely be called "My Documents\Arduino\libraries". For Mac users, it will likely be called "Documents/Arduino/libraries". On Linux, it will be the "libraries" folder in your sketchbook.

Your Arduino library folder should now look like this (on Windows):

My Documents\Arduino\libraries\ArduinoParty\ArduinoParty.cpp
My Documents\Arduino\libraries\ArduinoParty\ArduinoParty.h
My Documents\Arduino\libraries\ArduinoParty\examples
....

or like this (on Mac):

Documents/Arduino/libraries/ArduinoParty/ArduinoParty.cpp
Documents/Arduino/libraries/ArduinoParty/ArduinoParty.h
Documents/Arduino/libraries/ArduinoParty/examples
...

or similarly for Linux.

There may be more files than just the .cpp and .h files, just make sure they're all there.

(The library won't work if you put the .cpp and .h files directly into the libraries folder or if they're nested in an extra folder. For example:

Documents\Arduino\libraries\ArduinoParty.cpp and
Documents\Arduino\libraries\ArduinoParty\ArduinoParty\ArduinoParty.cpp

won't work.)

Restart the Arduino application. Make sure the new library appears in the Sketch->Import Library menu item of the software.

That's it! You've installed a library!

User downloaded libraries go in the sketchbook folder, in the libraries directory. If there isn't one there already, it isn't rocket science to figure out that you need to MAKE ONE!.

MAYBE SOMEONE SHOULD UPDATE THE GUIDE TO REFLECT THIS THEN, ELSE IT DOES BECOME ROCKET SCIENCE.

Riva:
ACCORDING TO THE ARDUINO GUIDE http://arduino.cc/en/Guide/Libraries IT'S WHERE THEY SHOULD GO.

You are misreading the guide.

The directory that the guide refers to, which PaulS also tells you is where downloaded libraries should go, is under My Documents.

The directory you referred to ('Where you installed Arduino\libraries') is under the Arduino IDE software installation, under Program Files.

PeterH:

Riva:
ACCORDING TO THE ARDUINO GUIDE http://arduino.cc/en/Guide/Libraries IT'S WHERE THEY SHOULD GO.

You are misreading the guide.

The directory that the guide refers to, which PaulS also tells you is where downloaded libraries should go, is under My Documents.

The directory you referred to ('Where you installed Arduino\libraries') is under the Arduino IDE software installation, under Program Files.

Then the guide needs clarifying, I have no arduino directory in My Documents but following the letter of the guide and putting them in the libraries directory where arduino IDE is installed it works. It does not mention anything about putting them in a libraries folder in where your sketch folder is as this is what I assume Paul meant. And while I'm on my rant the reference help that is downloaded with 1.0.3 IDE is out of date. The one example I can raise is the Serial.begin() with the extra protocol argument that was added but not mentioned in the downloaded version.

And while I'm on my rant the reference help that is downloaded with 1.0.3 IDE is out of date. The one example I can raise is the Serial.begin() with the extra protocol argument that was added but not mentioned in the downloaded version.

Are you volunteering? Maintaining documentation is a thankless job that is low priority.

Thanks for that discussion. All discussion is useful for a noob.

So bottom line is that I am putting the "Time" library in the newly made Sketchbook folder.

So what I have got is the following

arduino-1.0.2/libraries/Sketchbook/Time/Time.cpp and Time.h

Is that correct?

Perhaps this should go under a new topic, however, since this one was the predecessor to this problem I decided to keep it here.

So I have figured out how to have the arduino trigger a count of the time starting from the time that I had input into the original code. setTime(12,0,0,1,1,11); // set time to noon Jan 1 2011

Now I took the next step which is to have the arduino receive the real time from the computer using UNIX time that I got from epochconverter.com.

Now the next step is to try to figure out how to do the following. I would like to wire up my arduino with a button. When the button is pressed I want to have the arduino send the time that the button was pressed to the serial monitor.

Would it go something like this...

IF button pressed
THAN Serial.print the time

Button being a variable?

Thanks for your assistance to this noob. Im getting the hang of it bit by byte ;).

agrinshtein:
Thanks for that discussion. All discussion is useful for a noob.

So bottom line is that I am putting the "Time" library in the newly made Sketchbook folder.

So what I have got is the following

arduino-1.0.2/libraries/Sketchbook/Time/Time.cpp and Time.h

Is that correct?

Absolutely not.

What operating system are you on? Looks like Linux / Mac judging by the slashes.

In the Arduino app preferences it tells you the sketchbook location. In my case:

/Users/nick/Documents/Arduino

In that folder should be a libraries folder, ie.

/Users/nick/Documents/Arduino/libraries

Make "libraries" if it isn't there.

Put the library inside libraries, ie.

/Users/YOURNAME/Documents/Arduino/libraries/Time/Time.cpp (and Time.h)

Your installed Arduino folder (arduino-1.0.2) should be untouched by human hands. Otherwise when you upgrade to 1.0.3 you will lose all your installed libraries.

Having done that restart the IDE, so it notices the new library.

Riva:
You put the Time folder in arduino-1.0.2\libraries so you end up with
arduino-1.0.2>>libraries>>Time>>Time.h

Sorry, Riva. The link:

http://arduino.cc/en/Guide/Libraries

specifically mentions "Documents" and "My Documents". That is referring to your stuff (documents), not the installed Arduino executable.

[quote author=Nick Gammon link=topic=138859.msg1044545#msg1044545 date=1356478441]

Riva:
You put the Time folder in arduino-1.0.2\libraries so you end up with
arduino-1.0.2>>libraries>>Time>>Time.h
Sorry agrinshtein, your thread has gone off topic.

Sorry, Riva. The link:

http://arduino.cc/en/Guide/Libraries

specifically mentions "Documents" and "My Documents". That is referring to your stuff (documents), not the installed Arduino executable.[/quote]
It might specifically mention "Documents" and "My Documents" but as it's not explicitly stated the implication (to me) is the folder where you installed the arduino software. No where (currently) on that page does it say

That is referring to your stuff (documents), not the installed Arduino executable.

Would it be better to at least say put them in the libraries folder where your sketches are saved and not the executable/libraries folder.
The default download location in my older version of Windows is 'My Documents' and as the Windows install instructions on this page http://arduino.cc/en/Guide/Windows state

When the download finishes, unzip the downloaded file. Make sure to preserve the folder structure. Double-click the folder to open it. There should be a few files and sub-folders inside.

then what is one to conclude.
The bottom line is the instructions are ambiguous. Had they been better worded like Limor has on her Adafruit site Arduino Hacks non of this would happen.

Riva:
Would it be better to at least say put them in the libraries folder where your sketches are saved ...

You could well be right, sadly I don't have edit permission for those pages.

You could raise a bug report on the GitHub code site. Maybe something will be done. More likely than reporting it here.