-1

Can anyone help me regarding the output of this program?

What is the function of Num_Write?

Here is the code below:

int num_array[10][7] = { { 1,1,1,1,1,1,0 }, // 0 
{ 0,1,1,0,0,0,0 }, // 1 
{ 1,1,0,1,1,0,1 }, // 2 
{ 1,1,1,1,0,0,1 }, // 3 
{ 0,1,1,0,0,1,1 }, // 4 
{ 1,0,1,1,0,1,1 }, // 5 
{ 1,0,1,1,1,1,1 }, // 6
{ 1,1,1,0,0,0,0 }, // 7 
{ 1,1,1,1,1,1,1 }, // 8 
{ 1,1,1,0,0,1,1 }}; // 9 

void Num_Write(int); 

void setup() {  
  pinMode(2, OUTPUT); 
  pinMode(3, OUTPUT); 
  pinMode(4, OUTPUT); 
  pinMode(5, OUTPUT); 
  pinMode(6, OUTPUT); 
  pinMode(7, OUTPUT); 
  pinMode(8, OUTPUT); 
} 

void loop() {  
  for (int counter = 10; counter > 0; --counter) { 
    delay(1000); 
    Num_Write(counter-1); 
  } 
  delay(3000); 
} 

void Num_Write(int number) { 
  int pin= 2; 
  for (int j=0; j < 7; j++) { 
    digitalWrite(pin, num_array[number][j]); 
    pin++; 
  } 
}
3
  • The 2 dimensional array is holding the HIGH and LOW signal for each number to display in 7-segment. function "loop" is running through the first dimension to get the number from array, the function "Num_Write" actually is then setting the display segments from the second dimansion of the array. So all is just to display numbers 0 - 9 in a 7-segment display element Commented Oct 13, 2015 at 11:29
  • Urgh - obviously written by someone that really doesn't have a clue... 140 bytes of RAM to store what could be in 10 bytes of flash... Commented Oct 13, 2015 at 11:31
  • @FuaZe Const binary patterns is exactly what I use for this kind of thing. Commented Oct 13, 2015 at 13:42

1 Answer 1

2

Num_Write will map the output pins to display a number on the 7-segment display.

Please note that the schematic below is not the best, nor the proper way to connect a 7 segment display. It's intended to show the connection for illustrating purposes, not for actual wiring. Each segment lead should have it's own resistor. Also, depending on if you have a Common Anode or Common Cathode 7 segment, wiring will differ.

Image of a 7segment connected to a microcontroller

When the number 0 is being displayed (check in the num_array) all segments are lit, except for the last segment (segment G).

void Num_Write(int number) {                 //Write a number to the 7-segment display. 
  int pin= 2;                                //Take the starting (output) pin.
  for (int j=0; j < 7; j++) {                //Loop through all the segments of the given number.
    digitalWrite(pin, num_array[number][j]); //Write the output pin according to the pattern in the "num_array" for the given number.
    pin++;                                   //Go to the next pin.
  } 
}

So by looping through the pins, and looping through the array (while setting the pins) we can display a number on the 7 segment display.

Extra: The output will be somewhat like this, but then reversed (9 to 0) and withouth the A, B, C, D, E and F.

seven segment counting up

7
  • I know you probably didn't draw the schematic there, but you want to watch that resistor usage - a single resistor on the COM pin is very bad - you should mention that in your answer, or find a better schematic. Commented Oct 13, 2015 at 13:44
  • @Majenko, it does work though? ;D But yeah, I should add that indeed. It could draw too much power when you light up all segments at the same time? Or basically have different light intensities if you have more or less segments on. Commented Oct 13, 2015 at 13:46
  • The latter - plus the segments may light up at different intensities. In a worst case scenario you may even get a cascade failure and destroy your LED display. You need one resistor per segment to set the current through that individual segment. Commented Oct 13, 2015 at 13:48
  • 1
    Yup, also added that there is a difference in common anode and common cathode wiring. Commented Oct 13, 2015 at 13:53
  • 1
    I think single resistor configuration has its merits too. (1) The power draw will be the same as long as one or more of the segments are lit. This means less power as more segments are lit, but even with 1/7 or 1/8 of the brightness, the difference can still be very subtle depending on the selected resistor value. (2) The parts count is significantly less as the number of digits increase. For example, a 4 digit will require 4 resistors instead of 28/32 resistors. (3) As for the cascading failure: the resistor has to be replaced regardless of how many segments has failed. Commented Oct 15, 2015 at 22:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.