# Interrupts using a counter?

Hello

I am running a stepper motor and to step it, you need to send the signal high, short delay and then low - so you basically need to pulse.

I`m currently running with a delay:

``````loop:
write step pin HIGH;
delay (10);
write step pin LOW;
delay (10);
``````

sudo code, but you get the idea. High, delay, low … but the number of times it does this is critical as it dictates how many steps it will make. I`ve done a similar thing on a microcontroller with MikroC stuff and I (some how, I`m fairly new to this!) managed to get the above code running in an interrupt. That way I knew it was ALWAYS going to be running. Then I did an if statement within the interrupt to check wheather it should actually use the code and step motor:

That way I could guarantee that how ever big my code was and whatever else it was trying to do in the main section, it would always step the motor when I told it to as it was always jumping to the step/interrupt routine.

I`m really struggling to do this using Arduino. A simple one would be to just blink an LED using a count triggered interrupt rather than a delay.

Could anyone tell me if this is possible? How could I simply blink an LED say every second in an interrupt? Then the next step would be to blink the LED every 10ms (you obviously couldn`t see it!) using the interrupt.

Any help appreciated!
Chris

Simple enough.

You create an interrupt that runs at a known frequency - say every millisecond.

And yes, you just count up. When you hit certain numbers you do certain things - one of which can be re-setting your counter to 0.

You could have a couple of variables - one with the current "state" of the pulse in it (high or low), one with the current delay count, and one with the number of pulses to do.

Hi Majenko - have you any advice on how to physically setup this system and type of interrupt?

The current state out of the interrupt should be low, in the interrupt it will take the pin high and then low again, so always leaving on a low. The delay inside the interrupt can be very short indeed, infact, a few lines of code should delay it enough for the pulse from low-high-low. I`m happy to set the number of interrupts to do....

To be honest, I`m happy with all those variables and setting them up, I just can`t seem to setup the actual interrupt routine itself!

Any help appreciated on actually setting up a 10ms frequency interrupt.

Chris

I'd use the Timer1 library: http://arduino.cc/playground/Code/Timer1

attachInterrupt(function, period) Calls a function at the specified interval in microseconds. Be careful about trying to execute too complicated of an interrupt at too high of a frequency, or the CPU may never enter the main loop and your program will 'lock up'. Note that you can optionally set the period with this function if you include a value in microseconds as the last parameter when you call it.

I would go for the mstimer2 library. Here is an example that I implemented using mstimer2 http://blog.blinkenlight.net/experiments/basic-effects/persistence-of-vision/. And here is another one (even shorter): http://blog.blinkenlight.net/experiments/measurements/power-grid-monitor/.

This should give you an idea. Also the library documentation contains some examples. Get it here: http://arduino.cc/playground/Main/MsTimer2