Writing a library

I want to make a library and in order to learn that i staeted by writing very simple codes for setting a timer.

In the IDE i have written:
#include <AllTimer.h>

Alltimer mytimer;

void Setup()
{
pinMode(13,OUTPUT);
Serial.begin(9600);
}

void loop()
{
mytimer.setTimer(1);
}

And the error i am getting is

alltimer_1:2: error: ‘Alltimer’ does not name a type
alltimer_1.cpp: In function ‘void loop()’:
alltimer_1:12: error: ‘mytimer’ was not declared in this scope

What changes should i make in the header and cpp files?

AllTimer.h (124 Bytes)

Alltimer.cpp (402 Bytes)

Did you restart the IDE after creating your library?

electronovice: ::::SNIP:::: And the error i am getting is

alltimer_1:2: error: 'Alltimer' does not name a type alltimer_1.cpp: In function 'void loop()': alltimer_1:12: error: 'mytimer' was not declared in this scope

What changes should i make in the header and cpp files?

electronovice,

It's been quite a while, so this may be incorrect.

on alltimer_1:2: error: 'Alltimer' does not name a type this may need some type of return type for the compiler to pass it.

on alltimer_1:12: error: 'mytimer' was not declared in this scope is because it does not understand your previous prototype/declaration.

ALSO, there is no mytimer.setTimer(1); in your library; there is a Set().

Let us know of your success. Jesse

You have quite a few errors in your library files. You declare a member function with the name of setTimer yet within your cpp file you call it Set. Furthermore you omit the “void” in front of the function definition.

I’m not even sure what you mean by this line.
int x = (time_ms*1.5625); Are you trying to use a template or perhaps attempting to cast the result of a calculation to a static type?

I’m sure you’d be all over this if you had your environment setup correctly.

Here’s how I suggest you get started.

Open the IDE
click the down arrow on far right hand side of the IDE and select “New Tab”
Enter the name as AllTimer.h
Do the same again to create another tab named AllTimer.cpp
Now you can click on the separate tabs to enter your code in the relevant files
When you hit compile all three files will be compiled and show you your errors and warnings accordingly.

Once you have your library files working to your satisfaction you can close the IDE
Create a separate directory (within your libraries directory) called AllTimer
Copy the .h and .cpp files from your original sketch directory into this new directory, and there you go.

BTW Happy new year! :slight_smile: