Kahov
Kahov

Reputation: 21

How can I create interrupts in C for Arduino?

So I did this code for Arduino in C. It is for controlling a stepper motor. But every time I have to wait until the microcontroller begins a new loop so that it can take the value for the new variables, how can I create an interrupt so it will do it in any time of the program?

#include <avr/io.h>
#define F_CPU 4000000UL
#include <util/delay.h>

#include "IO/ioconfig.h"
#include "leebotones/leebotonesA.h"
#include "leebotones/leebotonesB.h"
#include "rutina/avanza.h"
#include "rutina/retrocede.h"

char variableA = 0;
char variableB = 0;

int main(void){
    ioconfig();
    while(1) {
        if (leebotonesA()==1) {
            variableA++;
        } //Fin de if leebotonesA.

        if (leebotonesB()==1) {
            if (variableA==0) {
                variableB=1;
            }
            else {
                variableB=0;
            }
        }

        if (variableA==2) {
            variableA=0;
            PORTD=0x00;
            _delay_ms(10000);
        } //Fin de if variableA.

        if (variableA==1 && variableB==0) {
            avanza();
        } //Fin de if leebotonesA.

        if (variableA==1 && variableB==1) {
            retrocede();
        }
        _delay_ms(25);
    }//End of while
}// End of main

Upvotes: 2

Views: 4510

Answers (2)

ViennaMike
ViennaMike

Reputation: 2337

MSTimer2 is an Arduino library function to let you set timer interrupts.

Another alternative to delay, rather than interrupts, is to set a timer and check it each time through the loop. http://arduino.cc/en/Tutorial/BlinkWithoutDelay explains the concepts. The Metro library http://arduino.cc/playground/Code/Metro implements this and is easy to use. I've used it instead of delay() so that I can check for a button push while my robot is moving.

Upvotes: 0

baalexander
baalexander

Reputation: 2589

A hardware interrupt on the Arduino occurs when one of the interrupt pins receives a change of state. The function to use, if you have access to the Arduino library, is attachInterrupt.

Example code to listen for an interrupt (derived from the documentation I linked to, I added comments to help explain):

// The Arduino has an LED configured at pin 13
int pin = 13;
// Holds the current state of the LED to handle toggling
volatile int state = LOW;

void setup()
{
  pinMode(pin, OUTPUT);
  // First Parameter:
  // 0 references the interrupt number. On the Duemilanove, interrupt 0
  // corresponds to digital pin 2 and interrupt 1 corresponds to digital pin
  // 3. There are only two interrupt pins for the Duemilanove and I believe
  // the Uno too.
  // Second Parameter:
  // blink is the name of the function to call when an interrupt is detected
  // Third Parameter:
  // CHANGE is the event that occurs on that pin. CHANGE implies the pin
  // changed values. There is also LOW, RISING, and FALLING.
  attachInterrupt(0, blink, CHANGE);
}

void loop()
{
  // Turns the LED on or off depending on the state
  digitalWrite(pin, state);
}

void blink()
{
  // Toggles the state
  state = !state;
}

There is also a concept of Pin Change Interrupts that is supported on every pin. See the bottom part of introduction to interrupts for more info.

However, sometimes a hardware interrupt can be avoided by refactoring your code. For example, keep your loop() running quickly --- mostly just reading inputs, limit the use of delay() --- and in the loop, call a function when the targeted inputted value is detected.

Upvotes: 3

Related Questions