I have received my 7 Seg displays and wired them up to display, blank display, then zero through to nine and then back to blank display etc . I have modified the code from my previous post where it was controlling eight LED's. It all works as I hoped for but I cannot for the life of me work out how to debounce the switch input. I have looked at all the examples of debounce codes and understand the basic principles but everything that I have tried so far is not working. As this is part of a larger project any help (as usual) would be greatly appreciated,
Thank you Pedro.
// Thanks to Grumpy Mike Arduino forum for this article - http://www.thebox.myzen.co.uk/Tutorial/Arrays.html
int pins [] = { 2, 3, 4, 5, 6, 7, 8, 9 }; // pin 9 allocated to DP but not used (first element of binary array in char elevenCode)
int digit[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // array to allow pushbutton to advance through elevenCode array elements
int switchPin = 12; // pushbutton attached to pin 12
int buttonState = LOW; // initialise buttonState as LOW
int counter = 0; // initialise counter at zero
char elevenCode[] = { B00000000, B01111110, B00110000, B01101101, B01111001, B00110011, B01011011, B01011111, B01110000, B01111111, B01111011 };
// Array element # No Display 0 1 2 3 4 5 6 7 8 9
void setup()
{
for(int i = 0; i < 8; i++) // set digital pins as OUTPUTS
pinMode(pins[i], OUTPUT);
pinMode(switchPin, INPUT); // sets pin 12 as pushbutton INPUT
}
void loop()
{
int newstate = digitalRead(switchPin); // has the state changed from
if (buttonState != newstate) // HIGH to LOW or vice versa
{
buttonState = newstate;
if (newstate == LOW) // If the button was pressed
counter++; // increment the counter by one
}
if(counter > 10)
counter = 0;
switch(counter)
{
case 0:
displayEleven(digit[0]); // function to display element 0 of elevenCode array
break;
case 1:
displayEleven(digit[1]); // function to display element 1 of elevenCode array
break;
case 2:
displayEleven(digit[2]); // function to display element 2 of elevenCode array
break;
case 3:
displayEleven(digit[3]); // function to display element 3 of elevenCode array
break;
case 4:
displayEleven(digit[4]); // function to display element 4 of elevenCode array
break;
case 5:
displayEleven(digit[5]); // function to display element 5 of elevenCode array
break;
case 6:
displayEleven(digit[6]); // function to display element 6 of elevenCode array
break;
case 7:
displayEleven(digit[7]); // function to display element 7 of elevenCode array
break;
case 8:
displayEleven(digit[8]); // function to display element 8 of elevenCode array
break;
case 9:
displayEleven(digit[9]); // function to display element 9 of elevenCode array
break;
case 10:
displayEleven(digit[10]); // function to display element 10 of elevenCode array
break;
}
}
void displayEleven(int num) // function to set OUTPUTS of 7_Seg from elevenCode array
{
int mask = 1;
for(int i = 0; i < 8; i++)
{
if((mask & elevenCode[num]) == 0)
digitalWrite(pins[i], LOW);
else digitalWrite(pins[i], HIGH);
mask = mask << 1;
}
}