I'm new in the Arduino world and I want to make a counter with two common anode 7-segment displays. The wiring for the pushbutton I'm using is with pull-up resistors. The wiring I'm using is shown in the images. The wiring is fine, no problem with that, but I need help with the programming.
For this program, I used the code from section 5.3 of the Arduino Cookbook for the debounce function and the code from secction 7.11 of the same Cookbook. Everything went fine, the project can count from 00 to 99, but I wanted to count with every push, but it only counts when I press the button for certain time. I want that if I press the button, no matter how long, it only counts one time. If I press the button 5 times, it counts five times, no matter how fast I push the button. I've been trying a lot with the code, but I can't seem to make it right. I will really appreaciate the help. The code I'm using is below:
void Display_Segment(int);
int startPin=2;
int digit[10][7]={{0,0,0,0,0,0,1}, //Digit "0"
{1,0,0,1,1,1,1}, //Digit"1"
{0,0,1,0,0,1,0},//Digit"2"
{0,0,0,0,1,1,0},//Digit"3"
{1,0,0,1,1,0,0}, //Digit"4"
{0,1,0,0,1,0,0}, //Digit"5"
{0,1,0,0,0,0,0}, //Digit"6"
{0,0,0,1,1,1,1}, //Digit "7"
{0,0,0,0,0,0,0}, //Digit "8"
{0,0,0,0,1,0,0}}; //Digit"9"
const int ledPin = 10; //the number of LED pin
const int buttonPin = 11; // the number of the pushbutton
int ledState = LOW; //the current state of the output
int buttonState = LOW; //the current reding from the input pin
int lastButtonState = LOW; //the previuos reding from theinput pin
unsigned long lastDebounceTime = 0;//the last time the output pin was toggled
int debounceDelay = 50; // the debounce time; increase if the output flickersa
void setup() {
// seven segment pins as OUTPUT
for(int a=2 ;a<=8;a++){
pinMode(a,OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
digitalWrite(buttonPin, HIGH);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW); //set initial LED state
}
}
void loop() { //read the state of the switch into a local variable:
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH && lastButtonState == LOW) { //indien knop werd ingedrukt EN dit is opgaande flank, start debounce
// reset the debunce timer
while (lastDebounceTime == 0) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) //start debounce
{
while (buttonState != lastButtonState) //uitvoer zolang de knop nog ingedrukt is, en dus =1
{
buttonState = digitalRead (buttonPin);
}
lastButtonState = !buttonState; // wordt 1 zodra knop gelost is
}
}
if (buttonState == LOW && lastButtonState == HIGH) // enkel uitvoer op neergande flank EN nadat knop gelost werd EN na debounce
{
digitalWrite (ledPin, HIGH);
delay(500);
digitalWrite (ledPin, LOW);
for(int value =0;value<=9;value++)
{
delay(1000);
Display_Segment(value);// passing value to the function for displaying on segment
if ( value = value++ ){
value = 0;
}
}
delay(2500);
}
lastButtonState = buttonState; //alles terug naar begintoestand: 0
lastDebounceTime = 0;
}
void Display_Segment(int value){
{
int startPin=2;
for(int x=0;x<7;x++){
digitalWrite(startPin, digit[value][x]);
startPin++;
}
}
}