Arduino Tutorial

By LiterallyTheOne

6: Interrupt

Arduino Tutorial, Interrupt

Introduction

  • Previous tutorial: Analog
  • This tutorial: Interrupt
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Interrupt

External Interrupt

  • Special Signal
  • Pause the current action
  • Run a code in a function
  • Interrupt Service Routine Function (ISR Function)
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Interrupt

External Interrupt in Arduino Uno

  • 2 External Interrupts
  • pin 2, 3
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Interrupt

Setup LEDs

setup leds

By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Interrupt

Write a Routine

#include <Arduino.h>

int led_pins[8] = {6, 7, 8, 9, 10, 11, 12, 13};

int current_led = 0;

void setup()
{
  for (int i = 0; i < 8; i++)
  {
    pinMode(led_pins[i], OUTPUT);
  }
}

void loop()
{
  digitalWrite(led_pins[current_led], HIGH);
  delay(200);
  digitalWrite(led_pins[current_led], LOW);

  current_led++;
  current_led %= 8;
}
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Interrupt

Output of LED setup

LED stup gif

By Ramin Zarebidoky (LiterallyTheOne)
LED pause
Arduino Tutorial, Interrupt

Connect an Interrupt

  • Button is Normally Closed
  • Default: 5V
  • Change: 0V
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Interrupt

Define Interrupt in Arduino

  • attachInterrupt
    • pin
    • ISR function
    • mode
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Interrupt

Define Interrupt pin

  • digitalPinToInterrupt
attachInterrupt(digitalPinToInterrupt(2), ... ,...);
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Interrupt

Define ISR function

void isr_pause()
{
}
attachInterrupt(digitalPinToInterrupt(2), isr_pause ,...);
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Interrupt

Modes

Mode Description figure
LOW trigger the interrupt whenever the pin is low. interrupt-stage-low
CHANGE trigger the interrupt whenever the pin changes value. interrupt-stage-change
RISING trigger when the pin goes from low to high. interrupt-stage-rise
FALLING trigger when the pin goes from high to low. interrupt-stage-fall
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Interrupt

Add mode

attachInterrupt(digitalPinToInterrupt(2), isr_pause, RISING);
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Interrupt

Fill the isr function

#include <Arduino.h>

int led_pins[8] = {6, 7, 8, 9, 10, 11, 12, 13};

int current_led = 0;
int x = 1;

void isr_pause()
{
  x = 1 - x;
}

void setup()
{
  for (int i = 0; i < 8; i++)
  {
    pinMode(led_pins[i], OUTPUT);
  }

  attachInterrupt(digitalPinToInterrupt(2), isr_pause, RISING);
}

void loop()
{
  digitalWrite(led_pins[current_led], HIGH);
  delay(200);
  digitalWrite(led_pins[current_led], LOW);

  current_led += x;
  current_led %= 8;
}
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Interrupt

Output of pause interrupt

pause interrupt

By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Interrupt

Volatile in Cpp

  • Stops Compiler optimazation for a variable
volatile int v;
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Interrupt

Reset with Interrupt

Interrupt reset

By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Interrupt

Increment pattern

Incerement pattern

By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Interrupt

LEDS dance

LEDs dance

By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Interrupt

By Ramin Zarebidoky (LiterallyTheOne)