Skip to content

Instantly share code, notes, and snippets.

@crmne
Created December 6, 2020 13:45
Show Gist options
  • Save crmne/08e545d656112b44113bbab928cf3e0b to your computer and use it in GitHub Desktop.
Save crmne/08e545d656112b44113bbab928cf3e0b to your computer and use it in GitHub Desktop.
An Arduino Based occupancy tracker with 2 buttons and a display
#include <LiquidCrystal.h>
#define MAX_SEATS 19
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
const int redButton = 7, greenButton = 6;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int n_occupied = 0;
byte redButtonState = 0, greenButtonState = 0, prevGreenButtonState = 0, prevRedButtonState = 0;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup() {
pinMode(redButton, INPUT);
pinMode(greenButton, INPUT);
display_occupancy(n_occupied);
}
void loop() {
int greenButtonReading = digitalRead(greenButton);
int redButtonReading = digitalRead(redButton);
if (greenButtonReading != prevGreenButtonState || redButtonReading != prevRedButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (greenButtonReading != greenButtonState || redButtonReading != redButtonState) {
greenButtonState = greenButtonReading;
redButtonState = redButtonReading;
if (greenButtonState == HIGH) {
if (n_occupied == MAX_SEATS) {
display_strings("Sorry! No seats", "available now");
delay(3000);
display_occupancy(n_occupied);
} else {
n_occupied++;
display_occupancy(n_occupied);
}
}
if (redButtonState == HIGH) {
if (n_occupied == 0) {
display_strings("O_o a ghost o_O", "Bye ghost!");
delay(3000);
display_occupancy(n_occupied);
} else {
n_occupied--;
display_occupancy(n_occupied);
}
}
}
}
prevGreenButtonState = greenButtonReading;
prevRedButtonState = redButtonReading;
}
void display_occupancy(int n_occupied) {
int n_available = MAX_SEATS - n_occupied;
String firstLine = "Occupied: " + String(n_occupied);
String secondLine = "Available: " + String(n_available);
display_strings(firstLine, secondLine);
}
void display_strings(String firstLine, String secondLine) {
lcd.begin(16, 2);
lcd.print(firstLine);
lcd.setCursor(0, 1);
lcd.print(secondLine);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment