Why "expected initializer before 'timer'`?

Here is the library: https://github.com/user14923929/ArduinoTimer/
Code:

#include <ArduinoTimer.h>
ArduinoTimer timer; // *Create an instance of the ArduinoTimer class

void setup() {
    Serial.begin(9600);
    timer.setTimeout(1000); // *Set timeout to 1000ms (1 second)
    timer.start();          // *Start the timer
}

void loop() {
    if (timer.tick()) {
        Serial.println("Timeout reached!");
        timer.start();      // *Restart the timer
    }
}

Error: expected initializer before 'timer'

The error should also tell you the source file and line.
Please copy/paste the entire output messages when compiling.

It is quite possible "user14923929" wrote a bad library.

In file included from /sketch/ArduinoTimer.cpp:1:0:
/sketch/ArduinoTimer.h:12:2: error: expected ';' after class definition
 }
  ^
  ;
/sketch/ArduinoTimer.cpp:4:1: error: prototype for 'int ArduinoTimer::setTimeout(long unsigned int)' does not match any in class 'ArduinoTimer'
 ArduinoTimer::setTimeout(unsigned long interval) {

 ^~~~~~~~~~~~
In file included from /sketch/ArduinoTimer.cpp:1:0:
/sketch/ArduinoTimer.h:3:14: error: candidate is: void ArduinoTimer::setTimeout(long unsigned int)
         void setTimeout(unsigned long interval);

              ^~~~~~~~~~
sketch.ino:2:14: error: expected initializer before 'timer'
 ArduinoTimer timer; // *Create an instance of the ArduinoTimer class
              ^~~~~
sketch.ino: In function 'void setup()':
sketch.ino:5:3: error: 'timer' was not declared in this scope
   timer.setTimeout(1000); // *Set the interval to 1000ms (1 second)
   ^~~~~
sketch.ino: In function 'void loop()':
sketch.ino:11:7: error: 'timer' was not declared in this scope
   if (timer.tick()) {
       ^~~~~

Error during build: exit status 1

From here: GitHub - user14923929/ArduinoTimer

ArduinoTimer.h

class ArduinoTimer {
    public:
        void setTimeout(unsigned long interval);
        void start();
        void stop();
        void reset();
        bool tick();
    private:
        unsigned long _interval;
        unsigned long _startTime;
        bool _running;
} // <-- error says missing ";" here

ArduinoTimer.cpp
#include "ArduinoTimer.h"
#include <Arduino.h>

ArduinoTimer::setTimeout(unsigned long interval) {
    _interval = interval;
    _running = false;
}

void ArduinoTimer::start() {
    _startTime = millis();
    _running = true;
}

void ArduinoTimer::stop() {
    _running = false;
}

void ArduinoTimer::reset() {
    _startTime = millis();
}

bool ArduinoTimer::tick() {
    if (_running && millis() - _startTime >= _interval) {
        _running = false;
        return true;
    }
    return false;
}

The class ArduinoTimer is missing a semi-colon, as the error states. (one of the errors)

Highly probable, I never heard of that library.

Anyway, if the OP had shown the full output it'd be easier for us. And, on top of that, if he/she doesn't have much experience I wonder why needs a library to just have "timers", and why that one in particular (there are dozens of others, like THIS or THIS just to mention the first couple of "timer" libraries).

Or learn the use of millis(), that's the best idea.

Probably was looking for "timer" with lower case "t" ... and found "Timer" with upper case "T"

I'm not sure about that. The "timer" is the variable defined in "ArduinoTimer timer;" and it looks like to be correct. But when compiling normally the first error is the main culprit because a missing semicolon on the library source will prevent the compiler to correctly process the subsequent part of code (and thowing many other errors out), so even the variable definition will be misinterpreted, and as the "timer" variable hasn't been defined, any usage will throw the compiler error.

Yep that's the one

poorly written library, author probably did not even compile the examples s/he is providing..

look this over

// single timer example

const unsigned long MsecPeriod = 1000;
      unsigned long msec0;
      unsigned long msec;

unsigned cnt;

void loop ()
{
    msec = millis ();
    if (msec - msec0 >= MsecPeriod)  {
        msec0 += MsecPeriod;
        Serial.println (++cnt);
    }
}

void setup ()
{
    Serial.begin (9600);
}

Yeah, no need for that bs library, I totally agree and agreed: :wink:

i think demonstrating how simple it can be used provides a better explanation. use of a library suggests complexity and ignorance.

i've seen newbies write code with nested loops waiting for time to pass in place of delay(). A timer goes hand-in-hand with state machines or something having state.

so i think just saying use millis() leaves a lot to be learned

why answer a coding question with so much text and so little code. Would you describe a math equation or write the equation? Would you describe an electrical circuit or show a schematic diagram?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.