Class Notes, 25 Jan 2022

Class 3: 25 Jan – Intro to Interrupts

Office hours start next week on campus in A10 at 5pm.

Review assignments

Design for Dreaming — good?  Bad?  Socially uncomfortable?

revisit tigoe’s page — what was physical interaction and what was reaction?  Do you see any themes in projects that aren’t allowed?   Is there something you’re tired of seeing?

I like theremins and have two.  Making them is easy.  Playing them is damned difficult.

Dorit Chrysler playing The Swan:  https://www.youtube.com/watch?v=AS9LJK8i1VE

Clara Rockmore, also playing The Swan:  https://www.youtube.com/watch?v=XdFSU8sn3mo

Theremin playing a theremin:  https://www.youtube.com/watch?v=w5qf9O6c20o

Interrupts

We use interrupts in daily life:

  • Asking a question in class
  • Phone rings (skeumorphic!)
  • Traffic light

What can we do during an interruption?  What do we do at a traffic light? During a lesson?

What happens when multiple people raise their hands in class?  What happens if the instructor is servicing a request and someone else raises their hand?  Stop answering the first person?  Delay the second person?

Interrupt request vs interrupt service.

Raising your hand is an interrupt request.  The instructor responding is servicing the interrupt request.

Explain how interrupts work  in software/hardware

  • clock has to stop, so millis(), Serial, and other I/O functions won’t work including reading/writing to pins
  • difficult to debug anything more complicated than changing a state variable
  • really useful for state machines because you don’t have to call digitalRead() or analogRead()
  • delay() timing is paused

What if you don’t want an interrupt to happen?  This is probably a bad thing, as a lot of what makes the Arduino work is interrupts you can’t see happen:

noInterrupts() — disable interrupts, like putting your phone in “Do Not Disturb””

interrupts() — turns interrupts back on

Interrupts can be hard to debug on Arduino.  No access to the Serial console, difficult to do anything other than simple math/logic operations.

Compilation hides errors by not failing.  Say you have this code for an interrupt handler:

void switchLed() {

ledOn++;

if (ledOn > led4) {

ledOn = led1;

}

}

Both of these compile without a warning/error:

// good

attachInterrupt(digitalPinToInterrupt(toggle_color), switchLed, RISING);

// bad, there will be no interrupt service — can you see why?

attachInterrupt(digitalPinToInterrupt(toggle_color), switchLeds, RISING);

 

Arduino models can support interrupts, but often on different pins:

https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/

Nick Gammon has an intensive, CS-focused page on interrupts:  https://gammon.com.au/interrupts

The simple example I showed in class:

int led1 = 8;
int led2 = 9;
int led3 = 10;
int led4 = 11;

int ledOn = 0;


volatile int toggle_color = 2;
volatile int toggle_speed = 3;

int ledDelay = 250;

void setup() {

    pinMode(led1, OUTPUT);
    pinMode(led2, OUTPUT);
    pinMode(led3, OUTPUT);
    pinMode(led4, OUTPUT);

    pinMode(toggle_color, INPUT);
    pinMode(toggle_speed, INPUT);
    attachInterrupt(digitalPinToInterrupt(toggle_color), switchLed, RISING);
    attachInterrupt(digitalPinToInterrupt(toggle_speed), changeSpeed, RISING);

    ledOn = led1;
    
    Serial.begin(9600);
}

void switchLed() {
    ledOn++;
    if (ledOn > led4) {
        ledOn = led1;
    }
}

void changeSpeed() {
    ledDelay = ledDelay * 2;
    if (ledDelay > 2000) {
        ledDelay = 250;
    }
   
}

void loop() {

    digitalWrite(ledOn, HIGH);
    delay(ledDelay);
    digitalWrite(ledOn, LOW);
    delay(ledDelay);
}

 

Assignment

Find some examples of interrupts in your daily life, complex and simple, post to Looking Outward.

Reading: Make It So Forward, Chap 1, Chap 2.  How did we think about the future in the past?  Post a few notes to the blog (Assignment 2) and we’ll talk about these at the start of class.

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.