Skip to content

Instantly share code, notes, and snippets.

@lvidarte
Last active March 28, 2016 02:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lvidarte/d60c6d69497038dae74a to your computer and use it in GitHub Desktop.
Save lvidarte/d60c6d69497038dae74a to your computer and use it in GitHub Desktop.
Arduino interrupt example
#include <stdio.h>
#include <LiquidCrystal.h>
#define BUTTON_PIN 2
#define LCD_LINE_LENGTH 17
volatile int state = 1;
static char buffer0[LCD_LINE_LENGTH];
static char buffer1[LCD_LINE_LENGTH];
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
void lcdPrintLine(char *buffer, byte lineNumber)
{
lcd.setCursor(0, lineNumber);
byte i = 0;
while (i < LCD_LINE_LENGTH && buffer[i] != '\0')
{
lcd.write(buffer[i]);
i++;
}
}
void interrupt()
{
state = digitalRead(BUTTON_PIN);
sprintf(buffer1, (state ? "continue..." : "interrupt!!"));
lcdPrintLine(buffer1, 1);
}
void setup()
{
lcd.begin(16, 2);
pinMode(BUTTON_PIN, INPUT_PULLUP);
attachInterrupt(0, interrupt, CHANGE);
}
void loop()
{
lcd.clear();
for (int i = 0; i < 25; i++) {
while( ! state);
sprintf(buffer0, "n = %d", i);
lcdPrintLine(buffer0, 0);
delay(500);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment