Bonjour,
J'ai réalisé en partie la programmation d'une commande pour un Ampli-casque, je dis en partie car j'ai eu recours à ChatGPT pour trouver une commande approprié "activated", le code que j'utilisais pour la bascule de sortie:
if (nouvelEtatEntree1 != etatEntree1) {
etatEntree1 = nouvelEtatEntree1;
if (etatEntree1) {
etatSortie1 = !etatSortie1;
digitalWrite(SORTIE_1, etatSortie1);
// Activation de la temporisation
tempsActivationSorties = millis();
}
}
Ne me permettais pas de réaliser une tempo à l'allumage, tout du moins je n'ai pas trouvé la solution.
le système est composé de 3 boutons poussoir:
- POWER
- Sélecteur sortie (2 sorties)
- Sélecteur Entrée (2 entrées)
l'ARDUINO pilote 5 relais 5v par le biais de Mosfet.
Le programme doit:
- Le bouton POWER doit allumer/éteindre la sortie 12
- Lorsque POWER est "OFF" toute les sorties et entrées doivent être coupées "12, 11, 10, 7 & 2".
- Lorsque POWER est "ON" la sortie 12 est activée et 4 secondes plus tard la sortie 11 & 7 est activées.
- Le sélecteur de sortie bascule entre la sortie 11 et 10.
- Le sélecteur d'entrée bascule entre la sortie 7 et 2.
Tout fonctionne presque parfaitement avec le code suivant, sauf lorsque j'allume l'ampli, la tempo de 4 sec s'écoule, les 2 sorties s'activent, mais si je souhaite basculer de la sortie 11 à la sortie 10, il arrive qu'il me coupe les 2 sorties 11 & 10 et exécute une bascule en couplant les 2 simultanément, idem pour les sortie 7 & 2.
Quelqu'un aurait il une solution à me proposer ? ou pour me corriger d'une erreur ?
```cpp
// Amplificateur casque
// Constantes pour les broches d'entrée et de sortie
const int buttonPin = 5; // Bouton-poussoir en broche 5, Sélecteur de Entrée
const int buttonPin1 = 6; // Bouton-poussoir en broche 1, bouton ON/OFF
const int buttonPin2 = 4; // Bouton-poussoir en broche 1, Sélecteur Sortie
const int RPin1 = 11; // Relais en broche 11, bascule SCR 1
const int RPin2 = 10; // Relais en broche 10, bascule SRC 2
const int RPin3 = 7; // Relais en broche 7, CASQUE
const int RPin4 = 2; // Relais en broche 2, PREAMPLI
const int RPin6 = 12; // Relais POWER
// État initial des sorties
const bool initialOutputState = LOW;
// Variables pour suivre l'état des sorties
bool stateRPin1 = initialOutputState;
bool stateRPin2 = initialOutputState;
bool stateRPin3 = initialOutputState;
bool stateRPin4 = initialOutputState;
bool stateRPin6 = initialOutputState;
// Temporisation pour RPin1 en ms
const unsigned long RPin1_delay = 4000;
// Variable pour suivre l'état de RPin1 après l'appui sur buttonPin1
bool RPin1_activated = false;
bool RPin3_activated = false; // Variable pour suivre l'état de RPin3
unsigned long prevMillis = 0; // Variable pour stocker le temps précédent
const int R_delay = 500; // Temps de rebond en ms
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Sélecteur de sortie
pinMode(buttonPin1, INPUT_PULLUP); // Bouton ON/OFF
pinMode(buttonPin2, INPUT_PULLUP); // Bouton Sélecteur Sortie
pinMode(RPin1, OUTPUT); // Relais en broche 11, bascule SCR 1
pinMode(RPin2, OUTPUT); // Relais en broche 10, bascule SRC 2
pinMode(RPin3, OUTPUT); // Relais en broche 7, bascule CASQUE
pinMode(RPin4, OUTPUT); // Relais en broche 2, bascule PREAMPLI
pinMode(RPin6, OUTPUT); // Sortie Alimentation Générale
digitalWrite(RPin1, initialOutputState);
digitalWrite(RPin2, initialOutputState);
digitalWrite(RPin3, initialOutputState);
digitalWrite(RPin4, initialOutputState);
digitalWrite(RPin6, initialOutputState);
}
void loop() {
// Gestion de l'alimentation générale (buttonPin1)
if (digitalRead(buttonPin1) == LOW && millis() - prevMillis > R_delay) {
prevMillis = millis(); // Stocke le temps actuel
RPin1_activated = !RPin1_activated;
RPin3_activated = !RPin3_activated;
digitalWrite(RPin6, RPin1_activated); // Inverse l'état de RPin6 en fonction de RPin1_activated
if (!RPin1_activated) {
digitalWrite(RPin1, initialOutputState);
digitalWrite(RPin2, initialOutputState);
digitalWrite(RPin3, initialOutputState);
digitalWrite(RPin4, initialOutputState);
stateRPin1 = stateRPin2 = stateRPin6 = initialOutputState;
stateRPin3 = stateRPin4 = stateRPin6 = initialOutputState;
}
}
// Gestion sélecteur d'entrée (buttonPin)
if (digitalRead(RPin6) == HIGH) {
if (digitalRead(buttonPin) == LOW && millis() - prevMillis > R_delay) {
prevMillis = millis(); // Stocke le temps actuel
stateRPin1 = !stateRPin1;
stateRPin2 = !stateRPin2;
digitalWrite(RPin1, stateRPin1);
digitalWrite(RPin2, stateRPin2);
}
}
// Gestion sélecteur sortie (buttonPin2)
if (digitalRead(RPin6) == HIGH) {
if (digitalRead(buttonPin2) == LOW && millis() - prevMillis > R_delay) {
prevMillis = millis(); // Stocke le temps actuel
stateRPin3 = !stateRPin3;
stateRPin4 = !stateRPin4;
digitalWrite(RPin3, stateRPin3);
digitalWrite(RPin4, stateRPin4);
}
}
// Gestion du temporisateur pour RPin1 (entrée)
if (RPin1_activated && millis() - prevMillis >= RPin1_delay) {
if (!stateRPin2) { // Si RPin2 est LOW
stateRPin1 = !stateRPin1;
digitalWrite(RPin1, stateRPin1);
}
}
// Gestion du temporisateur pour RPin3 (sortie)
if (RPin3_activated && millis() - prevMillis >= RPin1_delay) {
if (!stateRPin4) { // Si RPin4 est LOW
stateRPin3 = !stateRPin3;
digitalWrite(RPin3, stateRPin3);
}
}
}