Hello,
I want to build a simple countdown timer just for fun. I have a 7 segment led for displaying the time from 9s to 0s, one push button to start the countdown, one push button to increase the time, and a buzzer to buzz when the time is over.
The problem is my code is not running well. It display a 8 and nothing append when I push on a button.
Have you got any idea of what is the problem ?
/* Test de compteur à rebours
*
* un afficheur 7 segments affiche un temps de 0 à 9s
* un bouton on
* un bouton pour incrémenter le temps
* un buzzer pour signaler la fin du temps
*
*/
//constantes
int onPin = 12 ; //pour le bouton on
int reglPin = 13 ; //pour le bouton de réglage - for incrementing button
int buzzerPin = 11 ; //pour le buzzer
int ledPins[] = {2, 3, 4, 5, 6, 7, 8} ; //liste des leds
//variables
int compte = 0 ; //variable de comptage - for counting
// listes des pins pour faire les nombres
//pin 1, 2, 4, 5, 7, 9, 10 sur l'afficheur - on the 7 segment led
int seven_seg_digits[10][7] = { { 0,1,1,1,1,1,1 }, // = 0
{ 0,0,0,0,1,1,0 }, // = 1
{ 1,0,1,1,0,1,1 }, // = 2
{ 1,0,0,1,1,1,1 }, // = 3
{ 1,1,0,0,1,1,0 }, // = 4
{ 1,1,0,1,1,0,1 }, // = 5
{ 1,1,1,1,1,0,1 }, // = 6
{ 0,0,0,0,1,1,1 }, // = 7
{ 1,1,1,1,1,1,1 }, // = 8
{ 1,1,0,0,1,1,1 } // = 9
};
/*
* Fonctions
*/
// pour faire craquer le buzzer - for buzzing
void buzz() {
digitalWrite(buzzerPin, HIGH);
delayMicroseconds(2000);
}
// pour l'affichage - for displaying
void afficheLeds(int digit) {
int pin = 2;
for (int count = 0; count<7 ; ++count) {
digitalWrite(pin, seven_seg_digits[digit][count]);
++pin;
}
}
// pour compter à rebours - countdown
void compteRebours(int count) {
for (int c = count; c>0; --c) {
delay(1000);
afficheLeds(c);
}
buzz();
}
/*
* SetUp
*/
void setup()
{
for(int i=0; i<7 ; i++) { //on défini les leds comme des outputs
pinMode(ledPins[i], OUTPUT);
}
pinMode(buzzerPin, OUTPUT); //on défini le buzzer en output
pinMode(onPin, INPUT);
pinMode(reglPin, INPUT);
}
/*
* Boucle principale
*/
void loop()
{
afficheLeds(compte);
// pour incrémenter le temps - for incrementing time
if (digitalRead(reglPin) == LOW) {
++compte;
if(compte>9) {
compte = 0;
}
afficheLeds(compte);
}
//pour lancer le compte à rebours - for running the countdown
else if (digitalRead(onPin) == LOW) {
compteRebours(compte);
}
}