#include <Arduino.
h>
// Définition des broches
const int led1 = 9; // LED rapide (300ms)
const int led2 = 10; // LED moyenne (2s)
const int led3 = 11; // LED lente (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;
// État du bouton
volatile bool boutonActif = false;
void setup() {
// Configuration des broches
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(bouton, INPUT_PULLUP); // Active la résistance de pull-up interne
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; // 300ms
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 loop() {
// La gestion des LEDs automatiques est faite dans les interruptions
// Pour LED4, nous vérifions l'état du bouton dans la boucle principale
if (!boutonActif) {
digitalWrite(led4, LOW); // Éteindre LED4 si le bouton n'est pas pressé
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 pour le timing
volatile uint16_t led4Counter = 0;
ISR(TIMER2_COMPA_vect) {
if (boutonActif) {
led4Counter++;
if (led4Counter >= 6) { // ~100ms (ajusté pour le Timer2)
led4State = !led4State;
digitalWrite(led4, led4State);
led4Counter = 0;
}
}
}
// Interruption du bouton
void boutonChange() {
// Lecture de l'état du bouton (LOW quand pressé car INPUT_PULLUP)
boutonActif = (digitalRead(bouton) == LOW);
if (!boutonActif) {
digitalWrite(led4, LOW); // Éteindre immédiatement la LED quand on relâche le
bouton
}
}