#include "LiquidCrystal_I2C.h"
#include <Wire.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
int count = 0;
int MAX = 8;
int lastState0 = 1;
int lastState1 = 1;
void setup() {
Serial.begin(115200);
Wire.begin();
lcd.init();
lcd.backlight();
pinMode(PA0, INPUT_PULLUP); // entrée
pinMode(PA1, INPUT_PULLUP); // sortie
pinMode(PA2, OUTPUT); // 🔴 rouge
pinMode(PA3, OUTPUT); // 🟢 verte
lcd.setCursor(0,0);
lcd.print("Parking System");
lcd.setCursor(0,1);
lcd.print("Ready...");
delay(1500);
}
void loop() {
int state0 = digitalRead(PA0);
int state1 = digitalRead(PA1);
// -------- Entrée --------
if(state0 == LOW && lastState0 == HIGH)
{
if(count < MAX) count++;
}
// -------- Sortie --------
if(state1 == LOW && lastState1 == HIGH)
{
if(count > 0) count--;
}
lastState0 = state0;
lastState1 = state1;
// -------- LCD --------
lcd.setCursor(0,0);
lcd.print("Voitures: ");
lcd.print(count);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("Places: ");
lcd.print(MAX - count);
lcd.print(" ");
// -------- LEDs --------
if(count == MAX)
{
lcd.setCursor(0,2);
lcd.print("PARKING FULL ");
digitalWrite(PA2, HIGH); // 🔴 rouge ON
digitalWrite(PA3, LOW); // 🟢 verte OFF
}
else
{
lcd.setCursor(0,2);
lcd.print(" ");
digitalWrite(PA2, LOW); // 🔴 rouge OFF
digitalWrite(PA3, HIGH); // 🟢 verte ON
}
delay(20); // ⚡ très rapide (réponse instantanée)
}Loading
st-nucleo-c031c6
st-nucleo-c031c6