Pages: [1]   Go Down
Author Topic: Interrupts using a counter?  (Read 482 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
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
Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Quote
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.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

0
Offline Offline
Faraday Member
**
Karma: 23
Posts: 3469
20 LEDs are enough
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Check out my experiments http://blog.blinkenlight.net

Pages: [1]   Go Up
Jump to: