Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

probably flaw of start() #46

Open
5chufti opened this issue Nov 21, 2020 · 2 comments
Open

probably flaw of start() #46

5chufti opened this issue Nov 21, 2020 · 2 comments

Comments

@5chufti
Copy link

5chufti commented Nov 21, 2020

Description

At least on UNO with latest Arduino IDE and lib git version
timer.start() probably not working as seen in sketches developed with older version.
after a timer.stop() a timer.start() immediately calls ISR (not delayed by set interval)

(maybe it is just an undocumented sideeffect of recent version that stop() clears the set interval?)

Steps To Reproduce Problem

modified example shows problem: see below

Hardware & Software

Board UNO
Arduino IDE version
Version info & package name (from Tools > Boards > Board Manager) git version as of 21.Nov.2020
Operating system & version Win7

Arduino Sketch

#include <TimerOne.h>

const int led = LED_BUILTIN;  // the pin with a LED

void setup(void)
{
  pinMode(led, OUTPUT);
  Timer1.initialize(150000);
  Timer1.attachInterrupt(blinkLED); // blinkLED to run every 0.15 seconds
  Serial.begin(9600);
}

int ledState = LOW;
volatile unsigned long blinkCount = 0; // use volatile for shared variables

void blinkLED(void)
{
  Timer1.stop();
  if (ledState == LOW) {
    ledState = HIGH;
    blinkCount = blinkCount + 1;  // increase when LED turns on
  } else {
    ledState = LOW;
  }
  digitalWrite(led, ledState);
  Timer1.start();        //Timer1.initialize(150000); works in sketches w older version of lib
}

void loop(void)
{
  unsigned long blinkCopy;  // holds a copy of the blinkCount
  noInterrupts();
  blinkCopy = blinkCount;
  interrupts();

  Serial.print("blinkCount = ");
  Serial.println(blinkCopy);
  delay(100);
}

Errors or Incorrect Output

not blinking, counter increasing much too fast w/o blink

@vjenne
Copy link

vjenne commented Dec 19, 2020

Yes, I just ran into this problem too. My solution is as follows: In timerOne.h, i changed line 85.
TCNT1 = 0;
I changed 0 to 1. I think preloading this timer generates an interrupt the same as when an overflow happens like when the timer goes from 0xFFFF to 0x0000. Thus generating an overflow interrupt.
So if you change line 85 to
TCNT1 = 1;
No interrupt will happen. Your timing will be 1 clock tick faster than expected....

@jbidinger
Copy link

jbidinger commented Sep 3, 2022

I changed the Timer1.start() call to Timer1.resume() in blinkLED() and your sketch seems to be working. I'm testing on a Mega though. Having said that I did have problems with Timer1.start().

Looks like I'm nekrothreading here. Maybe it will help someone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants