Timers and Interrupts in Function

I am looking to control a stepper motor using timers and interrupts. I have been able to output the square wave of 400 hz that I want. What I want to do is compare two values and depending on the result switch the pin the square wave is being outputted from.

Pseudo code:
Here x is going to be the desired position of the motor
y is going to be the actual position imputed from a encoder

if x > y
toggle pin 12

if x < y
toggle pin 13

if x = y
do nothing

I am having trouble incorporating this with my timer and interrupt code. This is the code I currently have.

#define ledPin 13
void setup()

  • pinMode(ledPin, OUTPUT);*

  • // initialize timer1*

  • noInterrupts(); // disable all interrupts*

  • TCCR1A = 0;*

  • TCCR1B = 0;*

  • TCNT1 = 0;*

  • TCCR1B |= (1 << WGM12); // CTC mode*

  • TCCR1B |= (1 << CS00); // Set no prescaler*

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

  • interrupts(); // enable all interrupts*
    ISR(TIMER1_COMPA_vect) // timer compare interrupt service routine

  • digitalWrite(ledPin, digitalRead(ledPin) ^ 1); // toggle LED pin*
    void loop()

  • OCR1A = 20000; // Set the number of steps to compare to*

I was thinking if I could put the timer and interrupts into a function and have the input to the function be the pin that I want I could do this but I am struggling.

Any help is much appreciated!!

If all you are doing is writing to a pin, and you are going to do no other processing in the interrupt service routine, then you do not really need to use interrupts. You’d have to then be prepared to use specific pins dedicated to the timers. For Timer 1, these are OC1A and OC1B (D9 and D10 respectively).

You have also got another, simpler option, that is using the tone() library to generate your 400Hz square wave and you are free to specify the pins used.

However, assuming you wish to proceed with the code you have, you could do something like this:

// globals
volatile byte togglePin = -1 ;

. . .
. . .

loop() { 
   . . .
   . . .
   if ( x > y ) {
     togglePin = 12 ; 

   if (x < y) {
     togglePin = 13 ;

   if ( x == y)  {
     togglePin = -1 ;
. . .
. . .

. . .
. . .

ISR(TIMER1_COMPA_vect)          // timer compare interrupt service routine
  if ( togglePin != -1 ) digitalWrite(togglePin, digitalRead(togglePin) ^ 1);   // toggle xxx pin

Please enclose code in Code Tags </>

You can use the timer channels A and B for two separate outputs. Enable the Toggle On Compare Match for the active channel, and interrupts are required any more.

Find out later whether your code will also work with the stepper motor, details depend on your motor driver.

What I want to do is compare two values and depending on the result switch the pin the square wave is being outputted from.

Why? Do you want to switch the signal from stepperA to stepperB?

It is much easier to provide sensible advice if you describe the project.

Why not set up a 400Hz signal on the two different pins and then turn the output on or off as required.

A 400Hz signal could be generated using micros().

What stepper motor driver are you using?

Stepper Motor Basics
Simple Stepper Code