Creating Libraries HELP!!!

Hi all i’m very new to this whole programming thing but would just like a little help on a library i’m trying to create.
I have made successfully a sketch for displaying the time in 12 hour format. (Not that difficult i know ha ha). But i would like to create a library so as the clock function operates in the background and i can just display the time by calling lcd.print(Hours(or Minutes or Seconds)) in my main sketch of whatever program. Here is the cpp file:

/*
Clock.cpp - Library for Simple Clock Display
12 Hour Format
Call on integers Hours Minutes & Seconds to use
*/
#include <WProgram.h>
#include <Clock.h>

Clock::Clock(unsigned int Hours)
{
_Hours=Hours;
}
Clock::Clock(unsigned int Minutes)
{
_Minutes=Minutes;
}
Clock::Clock(unsigned int Seconds)
_Seconds=Seconds;
}

void Clock::ClockFunction()
{
_Seconds=++_Seconds;
if(_Seconds==60)
{ _Seconds=0;}
if(_Seconds==0)
{_Minutes=++_Minutes;}
if(_Minutes==60)
{_Minutes=0;
_Hours=++_Hours;}
if(_Hours==13)
{_Hours=01;}
delay (1000);
}

and here is the header file.

#ifndef Clock
#define Clock

#include <WProgram.h>

class Clock
{
public:
Clock (unsigned int Hours);
Clock (unsigned int Minutes);
Clock (unsigned int Seconds);
void ClockFunction;

private:
unsigned int _Hours;
unsigned int _Minutes;
unsigned int _Seconds;

};
#endif

Can anyone enlighten me as to what is going wrong here? it won’t compile when i try to make an example file for the library

Any help would be greatly appreciated :wink:

In the header...

#ifndef Clock[glow]_h[/glow]
#define Clock[glow]_h[/glow]

Thanks for replying but i think i have more than that wrong below is the sketch im trying to compile:

/*
This Sketch should display on a 16x2 line LCD the time 12:00:01
and count up. Used for general time display.
To set the time Enter the value agaist Hours Minutes and Seconds
in the Setup
*/
#include <Clock.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup(){
lcd.begin(16,2);
delay(1000);
lcd.clear();
lcd.setCursor(2,0);
delay(1000);
Hours=12;
Minutes=00;
Seconds=1;
delay(500);
}

void loop(){
// ClockFunction(); not sure if needed
clockDisplay();
}

void clockDisplay(){
lcd.print(Hours,DEC);
lcd.print(":");
if(Minutes<10){
lcd.print(“0”);
}
lcd.print(Minutes,DEC);
lcd.print(":");
if(Seconds<10){
lcd.print(“0”);
}
lcd.print(Seconds,DEC);
}

These are the errors that come up:

In file included from C:\arduino-0021\libraries\Clock\Clock.cpp:7:
Clock.h:10: error: ‘Clock::Clock(unsigned int)’ cannot be overloaded
Clock.h:9: error: with ‘Clock::Clock(unsigned int)’
Clock.h:11: error: ‘Clock::Clock(unsigned int)’ cannot be overloaded
Clock.h:9: error: with ‘Clock::Clock(unsigned int)’
Clock.h:12: error: variable or field ‘ClockFunction’ declared void
Clock.cpp:14: error: redefinition of ‘Clock::Clock(unsigned int)’
Clock.cpp:10: error: ‘Clock::Clock(unsigned int)’ previously defined here
Clock.cpp:19: error: expected initializer before ‘_Seconds’
Clock.cpp:20: error: expected declaration before ‘}’ token

I deleted the file extensions that get repeated over and over to make it easier to read

The error message is the compiler's friendly way of telling you to write proper code and not blindly copy and paste stuff.

     [glow]Clock[/glow] ([glow]unsigned int[/glow] Hours);
     [glow]Clock[/glow] ([glow]unsigned int[/glow] Minutes);
     [glow]Clock[/glow] ([glow]unsigned int[/glow] Seconds);

And how do you propose that anyone would know which of the three constructors will be called? Round-robin or psychic inspiration? Design your class properly and the library will compile and work without problems.

Korman

Well as i mentioned i am only new to this i was just trying to follow the tutorial on arduino website. So if anyone wants to lend a hand rather than just criticise me feel free. I was following the Morse example on the tutorial.

What's there more to help? Fix the names of your methods and all will be well. I'm certain the example on the Arduino site didn't instruct you to create 3 constructors with the same parameter type.

Sometimes it's just necessary to understand a little what one copies to make it work. If in doubt, go back to your C++ text-book and look up the first two examples about classes, that should be enough. Things to look for are: What's a method? What's a constructor?

Korman

Overloaded functions are allowed, as long as the arguments to the overloaded function are different. Different as in different number of arguments or different type of arguments, not different as in different names for the arguments.

In your existing class, which of the constructor overloads would you expect this code to invoke:

Clock timePiece(4);

to invoke?

You need to define why you expect to initialize a clock with just hours, just minutes, or just seconds. Setting just hours, just minutes, or just seconds makes sense. Reading just hours, just minutes, or just seconds makes sense. Initializing a clock with just minutes does not. At least not to me.

So, start with an explanation of what the class is for, what data it should manage, and what you need to be able to set and get. From that, the various constructors and set and get methods should be obvious. More difficult, of course, is implementing the class, but you haven't got that far, yet.

As you seem to be out of your depth in this case, why don't you use the perfectly fine Time class. It has most stuff you need and has the big advantage that it keeps the time correctly as opposed to your example of blind chicken software development. Once you get the syntax of your class worked out, you'll run the logic errors you have in your class design. This is a perfect example where reusing working code is better than building a shoddy version on your own.

As to you being a beginner, it's very advisable to learn to walk before trying to run.

Korman

i’m having trouble with this other code Korman can you tell me what it returns

void loop()
{
Serial.println(“Suck my balls”);
}

i’m having trouble with this other code Korman can you tell me what it returns

void loop()

As you see, the function loop is declared as having the return type void, thus no value is returned returned. But you don’t really have to worry about the return value of the function loop() anyway, because usually it’s only called inside the endless loop of the framework. If yo want to call it on your own, you may do so, but often it’s not necessary. If you decide to call loop recursively, you should take extra care that your stack doesn’t become too big and overwrites other variables in RAM. This can happen pretty quickly on the Arduino.

Korman