// A framework for 4 leds with 4 buttons and a display
// 24 May 2024
// by Koepel, Public Domain
// Using the hd44780 library.
#include <Wire.h>
#include <hd44780.h> // common hd44780 include
#include <hd44780ioClass/hd44780_I2Cexp.h> // specific I2C lcd
hd44780_I2Cexp lcd;
// Order: R G B Y
const int ledPins[4] = {4, 5, 6, 7};
const int buttonPins[4] = {10, 9, 12, 11};
const int buzzerPin = 8;
const char *Label[4] = { "Red", "Green", "Blue", "Yellow"};
int oldButtonValue[4] = {false};
void setup()
{
Serial.begin(115200);
for(int i=0; i<4; i++)
{
pinMode(ledPins[i], OUTPUT);
pinMode(buttonPins[i], INPUT_PULLUP);
}
int error = lcd.begin(16, 2);
if(error != 0)
{
Serial.println("Display not found !");
}
// Show something on the display
lcd.print("Hello");
// Blink the leds
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
digitalWrite(ledPins[i], HIGH);
delay(150);
digitalWrite(ledPins[i], LOW);
delay(150);
}
}
// Make sound with the buzzer
for(int i=330; i<8000; i*=1.1)
{
tone(buzzerPin, i);
delay(40);
}
noTone(buzzerPin);
Serial.println("Press the buttons");
}
void loop()
{
bool button[4] = {false};
// Read the buttons
for(int i=0; i<4; i++)
{
int value = digitalRead(buttonPins[i]);
if(value == LOW)
button[i] = true;
else
button[i] = false;
}
for(int i=0; i<4; i++)
{
if(button[i] != oldButtonValue[i])
{
Serial.print("The ");
Serial.print(Label[i]);
Serial.print(" is ");
Serial.print( button[i] ? "pressed" : "released");
Serial.println();
oldButtonValue[i] = button[i];
}
}
delay(25); // also for debouncing the buttons
}