HC7Seg - Library for 7 segment LED modules.

Useful guides, libraries, and example sketches to support our Arduino based products.
Post Reply
admin
Site Admin
Posts: 866
Joined: Sun Aug 05, 2012 4:02 pm

HC7Seg - Library for 7 segment LED modules.

Post by admin » Thu Oct 13, 2016 12:58 pm

Image



Arduino library for vairous types of 7 segment LED modules. The library is capable of driving a seven segment display from 1 to 8 digits, or combinations of modules making up to 8 digits. The module can also be of common anode or common cathode type.
Current supported products sold in our store:

1-DIGIT 7-SEGMENT LED MODULE SKU: HCOPTO0012
4-DIGIT 7-SEGMENT LED MODULE SKU: HCOPTO0013

The library makes use of the hardware timer 2 to refresh the display in the background leaving your sketch completely free to run your own code. It is includes an alphanumeric character set and with built in print commands both text and decimal numbers can easily be output to the display.



You will need to download (please log in to download the library) and unzip this library to the Arduino development environments library area.

On Windows:
My Documents\Arduino\libraries\

On Mac:
Documents/Arduino/libraries/

Linux:
Usually found within the users home area under /Arduino/libraries/



Using the HC7Seg library

To use the library just include the HC7Seg.h header file. E.g:

Code: Select all

#include "HC7Seg.h"

You will then need to create an instance of the library with the correct configuration for your module:

Code: Select all

HC7Seg HC7Seg(Type, SEG_A, SEG_B, SEG_C, SEG_D, SEG_E, SEG_F, SEG_G, SEG_DP, DIG_1, [DIG_2], [DIG_3], [DIG_4], [DIG_5], [DIG_6], [DIG_7], [DIG_8])
;

Where the first parameter Type is the type of module you are using and may be one of the following options:

COM_ANODE_1DIG (1 digit common anode display)
COM_ANODE_2DIG (2 digit common anode display)
COM_ANODE_3DIG (3 digit common anode display)
COM_ANODE_4DIG (4 digit common anode display)
COM_ANODE_5DIG (5 digit common anode display)
COM_ANODE_6DIG (6 digit common anode display)
COM_ANODE_7DIG (7 digit common anode display)
COM_ANODE_8DIG (8 digit common anode display)

COM_CATHODE_1DIG (1 digit common cathode display)
COM_CATHODE_2DIG (2 digit common cathode display)
COM_CATHODE_3DIG (3 digit common cathode display)
COM_CATHODE_4DIG (4 digit common cathode display)
COM_CATHODE_5DIG (5 digit common cathode display)
COM_CATHODE_6DIG (6 digit common cathode display)
COM_CATHODE_7DIG (7 digit common cathode display)
COM_CATHODE_8DIG (8 digit common cathode display)

SEG_A is the digital pin number that controls the segment A on your module
SEG_B is the digital pin number that controls the segment B on your module
SEG_C is the digital pin number that controls the segment C on your module
SEG_D is the digital pin number that controls the segment D on your module
SEG_E is the digital pin number that controls the segment E on your module
SEG_F is the digital pin number that controls the segment F on your module
SEG_G is the digital pin number that controls the segment G on your module
SEG_DP is the digital pin number that controls the decimal point on your module.

DIG_1 is the digital pin that controls the first digit (right most) on your module.
DIG_2 is optional digital pin that controls the second digit on your module.
DIG_3 is optional digital pin that controls the third digit on your module.
DIG_4 is optional digital pin that controls the forth digit on your module.
DIG_5 is optional digital pin that controls the fifth digit on your module.
DIG_6 is optional digital pin that controls the sixth digit on your module.
DIG_7 is optional digital pin that controls the seventh digit on your module.
DIG_8 is optional digital pin that controls the eighth digit on your module.



The following functions are available with this library:


Code: Select all

HC7Seg.print7Seg(Text, Offset, DP);
Prints a string of text to the display at a specified position where:
Text is the string or null terminated character array containing the text to print.

