Bonjour log6330
Le programme à 115200:
/*
Name: ARDFR_log6330_CmdVannesAspi.ino
Created: 08.11.2023
Author: jpbbricole/log6330
09.11.2033 Dialogue Nextion > Arduino #31
09.11.2033 Inversion des sens #37
10.11.2033 Montage proto #43
13.11.2033 Dialogue Arduino > Nextion Ecran log6330 NtoA.HMI #77
03.12.2033 Serial à 9600 #109
Commande des 4 vannes de l'aspirateur au moyen de servo
https://forum.arduino.cc/t/commande-pour-aspirateur-copeaux-ouverture-vannes/1186155
https://www.tinkercad.com/things/lucVz8vXAQ2-ardfrcommandeaspirateur
*/
#include <Servo.h>
//------------------------------------ Vannes
struct vanneParamDef // Définition des paramètres des servo
{
const int pwmPin; // Pin du signal PWM
const int pwmOuvert; // Impulsion maximum, position ouverte
const int pwmFerme; // Impulsion minimum, position fermée
boolean siOuverte; // Si vanne ouverte
const int ledVertePin; // Pin de la LED verte
const int ledRougePin; // Pin de la LED rouge
const int boutonPin; // Bouton On/Off
};
const int vanneLedEtatOn = HIGH; // Etat pour allumer la LED
const int boutonEtatOn = LOW; // Etat du bouton pressé. PULLUP, un côté du bouton à GND
vanneParamDef vannesParam[] =
{
// Pin PWMmin PWMmax Ouvert led V led R Bouton
{2, 900, 2100, false, 6, 7, A0},
{3, 900, 2100, false, 8, 9, A1},
{4, 900, 2100, false, 10, 11, A2},
{5, 900, 2100, false, 12, 13, A3},
};
const int vannesNombre = sizeof(vannesParam) / sizeof(vannesParam[0]); // Nombre de vannes
Servo vanneServo[vannesNombre]; // Création de l'objet vanneServo
//------------------------------------- Nextion (next)
enum nextObjIndex {nextObjNum, nextObjTxt}; // Type d'objets Nextion
String cmdRx = ""; // Commande reçue de nextion
boolean cmdRxNew = false; // Si une nouvele commande reçue
const int nextRxLed = A5; // Témoin de réception
const int nextRxLedEtatOn = HIGH; // Etat pour ON
unsigned long nextBtnRefreskTempo = 500; // Frequence de rafraîchissement des boutons de Nextion
unsigned long nextBtnRefreskMillis = millis(); // Frequence de rafraîchissement des boutons de Nextion, chrono
void setup()
{
Serial.begin(9600);
Serial.setTimeout(50); // Pour fin des commandes depuis le moniteur
pinMode(nextRxLed, OUTPUT); // LED témoin de réception Nextion
digitalWrite(nextRxLed, !nextRxLedEtatOn); // Eteindre la LED
for (int v = 0; v < vannesNombre; v ++) // Initialisation des servo
{
vanneServo[v].attach(vannesParam[v].pwmPin); // MIse en route du servo s
pinMode(vannesParam[v].ledVertePin, OUTPUT);
digitalWrite(vannesParam[v].ledVertePin, !vanneLedEtatOn); // Eteindre la LED
pinMode(vannesParam[v].ledRougePin, OUTPUT);
digitalWrite(vannesParam[v].ledRougePin, !vanneLedEtatOn); // Eteindre la LED
pinMode(vannesParam[v].boutonPin, INPUT_PULLUP); // PULLUP, un côté du bouton à GND
}
vanneOuvrir(-1); // Tout fermer
nextBoutonsEtat(); // Mise à jour de l'état des boutons
}
void loop()
{
//--------------------------------- Lecture des boutons
int boutonPresse = boutonsLecture();
if (boutonPresse != -1) // Si au moins 1 bouton pressé
{
//Serial.println(boutonPresse);
if (boutonPresse == -2) // Si reset (les 2 boutons extrêmes)
{
vanneOuvrir(-1); // Tout fermer
}
else
{
vanneOuvrir(boutonPresse); // Ouvrir la vanne
}
//Serial.println("OK");
boutonPresse = -1;
}
//--------------------------------- Rafraîchissement des boutons vers Nextion
if (millis() - nextBtnRefreskMillis >= nextBtnRefreskTempo) // Si temps de rafraîchissement
{
nextBoutonsEtat();
nextBtnRefreskMillis = millis();
}
//--------------------------------- Lecture port série Nextion
serialLecture();
if (cmdRxNew) // Si une commande reçue
{
digitalWrite(nextRxLed, nextRxLedEtatOn); // Allumer la LED de réception Nextion
nextCommandeRecue(cmdRx);
cmdRxNew = false; // La commande a été traitée
delay(200);
digitalWrite(nextRxLed, !nextRxLedEtatOn); // Eteindre la LED de réception Nextion
}
}
//------------------------------------- Nextion (next)
void nextCommandeRecue(String nextCmd)
{
nextCmd.toUpperCase(); // Tout en majuscules
nextCmd.replace(" ", ""); // Supprimer les espaces
Serial.println("\nExecution de : " + nextCmd);
int nextCmdParamInt; // Paramètre de la commande
if (nextCmd.startsWith("V")) // Si commande vanne
{
nextCmd.replace("V", ""); // On ne garde que le numéro de la vanne
nextCmdParamInt = nextCmd.toInt();
vanneOuvrir(nextCmdParamInt); // Ouvrir la vanne, si -1 toutes les vannes fermées
}
else
{
Serial.print(F("Commande inconnue!!! ")); Serial.println(nextCmd);
}
}
//------------------------------------- Mise à jour des boutons ON/OFF interface Nextion
void nextBoutonsEtat()
{
String btnTrace = "";
nextSendEnd();
for (int v = 0; v < vannesNombre; v ++)
{
String btnName = "bt" + String(v); // Nom du bouton dans Nextion
String btnValue = (vannesParam[v].siOuverte) ? "1" : "0";
btnTrace += (btnName + btnValue + " ");
nextSendValue(nextObjNum, btnName, btnValue);
}
//Serial.println(btnTrace);
}
void nextSendValue(int objectType, String objectName, String objectData)
{
String nextData = "";
switch (objectType)
{
case nextObjNum:
nextData = objectName + ".val=" + objectData;
break;
case nextObjTxt:
nextData = objectName + ".txt=\"" + objectData + "\"";
break;
}
for (int i = 0; i < nextData.length(); i++)
{
Serial.write(nextData[i]);
}
nextSendEnd();
delay(30);
}
void nextSendEnd() // Fin de transmission
{
Serial.write(0xff); //We need to write the 3 ending bits to the Nextion as well
Serial.write(0xff); //it will tell the Nextion that this is the end of what we want to send.
Serial.write(0xff);
}
//------------------------------------- Ouverture de la vanne vanneNum
// vanneNum -1 = tout fermer
void vanneOuvrir(int vanneNum)
{
for (int v = 0; v < vannesNombre; v ++)
{
//Serial.print("Vanne" + String(v) + "\t");
if (vanneNum != -1) // Si pas tout fermer
{
if (v == vanneNum) // Si c'est la bonne vanne, on ouvre
{
vanneServo[v].write(vannesParam[v].pwmOuvert);
vannesParam[v].siOuverte = true;
//Serial.println("OUVRIR");
}
else // Autrement on ferme
{
vanneServo[v].write(vannesParam[v].pwmFerme);
vannesParam[v].siOuverte = false;
//Serial.println("FERMER");
}
}
else
{
vanneServo[v].write(vannesParam[v].pwmFerme); // On ferme tout
vannesParam[v].siOuverte = false;
//Serial.println("FERMER*");
}
}
vannesLedEtat();
nextBoutonsEtat();
}
//------------------------------------- Allumage des LED en fonction de l'état de la vanne
void vannesLedEtat()
{
for (int v = 0; v < vannesNombre; v ++)
{
if (vannesParam[v].siOuverte)
{
digitalWrite(vannesParam[v].ledRougePin, !vanneLedEtatOn); // Allumer la LED
digitalWrite(vannesParam[v].ledVertePin, vanneLedEtatOn); // Eteindre la LED
}
else
{
digitalWrite(vannesParam[v].ledRougePin, vanneLedEtatOn); // Allumer la LED
digitalWrite(vannesParam[v].ledVertePin, !vanneLedEtatOn); // Eteindre la LED
}
}
}
//------------------------------------- Retourne -1 si pas de bouton pressé
// Retourne -2 si fonction reset (les 2 extrêmes pressés)
int boutonsLecture()
{
if ((digitalRead(vannesParam[0].boutonPin) == boutonEtatOn) && (digitalRead(vannesParam[vannesNombre -1].boutonPin) == boutonEtatOn))
{
return -2; // Pour reset
}
for (int v = 0; v < vannesNombre; v ++)
{
if (digitalRead(vannesParam[v].boutonPin) == boutonEtatOn) // Si ce bouton est pressé
{
return v;
}
}
return -1;
}
//------------------------------------- Lecture port série
void serialLecture()
{
if(Serial.available() > 0)
{
cmdRx = Serial.readString();
cmdRxNew = true;
}
}
Le programme Nextion:
Ecran log6330 NtoA.zip (235.2 KB)
Une toute belle journée à toi.
jpbbricole