#include <Arduino.
h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Création de l'objet LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); // Adresse I2C 0x27, 16 colonnes et 2 lignes
// Définition des broches
const int led1 = 9; // LED 300ms
const int led2 = 10; // LED 2s
const int led3 = 11; // LED 5s
const int led4 = 12; // LED contrôlée par bouton (100ms)
const int bouton = 2; // Bouton poussoir
// États des LEDs
volatile bool led1State = false;
volatile bool led2State = false;
volatile bool led3State = false;
volatile bool led4State = false;
volatile bool boutonActif = false;
// Compteurs pour l'affichage LCD
volatile unsigned long lastLCDUpdate = 0;
const unsigned long LCD_UPDATE_INTERVAL = 500; // Mise à jour LCD toutes les 500ms
void setup() {
// Initialisation du LCD
lcd.init();
lcd.backlight();
lcd.clear();
// Message initial
lcd.setCursor(0, 0);
lcd.print("Etat des LEDs:");
// Configuration des broches
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(bouton, INPUT_PULLUP);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
// Désactiver les interruptions pendant la configuration
noInterrupts();
// Configuration du Timer1 pour LED1 (300ms)
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = 4687;
TCCR1B |= (1 << WGM12);
TCCR1B |= (1 << CS12) | (1 << CS10);
TIMSK1 |= (1 << OCIE1A);
// Configuration du Timer2 pour LED2 (2s)
TCCR2A = 0;
TCCR2B = 0;
TCNT2 = 0;
OCR2A = 249;
TCCR2A |= (1 << WGM21);
TCCR2B |= (1 << CS22) | (1 << CS21) | (1 << CS20);
TIMSK2 |= (1 << OCIE2A);
// Configuration du Timer0 pour LED3 (5s)
TCCR0A = 0;
TCCR0B = 0;
TCNT0 = 0;
OCR0A = 249;
TCCR0A |= (1 << WGM01);
TCCR0B |= (1 << CS02) | (1 << CS00);
TIMSK0 |= (1 << OCIE0A);
// Configuration de l'interruption du bouton
attachInterrupt(digitalPinToInterrupt(bouton), boutonChange, CHANGE);
// Réactiver les interruptions
interrupts();
}
void updateLCD() {
lcd.setCursor(0, 1);
// Affiche l'état de chaque LED (● pour allumée, ○ pour éteinte)
lcd.print(led1State ? "1:" : "1:");
lcd.print(led1State ? "\x01" : "\x00");
lcd.print(" ");
lcd.print(led2State ? "2:" : "2:");
lcd.print(led2State ? "\x01" : "\x00");
lcd.print(" ");
lcd.print(led3State ? "3:" : "3:");
lcd.print(led3State ? "\x01" : "\x00");
lcd.print(" ");
lcd.print(led4State ? "4:" : "4:");
lcd.print(led4State ? "\x01" : "\x00");
}
void loop() {
// Mise à jour périodique du LCD
if (millis() - lastLCDUpdate >= LCD_UPDATE_INTERVAL) {
updateLCD();
lastLCDUpdate = millis();
}
if (!boutonActif) {
digitalWrite(led4, LOW);
led4State = false;
}
}
// Interruption Timer1 pour LED1 (300ms)
ISR(TIMER1_COMPA_vect) {
led1State = !led1State;
digitalWrite(led1, led1State);
}
// Interruption Timer2 pour LED2 (2s)
volatile uint16_t timer2Count = 0;
ISR(TIMER2_COMPA_vect) {
timer2Count++;
if(timer2Count >= 122) {
led2State = !led2State;
digitalWrite(led2, led2State);
timer2Count = 0;
}
}
// Interruption Timer0 pour LED3 (5s)
volatile uint16_t timer0Count = 0;
ISR(TIMER0_COMPA_vect) {
timer0Count++;
if(timer0Count >= 305) {
led3State = !led3State;
digitalWrite(led3, led3State);
timer0Count = 0;
}
}
// Interruption pour LED4 (100ms) - Utilise Timer2
volatile uint16_t led4Counter = 0;
ISR(TIMER2_COMPA_vect) {
if (boutonActif) {
led4Counter++;
if (led4Counter >= 6) {
led4State = !led4State;
digitalWrite(led4, led4State);
led4Counter = 0;
}
}
}
// Interruption du bouton
void boutonChange() {
boutonActif = (digitalRead(bouton) == LOW);
if (!boutonActif) {
digitalWrite(led4, LOW);
}
}