Arduino Interrupts

http://www.allaboutcircuits.com/technical-articles/using-interrupts-on-arduino/

 

https://www.arduino.cc/en/Reference/attachInterrupt

http://www.bristolwatch.com/arduino/arduino_irq.htm

 

 

 

 

Board Digital Pins Usable For Interrupts
Uno, Nano, Mini, other 328-based 2, 3
Mega, Mega2560, MegaADK 2, 3, 18, 19, 20, 21
Micro, Leonardo, other 32u4-based 0, 1, 2, 3, 7
Zero all digital pins, except 4
MKR1000 Rev.1 0, 1, 4, 5, 6, 7, 8, 9, A1, A2
Due all digital pins
101 all digital pins

 

Syntax

attachInterrupt(digitalPinToInterrupt(pin), ISR, mode); (recommended)
attachInterrupt(interrupt, ISR, mode); (not recommended)
attachInterrupt(pin, ISR, mode) ; (not recommended Arduino Due, Zero,MKR1000, 101 only)

Parameters

interrupt: the number of the interrupt (int)
pin: the pin number (Arduino Due, Zero, MKR1000 only)
ISR: the ISR to call when the interrupt occurs; this function must take no parameters and return nothing. This function is sometimes referred to as aninterrupt service routine.
mode: defines when the interrupt should be triggered. Four constants are predefined as valid values:

  • LOW to trigger the interrupt whenever the pin is low,
  • CHANGE to trigger the interrupt whenever the pin changes value
  • RISING to trigger when the pin goes from low to high,
  • FALLING for when the pin goes from high to low.
The Due board allows also:
  • HIGH to trigger the interrupt whenever the pin is high.
(Arduino Due, Zero, MKR1000 only)

Returns

none

Example

const byte ledPin = 13;
const byte interruptPin = 2;
volatile byte state = LOW;void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}void loop() {
  digitalWrite(ledPin, state);
}void blink() {
  state = !state;
}
—————————————————

SULAMA INTERRUPT

3lü toggle switch var, on-off-on
#define DirekSulaSwitch 12 // calisiyor mavi
#define SensorSulaSwitch 13 // calismiyor beyaz
Bunlar 12 ya da 13 e değiştiğinde interrupt çalışacak.
attachInterrupt(digitalPinToInterrupt(pin), ISR, CHANGE);
Mega, Mega2560, MegaADK 2, 3, 18, 19, 20, 21

CURRENT:

/////////////SWITCH
#define DirekSulaSwitch 12 // calisiyor mavi
#define SensorSulaSwitch 13 // calismiyor beyaz
///////////////////SWITCH////////////////////////////
void setup() { ///————————————————-SETUP

////////////////////SWITCH
pinMode(DirekSulaSwitch, INPUT); // calisiyor
pinMode(SensorSulaSwitch, INPUT);
////////////////////////SWITCH////////////////////////////////////

///——————————-SETUP———————–
void loop() {///——————————LOOP

} ///—————————–LOOP—–

void DirekSulama()
{
if (digitalRead(DirekSulaSwitch) == 1)
{
digitalWrite(RELAY1_ANA_SULAMA_POMPA, RELAY_CALIS);
}
else if (digitalRead(DirekSulaSwitch) == 0)
{
digitalWrite(RELAY1_ANA_SULAMA_POMPA, RELAY_DUR);
}
}

————————————————————————-

const byte interruptPin = 18;
volatile byte state = LOW;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}void loop() {
  digitalWrite(ledPin, state);
}void blink() {
state = !state;
}
 —————————————————————————–

SULAMA INTERRUPT MINE

/////////////SWITCH
#define DirekSulaSwitch 18// calisiyor mavi
#define SensorSulaSwitch 19 // calismiyor beyaz

volatile byte state = LOW;
///////////////////SWITCH////////////////////////////

