/////////////////////////////////////////////////////////////////////
// Programme ARDUINO pour le pilotage de servomoteurs //
// en DCC ou Analogique //
// pour commander des portes d'un batiment //
// //
// Paramètrage via le port Série //
// //
// UAICF Nevers-Vauzelles //
// https://www.modelisme58.fr //
// //
// Septembre 2025 //
// Version 1.0 //
// //
// //
/////////////////////////////////////////////////////////////////////
//*** A modifier si besoin ****************************************************
#define DEBUG_Wokwi_Test
//#define DEBUG_Serie
#define DEBUG_ProgrammeDCC
//*** Fin des modifications ***************************************************
#include <NmraDcc.h> // DCC
#include <Servo.h> // Commande des Servomoteurs
#include <EEPROM.h> // Gestion de la mémoire
#include <Bounce2.h> // Gestion anti-rebond
#include "configuration.h"
#include "fonctions.h"
#include "dcc.h"
#include "portserie.h"
const char Date_Compilation[] = __DATE__;
void setup()
{
Serial.begin(9600);
Serial.println(F("*** UAICF Nevers-Vauzelles - www.modelisme58.fr"));
Serial.println(Date_Compilation);
Serial.println(F("Commande de portes avec 2 SERVOMOTEURS compatible DCC - V 1.0"));
Serial.print(F("Commande en Analogique, Port COM et "));
Centrale = EEPROM.read(EepromCentrale );
if (Centrale < 0 || Centrale > 1)
{
Centrale = 0;
EEPROM.update(EepromCentrale, Centrale);
}
switch (Centrale)
{
case 0:
Serial.println(F("Centrale DCC (Roco, NanoX,...)\n"));
break;
case 1:
Serial.println(F("Centrale DCC LENZ\n"));
break;
}
bouton1.attach(PIN_BOUTON1, INPUT_PULLUP);
bouton2.attach(PIN_BOUTON2, INPUT_PULLUP);
bouton3.attach(PIN_BOUTON3, INPUT_PULLUP);
bouton4.attach(PIN_BOUTON4, INPUT_PULLUP);
bouton1.interval(DEBOUNCE_INTERVAL);
bouton2.interval(DEBOUNCE_INTERVAL);
bouton3.interval(DEBOUNCE_INTERVAL);
bouton4.interval(DEBOUNCE_INTERVAL);
configurationParametres();
servo1.attach(PIN_SERVO1);
servo2.attach(PIN_SERVO2);
servo1State.positionActuelle = SERVO1_ANGLE_MIN;
servo1State.positionCible = SERVO1_ANGLE_MIN;
servo1State.enMouvement = false;
servo1State.dernierMouvement = 0;
servo2State.positionActuelle = SERVO2_ANGLE_MIN;
servo2State.positionCible = SERVO2_ANGLE_MIN;
servo2State.enMouvement = false;
servo2State.dernierMouvement = 0;
servo1.write(servo1State.positionActuelle);
servo2.write(servo2State.positionActuelle);
#ifdef DEBUG_Serie
Serial.println("*** Système initialisé ***");
Serial.println("Bouton 1 : Simultané vers position 1 depuis position 2");
Serial.println("Bouton 2 : Simultané vers position 2 depuis position 1");
Serial.println("Bouton 3 : Servo1 (pos2→pos1) puis servo2 (pos2→pos1)");
Serial.println("Bouton 4 : Servo2 (pos1→pos2) puis servo1 (pos1→pos2)\n");
#endif
}
void loop()
{
serialEvent();
bouton1.update();
bouton2.update();
bouton3.update();
bouton4.update();
if (bouton1.fell())
{
Serial.println("Bouton 1 pressé - Déplacement simultané vers position 1");
sequenceActive = AUCUN;
deplacerVersPosition(COMMANDE_SIMULTANEE_POS1);
}
if (bouton2.fell())
{
Serial.println("Bouton 2 pressé - Déplacement simultané vers position 2");
sequenceActive = AUCUN;
deplacerVersPosition(COMMANDE_SIMULTANEE_POS2);
}
if (bouton3.fell())
{
Serial.println("Bouton 3 pressé - Servo1 puis Servo2 vers position 1");
sequenceActive = SEQUENCE_BOUTON3;
deplacerVersPosition(COMMANDE_SEQUENTIELLE_VERS_POS1);
}
if (bouton4.fell())
{
Serial.println("Bouton 4 pressé - Servo2 puis Servo1 vers position 2");
sequenceActive = SEQUENCE_BOUTON4;
deplacerVersPosition(COMMANDE_SEQUENTIELLE_VERS_POS2);
}
mettreAJourMouvements();
}