Last active
January 14, 2019 00:04
-
-
Save ypelletier/5940893 to your computer and use it in GitHub Desktop.
Ce sketch contrôle un bargraphe à LED branché à un Arduino par l'entremise d'un compteur décimal CD4017. Le nombre de LEDs allumées est proportionnel au signal analogique capté à l'entrée A0.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/************************************************************** | |
Bargraphe | |
Ce sketch contrôle un bargraphe à LED branché à un Arduino | |
par l'entremise d'un compteur décimal CD4017. Le nombre | |
de LEDs allumées est proportionnel au signal analogique | |
capté à l'entrée A0. | |
Inspiré d'un sketch de Leonel Machava http://codentronix.com | |
http://electroniqueamateur.blogspot.com/2013/07/bargraphe-led-arduino-et-cd4017.html | |
*************************************************************/ | |
// Les connections entre l'Arduino et le CD4017 | |
int clockPin = 2; // pin 14 du CD4017 | |
int resetPin = 3; // pin 15 du CD4017 | |
int powerPin = 4; // pin 16 du CD4017 | |
// Entrée analogique qui reçoit le signal qu'on veut | |
// afficher sur le bargraphe | |
int signalPin = A0; | |
void setup() { | |
pinMode(clockPin,OUTPUT); | |
pinMode(resetPin,OUTPUT); | |
pinMode(powerPin,OUTPUT); | |
} | |
void loop() { | |
int rawValue = analogRead(signalPin); // entre 0 et 1024 | |
int convertedValue = map(rawValue, 0, 1023, 0, 10); //entre 0 et 10 | |
show(convertedValue); //on allume le nombre de LEDs approprié | |
} | |
void show (int value){ | |
if (value == 0){ | |
digitalWrite(powerPin,LOW); // on n'alimente pas le CI: toutes les LEDS sont `éteintes | |
} | |
else{ | |
digitalWrite(powerPin,HIGH); // on alimente le CI: une LED allumée à la fois | |
} | |
digitalWrite(resetPin,HIGH); // compteur remis à zéro: première LED allumée | |
digitalWrite(resetPin,LOW); | |
delay(1); | |
for( int i = 0; i < value-1; i++ ) { | |
digitalWrite(clockPin,HIGH); // compteur incrémenté: ça allume la LED suivante | |
delay(1); | |
digitalWrite(clockPin,LOW); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment