const int digitPins[] = {10, 11, 12, 13}; // Remplacez par les numéros de broches réels pour les digits
const int segmentPins[] = {2, 3, 4, 5, 6, 7, 8}; // Remplacez par les numéros de broches réels pour les segments
const int buttonPin0 = A0; // Broche du bouton-poussoir
const int buttonPin1 = A1; // Broche du bouton-poussoir
const int buttonPin2 = A2; // Broche du bouton-poussoir
const int buttonPin3 = A3; // Broche du bouton-poussoir
const int buttonPin4 = A4; // Broche du bouton-poussoir
const int buttonPin5 = A5; // Broche du bouton-poussoir
const int buttonPin6 = 9; // Broche du bouton-poussoir
const int buttonPin7 = 10; // Broche du bouton-poussoir
const byte digitPatterns[][7] = {
{0, 0, 0, 0, 0, 0, 1}, // 0
{1, 0, 0, 1, 1, 1, 1}, // 1
{0, 0, 1, 0, 0, 1, 0}, // 2
{0, 0, 0, 0, 1, 1, 0}, // 3
{1, 0, 0, 1, 1, 0, 0}, // 4
{0, 1, 0, 0, 1, 0, 0}, // 5
{0, 1, 0, 0, 0, 0, 0}, // 6
{0, 0, 0, 1, 1, 1, 1}, // 7
{0, 0, 0, 0, 0, 0, 0}, // 8
{0, 0, 0, 0, 1, 0, 0} // 9
};
byte digitStates[4] = {0, 0, 0, 0}; // État de chaque digit
int currentDigit = 0; // Digit actuel à afficher
bool buttonPin0Pressed = false; // Indicateur pour suivre l'état du bouton-poussoir
bool buttonPin1Pressed = false; // Indicateur pour suivre l'état du bouton-poussoir
bool buttonPin2Pressed = false; // Indicateur pour suivre l'état du bouton-poussoir
bool buttonPin3Pressed = false; // Indicateur pour suivre l'état du bouton-poussoir
bool buttonPin4Pressed = false; // Indicateur pour suivre l'état du bouton-poussoir
bool buttonPin5Pressed = false; // Indicateur pour suivre l'état du bouton-poussoir
bool buttonPin6Pressed = false; // Indicateur pour suivre l'état du bouton-poussoir
bool buttonPin7Pressed = false; // Indicateur pour suivre l'état du bouton-poussoir
void setup() {
Serial.begin(9600);
while (!Serial) {
; // Attendez que le port série se connecte. Nécessaire pour USB natif
}
Serial.println("Initialisation complétée.");
// Initialisez les broches des digits et des segments en tant que sorties
for (int i = 0; i < 4; i++) {
pinMode(digitPins[i], OUTPUT);
}
for (int i = 0; i < 8; i++) {
pinMode(segmentPins[i], OUTPUT);
}
// Initialisez les broches des boutons en tant qu'entrées avec résistance de pull-up
pinMode(buttonPin0, INPUT_PULLUP);
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
pinMode(buttonPin4, INPUT_PULLUP);
pinMode(buttonPin5, INPUT_PULLUP);
pinMode(buttonPin6, INPUT_PULLUP);
pinMode(buttonPin7, INPUT_PULLUP);
turnOffAllSegment();
}
void loop() {
// Bouton0
if (digitalRead(buttonPin0) == LOW && !buttonPin1Pressed) {
buttonPin1Pressed = true;
digitStates[currentDigit] = 0;
currentDigit = (currentDigit + 1) % 4;
}
if (digitalRead(buttonPin0) == HIGH) {
buttonPin1Pressed = false;
}
// Bouton1
if (digitalRead(buttonPin1) == LOW && !buttonPin1Pressed) {
buttonPin1Pressed = true;
digitStates[currentDigit] = 1;
currentDigit = (currentDigit + 1) % 4;
}
if (digitalRead(buttonPin1) == HIGH) {
buttonPin1Pressed = false;
}
// Bouton2
if (digitalRead(buttonPin2) == LOW && !buttonPin2Pressed) {
buttonPin2Pressed = true;
digitStates[currentDigit] = 2;
currentDigit = (currentDigit + 1) % 4;
}
if (digitalRead(buttonPin2) == HIGH) {
buttonPin2Pressed = false;
}
// Bouton3
if (digitalRead(buttonPin3) == LOW && !buttonPin3Pressed) {
buttonPin3Pressed = true;
digitStates[currentDigit] = 3;
currentDigit = (currentDigit + 1) % 4;
}
if (digitalRead(buttonPin3) == HIGH) {
buttonPin3Pressed = false;
}
// Bouton4
if (digitalRead(buttonPin4) == LOW && !buttonPin4Pressed) {
buttonPin4Pressed = true;
digitStates[currentDigit] = 4;
currentDigit = (currentDigit + 1) % 4;
}
if (digitalRead(buttonPin4) == HIGH) {
buttonPin4Pressed = false;
}
// Bouton5
if (digitalRead(buttonPin5) == LOW && !buttonPin5Pressed) {
buttonPin5Pressed = true;
digitStates[currentDigit] = 5;
currentDigit = (currentDigit + 1) % 4;
}
if (digitalRead(buttonPin5) == HIGH) {
buttonPin5Pressed = false;
}
// Affichez les digits mis à jour
displayDigits();
}
void displayDigits() {
for (int digit = 0; digit < 4; digit++) {
// Activez l'anode commune pour le digit spécifié
digitalWrite(digitPins[digit], HIGH);
// Affichez les segments correspondants pour le digit
for (int i = 0; i < 7; i++) {
digitalWrite(segmentPins[i], digitPatterns[digitStates[digit]][i]);
}
// Ajoutez un délai si nécessaire pour contrôler le taux de rafraîchissement de l'affichage
delay(30);
// Éteignez le digit
turnOffAllSegment();
digitalWrite(digitPins[digit], LOW);
}
}
void turnOffAllSegment() {
for (int i = 0; i < 7; i++) {
digitalWrite(segmentPins[i], HIGH);
}
}