#include <CD74HC4067.h>
CD74HC4067 My_mux(4,5,18,19);
// Définition des broches de sélection (S0 à S3)
const int SELECT_PINS[] = {4, 5, 18, 19};
const int SIG_PIN = 27; // Broche de signal COM (GPIO 27)
// Canaux MUX utilisés
const int BLUE_LED_CH = 0;
const int GREEN_LED_CH = 1;
const int RED_LED_CH = 2;
const int BUTTON_CH = 3;
const int POT_CH = 4;
// Seuil du Potentiomètre (0-4095)
const int SEUIL_POT = 2048;
// État de l'application
volatile bool isRunning = false;
volatile bool buttonState = HIGH; // État non-appuyé est HIGH avec INPUT_PULLUP
// ----------------------------------------------------
// Fonction pour sélectionner le canal sur le MUX
// ----------------------------------------------------
void setMuxChannel(int channel) {
for (int i = 0; i < 4; i++) {
digitalWrite(SELECT_PINS[i], bitRead(channel, i) ? HIGH : LOW);
}
}
// ----------------------------------------------------
// Fonctions de lecture/écriture du MUX
// ----------------------------------------------------
int readMuxDigital(int channel) {
setMuxChannel(channel);
delayMicroseconds(5);
return digitalRead(SIG_PIN);
}
int readMuxAnalog(int channel) {
setMuxChannel(channel);
delayMicroseconds(5);
return analogRead(SIG_PIN);
}
// Logique Low-Side : HIGH désiré -> LOW sur Mux; LOW désiré -> HIGH sur Mux
void writeMuxDigital(int channel, int desiredState) {
setMuxChannel(channel);
delayMicroseconds(5);
digitalWrite(SIG_PIN, desiredState == HIGH ? LOW : HIGH);
}
// ----------------------------------------------------
// Configuration
// ----------------------------------------------------
void setup() {
Serial.begin(115200);
for (int pin : SELECT_PINS) {
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW); // Assure un état initial connu (Canal 0)
}
// Le SIG_PIN doit être initialisé en OUTPUT pour éteindre les LEDs
pinMode(SIG_PIN, OUTPUT);
// Éteindre toutes les LEDs au démarrage (HIGH sur le Mux pour Low-Side)
writeMuxDigital(BLUE_LED_CH, LOW);
writeMuxDigital(GREEN_LED_CH, LOW);
writeMuxDigital(RED_LED_CH, LOW);
Serial.println("Systeme pret. LEDs eteintes. Appuyer sur le bouton pour demarrer.");
}
// ----------------------------------------------------
// Boucle principale (Logique finale du programme)
// ----------------------------------------------------
void loop() {
// 1. GESTION DU BOUTON
// ---------------------------------------------------
// Préparer le SIG_PIN en mode INPUT_PULLUP pour lire le bouton
pinMode(SIG_PIN, INPUT_PULLUP);
int currentButtonState = readMuxDigital(BUTTON_CH);
// Détection de Flanc Descendant (appui sur le bouton: HIGH -> LOW)
if (currentButtonState == LOW && buttonState == HIGH) {
isRunning = !isRunning;
// Gérer le changement d'état (Nécessite OUTPUT)
pinMode(SIG_PIN, OUTPUT);
// Allumer/Éteindre la LED Bleue en fonction de l'état
writeMuxDigital(BLUE_LED_CH, isRunning ? HIGH : LOW);
// Éteindre les autres LEDs si l'on s'arrête
if (!isRunning) {
writeMuxDigital(GREEN_LED_CH, LOW);
writeMuxDigital(RED_LED_CH, LOW);
}
Serial.print("Statut: ");
Serial.println(isRunning ? "DEMARRE" : "ARRETE");
delay(200);
}
buttonState = currentButtonState;
// 2. LOGIQUE DE CONTRÔLE (Seulement si Démarré)
// ---------------------------------------------------
if (isRunning) {
// A. Lecture du Potentiomètre (Nécessite INPUT)
pinMode(SIG_PIN, INPUT);
int potValue = readMuxAnalog(POT_CH);
// B. Contrôle des LEDs Rouge/Verte (Nécessite OUTPUT)
pinMode(SIG_PIN, OUTPUT);
if (potValue < SEUIL_POT) {
writeMuxDigital(GREEN_LED_CH, HIGH);
writeMuxDigital(RED_LED_CH, LOW);
} else {
writeMuxDigital(GREEN_LED_CH, LOW);
writeMuxDigital(RED_LED_CH, HIGH);
}
Serial.print("Potentiomètre: ");
Serial.println(potValue);
}
delay(10);
}Loading
cd74hc4067
cd74hc4067