Domasi,
this code will increment a 7Seg display from a blank display, to zero, then digits one through nine and back to blank display on the twelveth button press. Button connected to Arduino pin 12.
Connect Arduino pins as follows –
Pin 9 Segment Decimal Point
8 A
7 B
6 C
5 D
4 E
3 F
2 G
// Code by Pedro147 Arduino Forum - Thanks to Grumpy Mike 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 };
int switchPin = 12; // pushbutton attached to pin 12
int buttonState = LOW; // initialise buttonState as LOW
int counter = 0; // initialise counter as zero
int timer1 = 100; // delay timer interval
int timer = 1000;
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);
if (buttonState != newstate) // has the state changed from
// HIGH to LOW or vice versa
{
buttonState = newstate;
if (newstate == LOW) // IF the button was pressed
++counter; // increment the counter by one
delay(20); // delay to debounce
}
if(counter > 10)
counter = 0;
displayEleven(counter);
}
void displayEleven(int num)
{
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;
}
}