#define PORT_LED 5
#define PORT_SENSOR A0
#define MAX_LED 10
#define VOLT_MAX 5
class Sensor{
private:
int portSensor;
public:
Sensor(int portSensor){
this->portSensor = portSensor;
}
int getValue(){
return(analogRead(portSensor));
}
float getVoltage(){
float voltage = float(this->getValue())*(5.0 / 1023.0);
return(voltage);
}
};
class Led{
private:
int portLed;
public:
Led(){
}
void init(int portLed){
this->portLed = portLed;
pinMode(this->portLed,OUTPUT);
}
void setOn(){
digitalWrite(portLed, true);
}
void setOff(){
digitalWrite(portLed, false);
}
void setFlash(){
setOn();
delay(1000);
setOff();
delay(1000);
}
};
class Bargraph{
/*Le pas d'allumage des LED dépend de la tension maximale
Il est égale à tension max / (nombre de LED +1)
Par exemple, pour 4 LED, le seuil est de 1v
pour 8 Led, le seuil est de 0,55
On initialise donc le bargraph avec le nombre de LED et la tension max
(on suppose cependant ici que la tension min = 0)
*/
private:
float step;//Pas en volts permettant de calculer les seuils d'allumage
int nbLed; //Nombre de LED
int portLed; //numéro de port de la première LED (les LED doivent se suivre)
Led tabLed[MAX_LED];
int type=1;//Type de bargraph : 4 ou 8 LED
void setEraseAll(){
for(int i=0;i<nbLed;i++){
tabLed[i].setOff();
}
}
public:
Bargraph(int portLed, float maxVolt, int nbLed){
//On calcule le type de bargraph à 4 ou 8 LED
this->portLed = portLed;//On initialise le premier port
this->nbLed = nbLed;
this->type = 8/this->nbLed; //type = 2 si 4 LED, 1 sinon
//Calcul du step en volts
this->step = maxVolt/(this->nbLed + 1);
//Initialisation des LED;
for(int i=0;i<MAX_LED;i++){
tabLed[MAX_LED-i-1].init(portLed+i*type);//Initialisation des ports de LED
}
}
int setDisplay(float voltage){
//La LED max à allumer est calculée ainsi :
//Serial.print("Pas : ");
//Serial.print(this->step);
//Serial.print(" Volts : ");
//Serial.print(voltage);
//Serial.print(" Numero LED : ");
int numberLed = int(voltage/this->step);
setEraseAll();
for(int i=0;i<numberLed;i++){
tabLed[i].setOn();
}
//Serial.print(numberLed);
//Serial.print(" Type : ");
//Serial.println(this->type);
return(numberLed);
}
};
//Création des objets
Sensor sensor(PORT_SENSOR); //Création d'un objet pour lire le potentiomètre
Bargraph bargraph(PORT_LED,VOLT_MAX,MAX_LED); //Création d'un objet bargraphe : on fournit le numéro de port de la première LED, la tension maximale et le nombre de LEDs
void setup() {
Serial.begin(9600);
}
void loop() {
bargraph.setDisplay(sensor.getVoltage()); //Affichage sur le bargraphe
}