Accès aux attributs d'une classe
Bonjour,
j'ai créé un objet Relais dans une petite bibliothèque pour gérer le fonctionnement de relais. Ceux-ci fonctionnent correctement. J'ai ajouté une méthode pour lire l'état de mes relais (ON ou OFF). Mais là, ils sont toujours à OFF.
Je dois faire une erreur de concept dans le fonctionnement de ma Classe. Si quelqu'un pouvait m'éclairer?
Relais.h
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
/**********************************************************
**************Bibliotèque Relais***************************
*******************************Mormic mai 2021*****/
#ifndef RELAIS_H_INCLUDED
#define RELAIS_H_INCLUDED
#include "Arduino.h"
class Relais{
protected:
int rPin;
char rMode;
unsigned int rTime;
public:
bool rEtat;
Relais();
Relais(int pin, char m, unsigned int t=0);
void begin();
void actionOn();
void actionOf();
bool relEtat();
};
#endif // RELAIS_H_INCLUDED |
Relais.cpp
Code:
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
|
/**********************************************************
**************Bibliotèque Relais***************************
*******************************Mormic mai 2021*****/
#include "Relais.h"
Relais::Relais(){}
Relais::Relais(int pin, char m, unsigned int t){
rPin = pin;
rMode = m;
rTime = t;
}
void Relais::begin(){
pinMode(rPin,OUTPUT);
}
void Relais::actionOn(){ //mode interrupteur
unsigned long x, y;
if(rMode == 'I'){
digitalWrite(rPin,HIGH);
rEtat = true;
}
else if(rMode =='T'){ //mode temporisation
y = rTime * 1000;
digitalWrite(rPin,HIGH);
x = millis();
rEtat = true;
while(millis()-x <= y){}
digitalWrite(rPin,LOW);
rEtat = false;
}
else if(rMode =='P'){ //mode poussoir
y = rTime;
digitalWrite(rPin,HIGH);
x = millis();
while(millis()-x <=y){}
digitalWrite(rPin,LOW);
rEtat = !rEtat;
}
}
void Relais::actionOf(){
if(rMode == 'I' || rMode =='T'){
digitalWrite(rPin, LOW);
rEtat = false;
}
}
bool Relais::relEtat(){
return rEtat;
} |
Prog
Code:
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
|
Relais rel1(25, 'I'); //mode 'I'=Inter 'P'=Poussoir temps en millis 'T'=Tempo en secondes
Relais rel2(26, 'P', 500); //Instance de l'objet Relais (pin, mode, temps commutation)
Relais rel3(27, 'T', 4);
rel1.begin();
rel2.begin();
rel3.begin();
//***************************Analyse message bluetooth*************************************
void analyseBT(String msg){
String x = msg.substring(0,3);
if(x=="env"){envoiEnviron();}
else if(x=="tem"){envoiTemp();}
else if(x=="rel"){comRelais(msg);} //forme de la commande: relxon / relxof
else if(x=="con"){configTemp(msg);} //commande: conftempx
else if(x=="eta"){envoiEtatRel();} //commande: etat
}
//******************************Commandes des relais**************************************
void comRelais(String msg){ //forme de la commande: relxon ou relxof
Relais rel;
switch(msg.charAt(3)){
case '1': rel = rel1; break;
case '2': rel = rel2; break;
case '3': rel = rel3; break;
}
if(msg.charAt(5)=='n') rel.actionOn(); //'n' -> on 'f' -> off
else if(msg.charAt(5)=='f') rel.actionOf();
}
//***********************Envoi l'état des relais*********************************
void envoiEtatRel(){
/* Relais rel[3] = {rel1, rel2, rel3};
for(int i=0; i<3; i++){
if(rel[i].relEtat() == true) SerialBT.println("Relais " + String(i+1) + " : ON");
else SerialBT.println("Relais " + String(i+1) + " : OFF");
}
*/
if(rel1.relEtat() == true) SerialBT.println("Relais 1: ON");
else SerialBT.println("Relais 1: OFF");
if(rel2.relEtat() == true) SerialBT.println("Relais 2: ON");
else SerialBT.println("Relais 2: OFF");
if(rel3.relEtat() == true) SerialBT.println("Relais 3: ON");
else SerialBT.println("Relais 3: OFF");
} |
Merci d'avance
Michel