1

Following is the code I found in Arduino example files. It compiled fine on Arduino UNO. But there was an error when I tried to compile it for nodeMCU 1.0(ESP 12E Module).

#include <TimerOne.h>
void setup() {
 pinMode(13,OUTPUT);
 Timer1.initialize(5000000);//5 sec
 Timer1.attachInterrupt(timerIsr);
}
void loop() {}
void timerIsr(){
 digitalWrite(13,!digitalRead(13));
}

Following was the error message. I had this kind of error previously on OneWire.h and I solved it by replacing the OneWire.h library with another OneWire.h library that was made especially for nodeMCU.

Error msg:

Arduino: 1.8.3 (Windows 8.1), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, 115200, 4M (3M SPIFFS)"
In file included from D:\iox Prime\TEC project\code\building modules\timerIsrDemo\timerIsrDemo.ino:1:0:
C:\Program Files (x86)\Arduino\libraries\TimerOne/TimerOne.h:39:20: fatal error: avr/io.h: No such file or directory
 #include <avr/io.h>
 ^
compilation terminated.
exit status 1
Error compiling for board NodeMCU 1.0 (ESP-12E Module).
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

I searched the library everywhere in the internet to solve this error but end up with no success. I wish somebody modifies the existing library for me. Thank you for the help.

asked Oct 7, 2017 at 11:49

3 Answers 3

2

I have found a solution. There is a library called Ticker.h I found this on github: https://github.com/esp8266/Arduino/tree/master/libraries/Ticker

Following is my demo code:

#include <Ticker.h>
Ticker ticker;
void setup() {
 pinMode(D7,OUTPUT);
 ticker.attach(1,isrFunc);//tickerObj.attach(timeInSecs,isrFunction)
}
void loop() {}
void isrFunc(){
 digitalWrite(D7,!digitalRead(D7));
}

For me, this code worked seamlessly on nodeMCU. Thank you.

answered Oct 7, 2017 at 16:51
1

The TimerOne library is specifically for Timer 1 on the AVR microcontrollers1. No other microcontroller has the exact same timer structure, so the library cannot be used with them.

You need a library that works with the timers the ESP8266 has, or you need to manually manipulate the timer registers, for which you will need to consult and understand the datasheet.


1: It also appears to have #defines in it for ARM (Teensy 3.x) chips.

answered Oct 7, 2017 at 12:01
0

There is a another Ticker.h for non ESP processors like UNO, see below:

// Filename: Ticker_Example - Does'nt work on ESP)
// this is based upon published version V3 of 3/2018
// refer to : https://github.com/sstaub/Ticker/blob/master/examples/Example.ino
// Complete example. Here we created five timers, you can run it and test the result
// in the Serial monitor and the on board LED.
#include "Ticker.h"
void printMessage();
void printCounter();
void printCountdown();
void blink();
void printCountUS();
bool ledState;
int counterUS;
/*
Creates a Ticker object Constructors / Destructor
Ticker(fptr callback, uint32_t interval, uint16_t repeats = 0, resolution_t resolution = MICROS)
 parameter callback for the function name you want to call
 parameter interval sets the interval time in ms or us when using MICROS_MICROS with the resolution parameter
 parameter repeats sets the number of repeats the callback should executed, 0 is endless
 parameter resolution sets the internal resolution of the Ticker, it can MICROS, MICROS_MICROS or MILLIS
or in Ray's simpler terms... Ticker (give it a name)(the callback function, interval, number of times, resolution)
*/
Ticker timer1(printMessage, 0, 1); // once, immediately
Ticker timer2(printCounter, 1000, MILLIS); // internal resolution is milli seconds
Ticker timer3(printCountdown, 1000, 5); // countdown 5 times, every second
Ticker timer4(blink, 500); // changing led every 500ms
// Ticker timer5(printCountUS, 100, 0, MICROS_MICROS); // originally used 'MICRO_MICROS' so the the interval time would be 100us
 //with a internal resolution of micro seconds BUT this did not compile
Ticker timer5(printCountUS, 2000, 0, MICROS); // now set to 2 seconds
void setup() {
 pinMode(LED_BUILTIN, OUTPUT);
 Serial.begin(9600);
 delay(2000);
 timer1.start();
 timer2.start();
 timer3.start();
 timer4.start();
 timer5.start();
 }
void loop() {
 timer1.update();
 timer2.update();
 timer3.update();
 timer4.update();
 timer5.update();
 }
void printCounter() {
 Serial.print("Counter ");
 Serial.println(timer2.getRepeatsCounter());
// Serial.println(timer2.counter()); // this original line did not compile
 }
 void printCountdown() {
 Serial.print("Countdown ");
 Serial.println(timer3.getRepeats() - timer3.getRepeatsCounter());
 // Serial.println(4 - timer3.counter()); // this original line did not compile
 }
void printMessage() {
 Serial.println("Hello!");
 }
void blink() { // blink every 500ms
 digitalWrite(LED_BUILTIN, ledState);
 ledState = !ledState;
 }
void printCountUS() {
 counterUS++;
 // if (counterUS == 10000) {
 // Serial.println("10000 * 100us");
 if (counterUS == 5) {
 Serial.print("5 * 2000mS when millis = ");
 Serial.println(millis());
 counterUS = 0;
 }
 }
/*
Functions called in format 'timer1.start();' for the Ticker previously named timer1
void start()
Start the Ticker. Will count the interval from the moment that you start it. If it is paused, it will restart the Ticker.
void resume()
Resume the Ticker. If not started, it will start it. If paused, it will resume it. For example, in a Ticker of 5 seconds, if it was paused in 3 seconds, the resume in continue in 3 seconds. Start will set passed time to 0 and restart until get 5 seconds.
void pause()
Pause the Ticker, so you can resume it.
void stop()
Stop the Ticker.
void update()
Must to be called in the loop(), it will check the Ticker, and if necessary, will run the callback
status_t state()
Returns the state of the Ticker.
uint32_t elapsed()
Returns the time passed since the last tick, ms or us depending from the resolution.
uint16_t counter()
Returns the number of executed callbacks.
 */ 
per1234
4,2782 gold badges24 silver badges43 bronze badges
answered Mar 30, 2018 at 8:17

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.