The third and final Arduino sketch uses bits to represent each segment and is a reduced code version of the previous sketch (1,210 bytes for sketch #3 instead of 1,852 bytes for sketch #2). A ten element array holds a byte for each number 0-9 that specifies what segments should be lit (pin low). Bit 0 corresponds to segment A, bit 1 to segment B and so on. In order to display the number 1, segments B and C need to be lit, so that is represented by the value 0b1111001. Function “lightSegments” reads these bits in sequence and sets the corresponding segments accordingly.
Sketch #3:
// www.TheElectronicsHobbyist.com/blog // Natalia Fargasch Norman // Seven-segment LED Display // Common Anode pins 3 and 8 // G F + A B // | | | | | -> pins and segments they control // --------- // F| A |B // |---G---| -> segments // E| D |C // --------- // | | | | | -> pins and segments they control // E D + C DP // Segments that make each number when lit: // 0 => -FEDCBA // 1 => ----BC- // 2 => G-ED-BA // 3 => G--DCBA // 4 => GF--CB- // 5 => GF-DC-A // 6 => GFEDC-A // 7 => ----CBA // 8 => GFEDCBA // 9 => GF-DCBA // Arduino digital pins used to light up // corresponding segments on the LED display #define A 2 #define B 3 #define C 4 #define D 5 #define E 6 #define F 7 #define G 8 // Pushbutton connected to pin 9 #define BUTTON 9 int count = 0; // current display count const byte numbers[10] = { 0b1000000, 0b1111001, 0b0100100, 0b0110000, 0b0011001, 0b0010010, 0b0000010, 0b1111000, 0b0000000, 0b0010000 }; void setup() { pinMode(A, OUTPUT); pinMode(B, OUTPUT); pinMode(C, OUTPUT); pinMode(D, OUTPUT); pinMode(E, OUTPUT); pinMode(F, OUTPUT); pinMode(G, OUTPUT); pinMode(BUTTON, INPUT); lightSegments(0b1000000); } void loop() { int val = digitalRead(BUTTON); if (val == HIGH) { count++; if (count == 10) count = 0; delay(200); lightSegments(numbers[count]); } } void lightSegments(byte number) { for (int i = 0; i < 7; i++) { int bit = bitRead(number, i); // segments connected to Arduino pins 2-8 digitalWrite(i+2, bit); } }
Watch a short video of this project in action.
You might also enjoy:





{ 6 comments… read them below or add one }
Hey, how would you write the code if you wanted two 7 segments so you could count to 10?
Argh, somehow your comment slipped out of my radar. Sorry Emily for not replying before. You may have already gotten your answer somewhere else, but you can also check my post from June 1st 2010, 2-digit 7-segment display counter:
http://www.theelectronicshobbyist.com/blog/2010/06/arduino-2-digit-7-segment-display-counter/
Hello
Will can give me a hand?
I want to change this schedule. Make each of the numbers, do the same time that shows on the display.
Example:
Display value 5, the procedure is repeated five times controlled by a censor. When account is activated. for the coming five times.
Use the button to set the desired value and another button will confirm the value and start the procedure. Repeated until the desired times.
Thank you
Sorry Lipzy, I’m not sure I understand your question… but if you want the input value to determine how many times an event should occur, you could read how many times the button was pressed and store that value in a variable. Later on you would use that variable as your for loop control counter. But I’m not sure of what you want to do. Feel free to clarify and I’ll try again to help! Good luck!
Yes that is exactly what he intended.
Does it help me?
I apologize for my English, no big deal!
Thank you.
Awesome. No need to apologize on the English… I understand! I am from Brazil and English is not my first language either.
{ 1 trackback }