interrupt/counter

Hi LandonW,

You are using a Teensy 3.2, which uses a Cortex M4 processor, so the way you set and handle interrupts may be different. None-the-less, here is an example of how to use an interrupt. This example expects an Arduino Uno, but you can try it on your board.

First connect a switch between pin 2 and ground. Hook your board to a serial port on your computer, and load the following sketch:

const byte inputPin = 2; // <-- switch <-- gnd
const byte outputPin = 13;  // --> builtin LED

volatile boolean inputState = false;
boolean lastInputState = false;

long count = 0L;

void setInputState() {
  inputState = digitalRead(inputPin);
}

void setup() {
  pinMode(inputPin, INPUT_PULLUP);
  pinMode(outputPin, OUTPUT);

  Serial.begin(115200);

  attachInterrupt(digitalPinToInterrupt(inputPin), setInputState, CHANGE);
}

void loop() {
  if (inputState != lastInputState) {
    digitalWrite(outputPin, inputState);
    lastInputState = inputState;

    Serial.println(count++);
  }
}

The built-in LED will turn on and off according to the switch, and the total number of times the switch was thrown will be shown on the serial monitor. The only thing the ISR does is set a variable high or low, depending on the state of the input pin. Everything else happens in the loop() function.