I did a small project like that. Count up and count down using push buttons. My reason .... I have a few single 7 segments and I want to try to light them up and practice my coding skills. I dont mind sharing my code. Before the coding is done, you have to determine if the 7 segments is common cathode or common anode. To light up a common cathode, a 1 / true / HIGH will light up, to light up a common anode, a 0 / false / LOW will light up. And the push buttons that I connected, a false / 0 / LOW is a button is pressed. So one of the raison the code that you have may not work is because it simply not matching your circuit, the way you connected. So check each segments to know what pins on the display will light up.
Here the code. It may help others new to Arduino to make the same circuit.
byte segs[7] = {12,11,10,9,8,7,6}; // a,b,c,d,e,f,g
byte button_one = 2;
byte button_two = 3;
boolean button_one_state;
boolean button_two_state;
boolean button_one_press;
boolean button_two_press;
boolean bits;
int counter;
// commun anode display - a zero will light up the segment - format GFEDCBA
// display - 0 1 2 3 4 5 6 7 8 9 A B C D E F
const byte numbers[16] = { B1000000, B1111001, B0100100, B0110000, B0011001,
B0010010, B0000010, B1111000, B0000000, B0010000, B0001000, B0000011,
B1000110, B0100001, B0000110, B0001110 };
void setup()
{
for (byte i=0;i<7;i++)
{
pinMode(segs[i], OUTPUT);
}
pinMode(button_one, INPUT);
pinMode(button_two, INPUT);
for (byte i=0;i<7;i++)
{
digitalWrite(segs[i], HIGH);
}
button_one_state = 1;
button_two_state = 1;
button_one_press = 0;
button_two_press = 0;
delay(2000);
counter = 0;
}
void loop()
{
button_one_state = digitalRead(button_one);
delay(50);
button_two_state = digitalRead(button_two);
delay(50);
if (button_one_state == 0 && button_one_press == 0)
{
count_up();
button_one_press = 1;
button_two_press = 0;
}
else
{
if (button_one_press == 1)
{
count_up();
}
}
if (button_two_state == 0 && button_two_press == 0)
{
count_down();
button_two_press = 1;
button_one_press = 0;
}
else
{
if (button_two_press == 1)
{
count_down();
}
}
}
void count_up()
{
if (counter>15) counter = 0;
for (byte i=0;i<7;i++)
{
bits = bitRead(numbers[counter], i);
digitalWrite(segs[i], bits);
}
delay(1000);
counter++;
}
void count_down()
{
if (counter<0) counter = 15;
for (byte i=0;i<7;i++)
{
bits = bitRead(numbers[counter], i);
digitalWrite(segs[i], bits);
}
delay(1000);
counter--;
}
The key trick is to use arrays for the pins of the display and a look up table ( array of data ) for the 7 segment pattern. And to display the leds on the 7 segments, you use a for loop and use bitread function to read a bit of a byte - the data pattern that is the look up table of the array.
The same technique can be use to put 8 bits of data into a parallel data bus like a LCD display ( without using the library ). Instead reading a data array, you simply do a bitread of the byte you want to send to a 8 bits data bus. Same technique also for a 4 bits data bus, like a 74138 or other multiplex chip. But because of the lack of pins on the Arduino, it better off using a 74595 shift register ( use shiftOut function ) or PCF8574 a I2C chip that use the Wire library.
Here a 7 segments data table the I made. I made it for one of my clock projects to use as a reference to help place the proper data in the look up table.
seven_segment_data_table.pdf (96.5 KB)