PlatformIO Community

Timer1 library and code not functioning

I installed the Timer1 library and tried this code :

#include <Arduino.h>
#include <TimerOne.h>

String ledStatus = "OFF";
int blueLED = 6;
int greenLED = 5;


void setup() {

  pinMode(blueLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  Timer1.initialize(1000000);
  Timer1.attachInterrupt(blinkGreen);
}

void loop() {
  digitalWrite(blueLED, HIGH);
  delay(250);
  digitalWrite(blueLED, LOW);
  delay(250);
}

void blinkGreen() {

  if (ledStatus == "ON") {          // Start of if1 loop
    digitalWrite (greenLED, LOW);
    ledStatus = "OFF";
    return;  // leave this function --> void blinkGreen and return to the main code --> void loop
  } // End of if1 loop

  if (ledStatus == "OFF") {       // Start if2 loop
    digitalWrite(greenLED, HIGH);
    ledStatus = "ON";
    return;
  }  // End if2 loop


}

when uploading it I get this error :

src/main.cpp: In function ‘void setup()’:
src/main.cpp:14:26: error: ‘blinkGreen’ was not declared in this scope
Timer1.attachInterrupt(blinkGreen);
^
*** [.pioenvs/nanoatmega328/src/main.cpp.o] Error 1
========================== [ERROR] Took 5.76 seconds ==========================

Linter
Severity Provider Description Line
Error GCC ‘blinkGreen’ was not declared in this scope 14:26
Error Build error: ‘blinkGreen’ was not declared in this scopee 14:26
PIO Buildsrc/main.cpp20040:1(39, 759)
LFUTF-8C++0 files

And the same code works flawlessly if uploaded using Arduino IDE
What could be wrong?

Put blinkGreen function above setup().
Or add a declaration: void blinkGreen(); above setup().
This is done implicitly by Arduino IDE.

1 Like

Thank you a million :slight_smile:
I added the line void blinkGreen( ); before void setup ( ) and now It is working flawlessly

Thanks again

I might be a bit of a noob here, but isn’t it pretty standard for any ANSI C derivative languages to allow the functions to be pretty much anywhere in the file, and have the compiler still find them?

As fair as I understood; the Arduino framework is just a bunch of libraries on top of C++, so it seems a bit odd to me that we need to put ISR defs above setup(). Also by my interweb suggests that the people using the vanilla Arduino IDE (gcc IIRC) aren’t having this problem.

Could someone please clarify what’s going on, or point me to what I’m missing? Is this simply a “gcc vs Visual Studio” thing, or some kind of an ANSI-compliance thing?

Thanks,
Nrrdzilla

No, it’s not.

You can put them anywhere in the file if you first declare their existence before they are used for the first time. If the compiler doesn’t know about the existence of the function before it encounters it’s first use, it will spit the dummy.

The Arduino IDE has a preprocessor system (Arduino builder) which does things in a non-C/C+±standard manner - such as generating function prototypes itself, and mashing all the open tabs in a sketch together, making it a single file.

It’s just easier to shove the setup and loop at the end of the file, then you don’t need to declare prototypes at all since they’ll have all defined by the time they are called in setup or loop. :wink:

Should be Timer1.attachInterrupt(&blinkGreen);

1 Like