#include <Wire.h> // Inclure la bibliothèque Wire pour la communication I2C
#include <LiquidCrystal_I2C.h> // Inclure la bibliothèque LiquidCrystal_I2C pour l'écran LCD I2C
// Adresse I2C de votre écran LCD 16x2 (peut varier selon votre module)
#define LCD_ADDRESS 0x27
// Définir un objet LiquidCrystal_I2C avec l'adresse I2C
LiquidCrystal_I2C lcd(LCD_ADDRESS, 16, 2); // 16 colonnes, 2 lignes
// Define pins for ultrasonic sensors
const int trigPins[] = {13, 11, 9}; // Trig pins for three sensors
const int echoPins[] = {12, 10, 8}; // Echo pins for three sensors
// Define pins for LEDs
const int ledPins[] = {5, 6, 7}; // LED pins for three sensors
// Status for each spot
bool spotStatus[] = {false, false, false};
void setup() {
// Initialize serial communication for debugging
Serial.begin(9600);
// Initialize ultrasonic sensor pins
for (int i = 0; i < 3; i++) {
pinMode(trigPins[i], OUTPUT);
pinMode(echoPins[i], INPUT);
}
// Initialize LED pins
for (int i = 0; i < 3; i++) {
pinMode(ledPins[i], OUTPUT);
}
// Initialize LCD
lcd.init(); // Initialize the LCD
lcd.backlight(); // Allumer le rétroéclairage
lcd.begin(16, 2); // Initialize the LCD with 16 columns and 2 rows
}
void loop() {
// Call function to check each sensor
for (int i = 0; i < 3; i++) {
checkSensor(trigPins[i], echoPins[i], ledPins[i], i);
}
delay(1000); // Delay to make readings more manageable
}
void checkSensor(int trigPin, int echoPin, int ledPin, int spotIndex) {
// Variables to store the duration and distance
long duration, distance;
// Clear the trigPin by setting it LOW
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Set the trigPin HIGH for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echoPin, and calculate the distance
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2; // Speed of sound is 34 cm/ms
// If the distance is greater than a certain threshold, the spot is available
if (distance > 200) { // Adjust this threshold based on your setup
digitalWrite(ledPin, HIGH); // Turn on LED to indicate availability
spotStatus[spotIndex] = true;
} else {
digitalWrite(ledPin, LOW); // Turn off LED to indicate occupancy
spotStatus[spotIndex] = false;
}
// Print debug information
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Update LCD display
updateLCD();
}
void updateLCD() {
lcd.clear(); // Clear the LCD
// Display spot status on LCD
lcd.setCursor(0, 0);
lcd.print("Spot1:");
lcd.print(spotStatus[0] ? "1" : "0");
lcd.setCursor(0, 1);
lcd.print("Spot2:");
lcd.print(spotStatus[1] ? "1" : "0");
lcd.setCursor(8, 0);
lcd.print("Spot3:");
lcd.print(spotStatus[2] ? "1" : "0");
}