Offset is the digit position from where the text will start.
1 = the right most digit, 2 = second right most digit, etc. Characters can also be positioned off display.

DP is an optional parameter (default = false) which specifies if to use the seven segments DP led to represent full stops and decimal points. Valid values for DP are
false = full stops and decimal points will take up a whole digit on the display.
true = full stops and decimal points will use the DP segment on the preceding character/number.



Code: Select all

HC7Seg.print7Seg(value, Offset, DecimalPlaces, DP);

Prints a signed decimal number to the display where:

Value is the signed floating point number to display

Offset is the digit position from where the text will start.
1 = the right most digit, 2 = second right most digit, etc. Characters can also be positioned off display.

DecimalPlaces is optional and set number of decimal places to display the number to.

DP is an optional parameter (default = true) which specifies if to use the seven segments DP led to represent full stops and decimal points. Valid values for DP are
false = full stops and decimal points will take up a whole digit on the display.
true = full stops and decimal points will use the DP segment on the preceding character/number.



Code: Select all

HC7Seg.SetDigit(Digit, Data);
Writes a raw 8 bit value to the output buffer where:

Digit is the digit number for the display to change.
Valid values are from 1 to 8

Data is an 8 bit byte which directly sets the state of each segment of the specified digit.


Code: Select all

HC7Seg.clear();

Clears the output buffer and display (turns off all LEDs)



Image

Code: Select all

/* FILE:    HC7Seg_Example
   DATE:    12/10/16
   VERSION: 0.1
   AUTHOR:  Andrew Davies


21/01/16 version 0.1: Original version

This sketch demonstrates how to use the HC7Seg Arduino library to control
a 7 segment LED display. The library is capable of driving a seven segment
display from 1 to 8 digits, or combinations of modules making up to 8 digits.
The module can also be of common anode or common cathode type. This sketch is
configured for a 4 digit common cathode display see item SKU HCOPTO0013 in our
store hobbycomponents.com.

The HC7Seg library can be downloaded from the software section of our support
forum here:

http://forum.hobbycomponents.com/viewforum.php?f=58


You may copy, alter and reuse this code in any way you like, but please leave
reference to HobbyComponents.com in your comments if you redistribute this code.
This software may not be used directly for the purpose of selling products that
directly compete with Hobby Components Ltd's own range of products.
THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
REASON WHATSOEVER.
*/



#include "Arduino.h"
#include "HC7Seg.h"

/* Digital pins used to drive each digit and digit segment of your display. Please
   change these to match the connections to your module */
#define SEG_A 	2
#define SEG_B 	4
#define SEG_C 	8
#define SEG_D 	6
#define SEG_E 	7
#define SEG_F 	3
#define SEG_G 	9
#define SEG_DP 	5
#define DIG_1	19
#define DIG_2	12
#define DIG_3	11
#define DIG_4	10

//HC7Seg HC7Seg(COM_CATHODE_1DIG, SEG_A, SEG_B, SEG_C, SEG_D, SEG_E, SEG_F, SEG_G, SEG_DP, DIG_1); //1 digit common cathode example (e.g. HCOPTO0012)
HC7Seg HC7Seg(COM_CATHODE_4DIG, SEG_A, SEG_B, SEG_C, SEG_D, SEG_E, SEG_F, SEG_G, SEG_DP, DIG_1, DIG_2, DIG_3, DIG_4); //4 digit common cathode example (e.g. HCOPTO0013)
//HC7Seg HC7Seg(COM_ANODE_8DIG, SEG_A, SEG_B, SEG_C, SEG_D, SEG_E, SEG_F, SEG_G, SEG_DP, DIG_1, DIG_2, DIG_3, DIG_4, DIG_5, DIG_6, DIG_7, DIG_8); //8 digit common anode example


void setup()
{
}

