[Solved] Timer.h

hello,
I want to use a "periodic" execution of a function, and the Timer library here:

https://playground.arduino.cc/Code/Timer#Installation

is (or, believe it is) what I need. Something like :

#include "Timer.h"
 
 
Timer t;
int pin = 13;
 
 
void setup()
{
  Serial.begin(9600);
  pinMode(pin, OUTPUT);

//  t.oscillate(pin, 100, LOW); not this
 
 t.every(1000, takeReading); //this one

}
 
 
void loop()
{
  t.update();
}
 
 
void takeReading()
{
  Serial.println(analogRead(0));
}

I have spent more than one hour trying to install, but no success. The folder Timer, Master-Timer and all files have been copied to any place you can imagine (including documents/arduino/libraries/..)

On compilation I get:
C:\Users\DK\AppData\Local\Temp\arduino_modified_sketch_406436_00TimerTest.ino:2:19: fatal error: Timer.h: No such file or directory

#include "Timer.h"

^

compilation terminated.

exit status 1

I miss something obvious....

  1. Download the library from the following link:
    https://github.com/JChristensen/Timer/archive/v2.1.zip
  2. Select Sketch > Include Library > Add .ZIP Library from the Arduino IDE menus.
  3. Select the downloaded file.
  4. Click the Open button.

Your user libraries should be here:

C:\Users\DK\Documents\Arduino\libraries\Timer

DKWatson:
Your user libraries should be here:

C:\Users\DK\Documents\Arduino\libraries\Timer

Not necessarily. They are located at {sketchbook folder}/libraries. The sketchbook folder location can be set to anything you like via File > Preferences > Sketchbook location.

@pert
thank you

my fault
tried manually instead of automatic (zip)

(but cannot understand why manual installation didnot work. I have done it >20 times until now...)

It's definitely a good skill to learn how to make manual installation work. These are the instructions:

  • Download https://github.com/JChristensen/Timer/archive/v2.1.zip
  • Unzip the downloaded file
  • Move the unzipped folder to {sketchbook folder}/libraries. You can find/set the location of your sketchbook folder in the Arduino IDE at File > Preferences > Sketchbook location.

Make sure the Timer library folder is directly under the libraries folder, not in a subfolder:
CORRECT:

{sketchbook folder}
|_libraries
|_Timer-2.1
|_Timer.h

INCORRECT:

{sketchbook folder}
|_libraries
|_Timer-2.1
|_Timer-2.1
|_Timer.h

Note that it's actually best practices to remove the -2.1 from the library folder, so that the folder name matches the primary .h file name. The reason is that if there are multiple files that match your #include directive, the file that matches the folder name has priority. GitHub adds the branch name to the downloaded .zip file, which causes the folder name to no longer match the .h filename. Usually that doesn't cause any problems and it seemed like it was best to provide the easiest possible instructions to get you to a working state (since from the error I could see there are no other files named Timer.h previously installed) but it's worth adding that info now that things are working for you and I'm providing extra info.

Note that the Timer library has what's called the Arduino Libraries 1.0 format. Other libraries will have the 1.5 format. The installed folder structure of a hypothetical 1.5 format library named "Foobar" would look like this:

{sketchbook folder}
|_libraries
|_Foobar
|_library.properties
|_src
|_Foobar.h

thanks again.
Although confused, I “believe” i followed all steps correctly. Even though (as I said) I had many copies (1 in doc…/lib…, 1 in ardu…/lib… both with all files in standalone AND in folders)
after .zip install , compiler sees ALL copies ans says which is used

Anyway it is over.
After first try, I spent 1+ hour to “find” that function in library takes 3 parameters but in example is with only2
https://playground.arduino.cc/Code/Timer
examples 3 and 4 line13
(and compilation fails)

This is over too, my time also
Good night

There are two different versions of the library. The 2.x version I recommended to you because (according to the documentation) it has some significant improvements. There is also a 1.x version, which you can find here:
https://github.com/JChristensen/Timer/archive/master.zip
It's quite possible that the Arduino Playground documentation is for the 1.x version. Also note there is a book on Arduino by Simon Monk which refers to the 1.x library. You should be able to find demonstrations of the usage of the specific version you have installed under File > Examples > Timer (or whatever the installed library folder was named).

you are probably right.

I come back on post because of this:
I thought that "timers" in general use the ...timers (hardware). But as I see on this lib, it uses millis(). So it brings the question:
what happens in millis() overload (rollover)? Follows part of the lib:

int8_t Timer::every(unsigned long period, void (*callback)(void*), int repeatCount, void* context)
{
    int8_t i = findFreeEventIndex();
    if (i == -1) return -1;

    _events[i].eventType = EVENT_EVERY;
    _events[i].period = period;
    _events[i].repeatCount = repeatCount;
    _events[i].callback = callback;
    _events[i].lastEventTime = millis();
    _events[i].count = 0;
    _events[i].context = context;
    return i;
}

Since this lib "keeps" the millis of last event, how it compares with new millis after rollover?

As you understand I need a hardware timer (counting clock ticks). Millis() makes demanding to ckeck if "old value" is smaller than millis() value (normal run from start until rollover) or greater (1st time after rollover) before any compare.

https://github.com/JChristensen/Timer/blob/v2.1/Event.cpp#L45

if (now - lastEventTime >= period)

This code will correctly handle millis() overflow or rollover. That’s all that is necessary. Do a search for this topic and you will find some detailed explanation.

Oh! that in the update !! I did not look there.

ThankYou++;

Have a nice day