Skip to content

Instantly share code, notes, and snippets.

@lvidarte lvidarte/sketch.ino
Last active Mar 28, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.