void setup() { ///----------------------------------SETUP

////////////////////SWITCH
pinMode(DirekSulaSwitch, INPUT); // calisiyor
pinMode(SensorSulaSwitch, INPUT);
// Attach an interrupt to that pin which corresponds to interrupt 1
// It will trigger when the input signals is FALLING
//can also use instead of 5: digitalPinToInterrupt(18)
attachInterrupt(5, DirekSulama, CHANGE); 
  // interrupt 5 digital pin 18  connected SW0
  attachInterrupt(1, ISR1, CHANGE); 
  // interrupt 4  digital pin 19 connected SW1

/////////////SWITCH////////////////////////

///------------------SETUP-----------------

void loop() {///-------------------LOOP

} ///--------------LOOP-----

void DirekSulama()
{
if (digitalRead(DirekSulaSwitch) == 1)
{
digitalWrite(RELAY1_ANA_SULAMA_POMPA, RELAY_CALIS);
}
else if (digitalRead(DirekSulaSwitch) == 0)
{
digitalWrite(RELAY1_ANA_SULAMA_POMPA, RELAY_DUR);
}
}

 

----------------------------------------------

 

packt-publishing-arduino-development-cookbook-2015

///
Code
The following code will fade one external LED. When we press the button, it will change
which LED is fading:

// Include the required Wire library for I2C

int LED1 = 5;
int LED2 = 6;

// Set a variable which we can change in the interrupt function

volatile int currentLED = LED1;
void setup()
{
// Set the button pin as an input with PULL UP resistor
pinMode(3, INPUT_PULLUP);
// Attach an interrupt to that pin which corresponds to interrupt 1
// It will trigger when the input signals is FALLING
attachInterrupt(1, changeLED, FALLING);
}
// Function that is being triggered by the interrupt
void changeLED(){
if (currentLED == LED1) currentLED = LED2;
else currentLED = LED1;
}
void loop(){
// Fade In
for (int i = 0; i < 256; i++){
analogWrite(currentLED, i);
delay(10);
}
// Fade In
for (int i = 255; i > 0; i--){
analogWrite(currentLED, i);
delay(10);
}
}---------------------------------------------------------

 

How it works…
When a change is detected on an interrupt pin, the code is paused, the specified function is
run, and the code execution is resumed. The change can be a falling or rising edge, and a
few more things. Take a look at the There’s more… section of this recipe.
Let’s look in the code breakdown.
Code breakdown
The first important difference we see is the volatile variable type:
volatile int currentLED = LED1;
The volatile variable type is a directive to the compiler. It tells to store the variable in
easily accessible RAM as it will be accessed during execution. Any variable we change
during an interrupt-attached function has to be volatile, otherwise weird things will
happen.
In the setup() function, we attach a function to interrupt 1. We call the changeLED()
function and it will trigger when there is a falling edge in the signal on the interrupt pin.
For more about trigger types, take a look at the There’s more… section of this recipe.
attachInterrupt(1, changeLED, FALLING);
Now let’s explore the changeLED function. Any function attached to an interrupt cannot
return anything (it has to be a void function) and cannot have any arguments. Any
variable modified inside an attached function has to be volatile.
void changeLED(){
if (currentLED == LED1) currentLED = LED2;
else currentLED = LED1;
}
In this function, we invert the current value of the currentLED variable. If it’s LED1 it
becomes LED2 and so on. This function can execute at any time a FALLING edge is detected
on the interrupt pin.
There’s more…
Here we will look a little more deeply into the different things interrupts can do.
Interrupts on various Arduinos
Different Arduinos have a different amount of external interrupts. Remember, in the
attachInterrupt() function, the first parameter is the number of the interrupt, not the
digital pin on which it can be found. The following table is a good reference:

Board Interrupt 0 Interrupt 1 Interrupt 2 Interrupt 3 Interrupt 4 Interrupt 5
Uno, Pro Mini 2 3
Mega2560 2 3 21 20 19 18
Leonardo Yún 3 2 0 1 7

On the Yún, pins 0 and 1 are also used for serial communication, so try to avoid them.
The Arduino Due, however, is an exception. We can assign an interrupt to any digital pin.
In the attachInterrupt function, the first argument will actually be digitalPin we want
to use.
About interrupt-attached functions
Remember that the code pauses when an interrupt is detected and our function is executed.
Because of timer interference, the delay() function will not work. Also, characters
received by the serial during the execution of our interrupt function might be dropped.
However, the delayMicrosecond() function should work just fine.
In general, functions triggered by interrupt should be as short as possible with minimum
impact and execution time. Otherwise, they might just do very strange things.
Different triggering modes
The last argument of the attachInterrupt() function is the triggering mode. There are
four different modes with a fifth custom mode for the Arduino Due:
LOW: This triggers the function whenever the value on the interrupt pin is LOW. If
the function finishes execution and the pin is still LOW, it will execute again and
again until the pin is HIGH.
CHANGE: This triggers the function whenever a change is detected on the interrupt
pin. Change means that either the pin switches from LOW to HIGH or from HIGH to
LOW.
RISING: This will trigger the function whenever the signal on the interrupt pin
changes from LOW to HIGH.
FALLING: This will trigger the function whenever the signal on the interrupt pin
changes from HIGH to LOW.
HIGH: This is a Due exclusive and works exactly like LOW mode, except that it
triggers when the signal is HIGH.
Detaching an interrupt
There is also a function that detaches interrupts:
detachInterrupt(interrupt);
It requires the interrupt number as the argument. On the Due, it requires the pin number to
which the interrupt was attached.
Once the detachInterrupt() function is used, a different function can be attached using
the attachInterrupt() function.

Leave a Reply