7 Segments Display Librairie


I have done a 7 segments display librairie for arduino.(sorry if there is already one)

It gives an easier way to display a number or a character.

Segment object(pinA,pinB,pinC,pinD,pinE,pinF,pinG,pinDP)

object.display(number) or object.display('letter')

It’s my first librairie, so if you have any comments, feel free to say it :slight_smile:


Segment.zip (2.05 KB)

Thanks for sharing, - there may already be a dozen, but this one is yours and that is what counts!

Some comments in no particular order

  • I’m missing a version number , important if you get customer feedback.

  • the pins only need an uint8_t instead of unsigned int - both parameters as internal (smaller footprint)

  • you could place the internal pins in an array to minimize code-size
    e.g. for (uint8_t i=0; i<7; i++) pinMode(_pin*, OUTPUT);*
    - you could wrap the 8 lines of digitalWrite in a function with 8 parameters,

  • will reduce the footprint.*
    - I would create an array of bytes that represent the HIGH LOW patterns for the chars supported
  • and in combination with the array of pins you can make the lib substantial smaller.*
  • The 8 bits in the byte replaces the 8 parameters in show()*
    *uint8_t  ar[37]  = {    // 36 == { 0…9 ,  a…z } + 1 error
            b11111100,  // 0
            b01100000,    // 1
            }  // 1 = high  0 = low

chars that cannot be made e.g. K  can have an error bit pattern e.g | |

boolean Segment::display(unsigned char character)
  // 0…9
  if (character < 10)
    show( ar[character] ); 
    return true;
  // 0 … 9 as char
  if ( ‘0’<= character && character <= ‘9’)
    uint8_t idx = character - ‘0’;  // map on 0…9
    show( ar[idx] );
    return true;
  // small
  if ( ‘a’ <= character && character <= ‘z’)  // display the pattern for char  (which can be the error pattern)
    uint8_t idx = character - ‘a’ + 10;  // map on 10…35
    show( ar[idx] );
    return true;
  // capitals - > map on the ‘a’… ‘z’
  // todo code

// handle error here
  return false;

void show( uint8_t b)
  for (uint8_t i = 0; i< 8; i++)
    digitalWrite(_pin[i],  b & (1 << i) ); // this is some bit magic  that checks if bit  i  is 1 in byte b.
// can also be 8 digitalWrite() statements*
- this is no good C

  • if(character!=0||1||2||3||4||5||6||7||8||9||‘a’||‘A’||‘b’||‘B’||‘c’||‘C’||‘d’||‘D’||‘e’||‘E’||‘f’||‘F’||‘h’||‘H’||‘j’||‘J’||‘l’||‘L’||‘o’||‘O’||‘p’||‘P’||‘s’||‘S’){*
    - better not to use delay in the library as it blocks all code.
  • The function returns false (as it should) and the user of the library can create his own error handling.*
    This seems like a lot of remarks but this way you can improve your library skills.
    Keep the existing lib as a reference, so you can see the improvements.
    If there are unclear remarks, just reply and I’ll notice

update: + you should also add one or more demo sketches (+ electric schema) how to use the library

Thank you for your feedback. I will try to do a new one as soon as I can. :)

However, I don't understand the function 'show()'. There is no reference on the arduino website.

ElectrikMan: Thank you for your feedback. I will try to do a new one as soon as I can. :)

However, I don't understand the function 'show()'. There is no reference on the arduino website.

you need to write it yourself :) It is an idea to encapsulate all those digitalWrite()'s in a self written function to minimize the code size of the library.

And in fact, I think show() is not a very good name

Ok thank you very much :smiley: