Arduino interrupt example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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