I'm using Timer1 library for generation of pulse. But in my code, at some places i need to change the timer values. Can anyone suggest the ways to do it.

In below code in place of 370000, I need to change the value after getting some inputs in conditional loops. Ex. if (x==1) Timer value should be 370000 else 185000.

#include <TimerOne.h>

int count = 0;
int LED = 13;

void setup() {
 pinMode(LED, OUTPUT);

void loop() {
 Serial.print("Count Value: ");

2 Answers 2


When you look into TimerOne.h, which is the header file for the TimerOne library, you can see at line 55 the method

void setPeriod(unsigned long microseconds)

which can be used to set the period of the timer at any time in your program. The method TimerOne.initialize() also calls this method to set the timer period.


Just use a variable...

unsigned long val = 0;

void setup(){
    val = 370000;
  else {
    val = 185000;
  • Calling the initialize method multiple times feels wrong. In some libraries this can have some weird side effects. In the case of Timer1 if you call detachInterrupt and then change the period using initialize, instead of setPeriod, the interrupts will start again, and you ISR will get called. Though I admit that's a bit of an edge case.
    – Gerben
    Apr 22, 2020 at 11:55
  • Who said to call it multiple times? It's only called once in setup. Is there more to this than was in the question? I thought we were coming up with a way to alter the 370000 number. Question should be more specific. If this answer is a downvote for answering exactly what the question said then I'm downvoting the question for being vague.
    – Delta_G
    Apr 22, 2020 at 13:21
  • @Delta_G - You can call the initialize function twice per minute with different values if you like. I've built a clock that does just that, and has been running for over a year with no issues. arduino.stackexchange.com/a/68627/37523 +1 for your answer.
    – VE7JRO
    Apr 22, 2020 at 14:58
  • I still say the question didn't ask to do it over and over. The question just asked how to do it once in setup.
    – Delta_G
    Apr 22, 2020 at 15:01
  • @Delta_G - The question is not the greatest, but it does say ...at some places i need to change the timer values. I still like your answer :)
    – VE7JRO
    Apr 22, 2020 at 15:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.