1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
| #define bpNombreMax 8 // Nombre de boutons
#define bpMotNombreMax 4 // Nombre de moteurs max par bouton
#define bpEtatPresse LOW // Etat lu du bouton presse
#define motAigNombreMax 17 // Nombre de moteurs d'aiguilles
#define motEtatOn HIGH // Etat pour moteur ON
#define motEtatOnTemps 50 // Moteur, temps de l'impulsiom ON
//------------------------------------- Structure de l'objet moteur/aiguiulle (motAig
struct motAigDefinition
{String nom[motAigNombreMax]; byte port[motAigNombreMax];};
/*
Enumeration des aiguilles pour faciliter leur appel
leur nombre = motAigNombreMax
*/
const byte maNU = -1; // Pour position non utilisee
enum motAigIndex
{ maG1_D, maA1_G, maA1_D, maA2_G, maA2_D, maA3_G, maA3_D, maA4_G, maA4_D, maB1_G, maB1_D, maB2_G, maB2_D, maB3_G, maB3_D, maB4_G, maB4_D};
/*
remplissage de la structure des moteurs/aiguilles motAig[0] a bp[motAigNombreMax-1]
dans l'ordre motAig[n].nom, motAig[n].port
*/
motAigDefinition motAig = {
{"G1 D", "A1 G", "A1 D", "A2 G", "A2 D", "A3 G", "A3 D", "A4 G", "A4 D", "B1 G", "B1 D", "B2 G", "B2 D", "B3 G", "B3 D", "B4 G", "B4 D"},
{ 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38}
};
//------------------------------------- Structure de l'objet bouton poussoir (bp)
struct bpDefinition
{String nom; byte port; byte motPort[bpMotNombreMax];};
/*
remplissage de la structure des boutons bp[0] a bp[bpNombreMax-1]
dans l'ordre bp[n].nom, bp[n].port, bp[n].moteurs[0] a bp[n].moteurs[motNombreMax-1]
*/
bpDefinition bp[] = {
{"C1_V1", 5, maG1_D, maA1_G, maA3_D, maNU},
{"C1_V2", 6, maG1_D, maA1_D, maA4_D, maNU},
{"C2_V1", 7, maB1_G, maB3_D, maA2_G, maA3_G},
{"C2_V2", 8, maB1_G, maB3_D, maA2_D, maA4_G},
{"C2_V3", 9, maB1_D, maB4_D, maNU, maNU},
{"C3_V1", 10, maB2_G, maB3_G, maA2_G, maA3_G},
{"C3_V2", 11, maB2_G, maB3_G, maA2_D, maA4_G},
{"C3_V3", 12, maB2_G, maB4_G, maNU, maNU},
};
void setup()
{
Serial.begin(115200);
//--------------------------------- Initialisation des bp
for (int b = 0; b < bpNombreMax; b ++)
{
pinMode(bp[b].port, INPUT_PULLUP); // Pour bpEtatPresse LOW
}
//--------------------------------- Initialisation des moteurs d'aiguilles
for (int ma = 0; ma < motAigNombreMax; ma ++)
{
pinMode(motAig.port[ma], OUTPUT);
digitalWrite(motAig.port[ma], !motEtatOn); // Moteur a l'arrêt
}
gareInitialisation();
}
void loop()
{
for (int b = 0; b < bpNombreMax; b ++) // Balayage des bp
{
if (digitalRead(bp[b].port) == bpEtatPresse)
{
bpPresseAction(b);
while(digitalRead(bp[b].port) == bpEtatPresse){} // Attente du relâchement du bp
delay(100); // Pour eviter les rebonds au relachement
}
}
}
//------------------------------------- Initialisation de la gare
void gareInitialisation()
{
Serial.println(F("\tC2_V1"));
Serial.println(F("Initialisation de la gare"));
motAigMouvement(maG1_D, true); // aiguillage G1 D
motAigMouvement(maA1_G, true); // aiguillage A1 G
motAigMouvement(maA3_G, true); // aiguillage A3 G
Serial.println(F("\tC2_V2"));
motAigMouvement(maB1_G, true); // aiguillage B1 G
motAigMouvement(maB3_D, true); // aiguillage B3 D
motAigMouvement(maA2_D, true); // aiguillage A2 D
motAigMouvement(maA4_G, true); // aiguillage A4 G
Serial.println(F("\tC3_V3"));
motAigMouvement(maB2_D, true); // aiguillage B2 D
motAigMouvement(maB4_G, true); // aiguillage B4 G
}
//------------------------------------- Action quand bouton bpNumero est pressé
void bpPresseAction(int bpNumero)
{
Serial.print(F("Bouton\t"));
Serial.print(bp[bpNumero].nom);
Serial.print(F("\tPresse\t aiguilles\t"));
for (int m = 0; m < bpMotNombreMax; m ++) // Lister tout les ports moteur du bp bpNumero
{
int motAigIndex = bp[bpNumero].motPort[m]; // Numero du port moteur
if (motAigIndex != maNU) // Si port utilise
{
Serial.print(motAig.nom[motAigIndex]); Serial.print("\t");
motAigMouvement(motAigIndex, false);
}
}
Serial.println("");
}
//------------------------------------- Moteur aiguille mouvement
void motAigMouvement(byte motAigIndex, boolean echoCmd)
{
byte motAigPort = motAig.port[motAigIndex];
digitalWrite(motAigPort, motEtatOn); // Moteur ON
delay(motEtatOnTemps);
digitalWrite(motAigPort, !motEtatOn); // Moteur NOT ON
delay(motEtatOnTemps);
if (echoCmd)
{
Serial.print(F("Aiguille ")); Serial.println(motAig.nom[motAigIndex]);
}
} |
Partager