#define PORT_LED 5
#define PORT_MAX 8
#define PORT_MIN 2
#define TEMPO 250
class Led{
private:
int pinLed;
public:
Led ();
bool init(int); //permet d'initialiser l'objet, ici le numéro de port
void setOn();//Allume la LED
void setOff();//Eteindre la LED
};
Led::Led(){}
bool Led::init(int value){
bool error=LOW;
if(value >= PORT_MIN && value <= PORT_MAX){
pinLed = value;
pinMode(pinLed, OUTPUT);
}
else{
error=HIGH;
}
return(error);
}
void Led::setOn(){
digitalWrite(pinLed, HIGH);
delay(TEMPO);
}
void Led::setOff(){
digitalWrite(pinLed, LOW);
delay(TEMPO);
}
/* Progamme principal */
Led tabLed[3]; //On définit un tableau d'objets de 3 LED
void setup(){
Serial.begin(9600);
//Initialisation des Leds
for(short compteur=0;compteur<3;compteur++){
tabLed[compteur].init(PORT_LED + compteur); //Il n'est pas possible d'avoir un constructeur avec paramètres, on utilise donc une fonction d'initialisation
}
}
void loop(){
for(short compteur=0;compteur<3;compteur++){
tabLed[compteur].setOn();//Appel d'une fonction membre d'un objet du tableau
}
for(short compteur=0;compteur<3;compteur++){
tabLed[compteur].setOff();
}
}