# redefining variable for use in timer interrupt

**URL:** <https://forum.arduino.cc/t/redefining-variable-for-use-in-timer-interrupt/264001>\
**Category:** Programming\
**Created:** [October 18, 2014, 1:10pm UTC](https://forum.arduino.cc/t/redefining-variable-for-use-in-timer-interrupt/264001 "2014-10-18T13:10:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon20081101](https://avatars.discourse-cdn.com/v4/letter/a/b77776/32.png) [@anon20081101](https://forum.arduino.cc/u/anon20081101)\
**Post date:** [October 18, 2014, 1:10pm UTC](https://forum.arduino.cc/t/redefining-variable-for-use-in-timer-interrupt/264001/1 "2014-10-18T13:10:20Z")

</div>

I am using a timer interrupt to control various counters, the speed of the counter is controlled using the speeder variable which is defined above setup() so its global, however when i try redefining this within the loop function it doesn't do anything? I need to be able to redefine it several times and i'm unsure how to do it.

```cpp
int speeder; // speed of counter set to 30 for bounce

void setup()
{
  
  Serial.begin(9600);
  pinMode(pirpin,INPUT);
  digitalWrite(pirpin,LOW);

// initialize Timer1
cli();          // disable global interrupts
TCCR1A = 0;    // set entire TCCR1A register to 0
TCCR1B = 0;    // same for TCCR1B

// set compare match register to desired timer count:
//OCR1A = 15624; Original speed control
OCR1A = speeder; 

// turn on CTC mode:
TCCR1B |= (1 << WGM12);

// Set CS10 and CS12 bits for 1024 prescaler:
TCCR1B |= (1 << CS10);
TCCR1B |= (1 << CS12);

// enable timer compare interrupt:
TIMSK1 |= (1 << OCIE1A);

calculate_ofsets();

// enable global interrupts:
sei();
}

void loop(){

if (digitalRead(pirpin) == HIGH){
  speeder=30;
tracker ++;
 // bounce = true;
}
else {
  
  tracker = 0;
}
}

```

---

<div class="post-metadata">

**Author:** ![Peter\_n](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@Peter\_n](https://forum.arduino.cc/u/Peter_n)\
**Post date:** [October 18, 2014, 1:42pm UTC](https://forum.arduino.cc/t/redefining-variable-for-use-in-timer-interrupt/264001/2 "2014-10-18T13:42:05Z")

</div>

The 'speeder' is not set to an initial value.  
It probably starts with zero, but you did not assign a value to it when you set OCR1A to 'speeder'.

In the loop you change 'speeder' to 30. That is okay. But you change that variable, without using that variable. I think you can set OCR1A directly to a new value.

Why do you want to change the frequency of the timer interrupt ? Can you have a fixed frequency and do the counting and different timing in software ?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 8:56pm UTC](https://forum.arduino.cc/t/redefining-variable-for-use-in-timer-interrupt/264001/3 "2021-05-05T20:56:01Z")

</div>

Closed on May 5, 2021, 8:56pm UTC