void loop()
{
	/* Scroll some text */
	for(int i = 0; i <= 18; i++)
	{
		HC7Seg.clear();
		HC7Seg.print7Seg("HELLO WORLD !! ", i);
		delay(150);
	}

	/* Scroll an integer number */
	for(int i = 0; i <= 14; i++)
	{
		HC7Seg.clear();
		HC7Seg.print7Seg(-123.45, i, 2);
		delay(150);
	}
}



Image
HC7Seg.zip
You do not have the required permissions to view the files attached to this post.

Ian.G
Posts: 3
Joined: Tue Apr 27, 2021 9:49 pm

Re: HC7Seg - Library for 7 segment LED modules.

Post by Ian.G » Sun May 16, 2021 5:19 pm

I would appreciate some help with the library for 7 segment displays: HC7Seg.
I am reading date and time from a RTC module (HCMODU0124) and I can compile the data into a string which I can print to the serial port. My trouble comes in sending the string to a 4 digit seven segment display (HCOPTO0013). I am attaching three little test sketches which I hope illustrate the problem.

If I send a character array to the display, that works (see 4_x_7_Sg_Test_1 .ino).
If I include a string in quotes and send that to the display, again that works (see 4_x_7_Sg_Test_2.ino).
If I send a string variable - it fails to compile (see 4_x_7_Sg_Test_3.ino). no matching function for call to 'HC7Seg::print7Seg(String&, int&)'

Any guidance would be gratefully received.
Ian.G
You do not have the required permissions to view the files attached to this post.

andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: HC7Seg - Library for 7 segment LED modules.

Post by andrew » Mon May 17, 2021 8:06 am

In the Arduino environment there are two separate ways to handle text: Strings (large S) and strings (small s). You're getting an error because the library expects the small s type and you are passing a large S type string to it.

Strings with a small s are just character arrays with a null terminator character and the end of the text and are part of standard c.

Strings with a large S were added by Arduino to try and make handing text easier for beginners. The main problems with them though is that if you don't know how to use them correctly you can can end up with memory fragmentation and out of memory errors. They also take up a lot more memory then the standard string type. So if you're not committed to using them I'd advise avoiding them and learning how to use the standard c string type. Fortunately there are a few standard c functions that will help you manipulate c strings:

strcpy() copies one string to another
strncpy() copies a specified number of characters from one string to another
strcat() concatenates two strings
strncat() concatenates a specified number of characters from one string to another
strcmp() compares two strings
strncmp() compare the first specified number of characters between two strings

The main thing to remember when using strings is to make sure the character array used to hold the text you copy into it is big enough to hold the text plus a single null terminator character ('\0') at the end.

But if you still want to stick with Strings then there is a function that will allow you to convert text from a String to a string. I've not tested it but the following should work:


  1. /* Example String (large S)*/
  2. String testString = "Hello World";
  3.  
  4. /* Character array to hold the text as a string (small s). Note the array is 1 character bigger to hold the null terminator. */
  5. char testCharArray[12];
  6.  
  7.  
  8. void setup()
  9. {
  10.   /* Convert from String to string */
  11.   testString.toCharArray(testCharArray, sizeof(testCharArray));
  12. }
  13.  
  14. void loop()
  15. {
  16.   /* Scroll some text */
  17.   for(int i = 0; i <= 18; i++)
  18.   {
  19.     HC7Seg.clear();
  20.     HC7Seg.print7Seg(testCharArray, i);
  21.     delay(150);
  22.   }
  23. }
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

Ian.G
Posts: 3
Joined: Tue Apr 27, 2021 9:49 pm

Re: HC7Seg - Library for 7 segment LED modules.

Post by Ian.G » Mon May 17, 2021 5:11 pm

Andrew,
Thank you for kind assistance.
I now understand that I was using the wrong method of handling strings. I will have another go at my sketch.
It is encouraging to understand why I was failing, and to be guided towards the correct route.
Thanks again for your help. Regards, Ian

Post Reply

Return to “Arduino”