const int digits[11] = {
0b0111111, //0
0b0000110, //1
0b1011011, //2
0b1001111, //3
0b1100110, //4
0b1101101, //5
0b1111101, //6
0b0000111, //7
0b1111111, //8
0b1101111, //9
0b0000000, //blank
};
bool bPress = false;
const int IncbuttonPin = A0;
const int DecbuttonPin = A1;
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int IncbuttonState = 0; // current state of the button
int lastIncbuttonState = 0; // previous state of the button
int DecbuttonState = 0; // current state of the button
int lastDecbuttonState = 0; // previous state of the button
void setup() {
// put your setup code here, to run once:
for(int i=0; i<7; i++){
pinMode(i, OUTPUT);
}
pinMode( IncbuttonPin , INPUT_PULLUP );
pinMode( DecbuttonPin , INPUT_PULLUP );
displayDigit(buttonPushCounter);
}
void checkIncButtonPress() {
// compare the IncbuttonState to its previous state
if (IncbuttonState != lastIncbuttonState) {
// if the state has changed, increment the counter
if (IncbuttonState == LOW) {
// if the current state is HIGH then the button went from off to on:
bPress = true;
buttonPushCounter++;
if( buttonPushCounter > 9){
buttonPushCounter =0 ;
} // Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastIncbuttonState = IncbuttonState;
}
}
void checkDecButtonPress() {
// compare the IncbuttonState to its previous state
if (DecbuttonState != lastDecbuttonState) {
// if the state has changed, increment the counter
if (DecbuttonState == LOW) {
// if the current state is HIGH then the button went from off to on:
bPress = true;
buttonPushCounter--;
if( buttonPushCounter < 0){
buttonPushCounter = 9 ;
} // Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastDecbuttonState = DecbuttonState;
}
}
void displayDigit(int digit) {
int pin1, m;
for (pin1 = 0, m = 0; pin1 < 7; pin1++, m++){
digitalWrite(pin1, bitRead(digits[digit], m));
}
}
void turnOff() {
int pins, n;
for (pins = 0, n = 0; pins < 7; pins++, n++){
digitalWrite(pins, bitRead(digits[0], n));
}
}
void loop() {
IncbuttonState = digitalRead(IncbuttonPin);
DecbuttonState = digitalRead(DecbuttonPin);
checkIncButtonPress();
checkDecButtonPress();
if(bPress){
bPress = false;
turnOff();
displayDigit(buttonPushCounter);
}
}