# Variable for Arduino Timer

**URL:** <https://discourse.processing.org/t/variable-for-arduino-timer/28066>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [February 24, 2021, 9:27pm UTC](https://discourse.processing.org/t/variable-for-arduino-timer/28066 "2021-02-24T21:27:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![saaqib.m](https://avatars.discourse-cdn.com/v4/letter/s/ee7513/32.png) [@saaqib.m](https://discourse.processing.org/u/saaqib.m)\
**Post date:** [February 24, 2021, 9:27pm UTC](https://discourse.processing.org/t/variable-for-arduino-timer/28066/1 "2021-02-24T21:27:16Z")

</div>

Hi, I have been trying to set the timer constant of OCR1A to the variable TImer\_count so that when I change Sfreq it automatically updates Timer\_count and hence OCR1A. Does anyone know how to and what kind of variable I set Timer\_count to? Many thanks!

```auto
int Sfreq = 100;
unsigned long Timer_count = ceil(((16*10^6) / (Sfreq*1024) - 1));

voidSetup(){
// Previous timer code
OCR1A = 156; //-----> I want to set this number to Timer_count such that OCR1A = Timer_count
//After timer code
}

```

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [February 25, 2021, 6:02pm UTC](https://discourse.processing.org/t/variable-for-arduino-timer/28066/2 "2021-02-25T18:02:29Z")

</div>

Hi,

> [@saaqib.m](#):
>
> so that when I change Sfreq it automatically updates Timer\_count and hence OCR1A

You can place those instructions in the `draw()` function :

```auto
int Sfreq = 100;
unsigned long Timer_count;

void draw() {
  Timer_count = ceil(((16*10^6) / (Sfreq*1024) - 1));

  OCR1A = Timer_count;
}

```

Because the `draw()` function is going to be executed roughly 60 times per second, the `OCR1A` variable is going to be assigned to the `Timer_count` value that is related to the `Sfreq` value.

If you used `setup()` to assign your variables, then the function would only be executed once at startup and it’s not updating in real time.

(note that it’s `void setup()` not `void Setup()` 😉 )

---

<div class="post-metadata">

**Author:** ![saaqib.m](https://avatars.discourse-cdn.com/v4/letter/s/ee7513/32.png) [@saaqib.m](https://discourse.processing.org/u/saaqib.m)\
**Post date:** [February 26, 2021, 9:01pm UTC](https://discourse.processing.org/t/variable-for-arduino-timer/28066/3 "2021-02-26T21:01:09Z")

</div>

Hi there Joseph. Thank you so much. I am trying to implement this but it isn’t working for some reason?

Here is my full setup code:

```auto
#include <SoftwareSerial.h> 
#include <avr/io.h>

int i;
int r_pin = A0;
int sample_output;
int Sfreq = 1000;
unsigned long Timer_count;

void setup (){
  noInterrupts();
  Timer_count = (16*10^6) / (Sfreq*1024) - 1;
  Serial.begin (9600);
  ADCSRA |= (1 << ADPS0) | (1 << ADPS1) | (1 << ADPS2); // ADC Prescaler of 128

  //set timer1 interrupt at Sfreq Hz
  TCCR1A = 0;// set entire TCCR1A register to 0
  TCCR1B = 0;// same for TCCR1B
  TCNT1 = 0;//initialize counter value to 0
  // set compare match register for 1hz increments
  OCR1A = Timer_count;// = (16*10^6) / (Sfreq*Timer Prescaler) - 1 (must be <65536)
  // turn on CTC mode
  TCCR1B |= (1 << WGM12);
  // Set CS12 and and CS11 and CS10 bits for prescaler <----
  TCCR1B |= (1 << CS12) | (1 << CS10); //-- 1024
// TCCR1B |= (1 << CS12); //-- 256
// TCCR1B |= (1 << CS11) | (1 << CS10); //-- 64
// TCCR1B |= (1 << CS11); //-- 8
// TCCR1B |= (1 << CS10); //-- 1

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

  interrupts();
} // end of setup

```

Could you take a look please?

---

<div class="post-metadata">

**Author:** ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)\
**Post date:** [March 1, 2021, 8:23am UTC](https://discourse.processing.org/t/variable-for-arduino-timer/28066/4 "2021-03-01T08:23:14Z")

</div>

Hi @saaqib.m,

Your code is from arduino IDE, not Processing IDE.  
Try the arduino Forum instead, since your problem is related to arduino code.  
Here is the link to it:  
[https://forum.arduino.cc/](https://forum.arduino.cc/)

Best regards

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [March 7, 2021, 9:56pm UTC](https://discourse.processing.org/t/variable-for-arduino-timer/28066/5 "2021-03-07T21:56:55Z")

</div>

Hello,

The code you provided looks like this example:  
_[Arduino Timer Interrupts : 6 Steps (with Pictures) - Instructables](https://www.instructables.com/Arduino-Timer-Interrupts/)_

This is NOT correct for 1Hz:  
`Timer_count = (16*10^6) / (Sfreq*1024) - 1; // ^ is a Bitwise exclusive OR`

This is correct for 1Hz:  
` Timer_count = (16*1000000) / (Sfreq*1024) - 1;`

Reference:  
_[Summary of Operators (The Java™ Tutorials \> Learning the Java Language \> Language Basics)](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html)_
