I wrote some simple code to test every segment of a 4 Digit 7 Segment LED Display. I know it is pretty basic, but I am proud of myself, and that is all that I care about. ![]()
// About Me:
//-------------------------------------------------------------------------------------------------------------------
// This code was written by Jeremy Carlson
// Known as Jeremy1998 on Arduino Forums
// Email me at jeremyjcarlson@yahoo.com for help with code, hardware, or just to chat.
//-------------------------------------------------------------------------------------------------------------------
// About The Code:
//-------------------------------------------------------------------------------------------------------------------
// This code is written for use with displays connect with NPN Transistors.
// It can, however, easily be modified for use with direct drive of either common cathode or common annode displays.
// I will most likely be making updates for testing the colon and decimal.
// If I do, That will be posted on the Arduino Forums as well.
//-------------------------------------------------------------------------------------------------------------------
const byte digit[4] = {13, 12, 11, 10}; // Four digits on pins 13 - 10
const byte segment[7] = {9, 8, 7, 6, 5, 4, 3}; // Seven segments on pins 9 - 3
int time1 = 500; // A variable for storing the delays in void digits
int time2 = 100; // A variable for storing the delays in void segments
int repeatdigits = 5; // A variable for how many times void digits is called out
int repeatsegments = 5; // A variable for how many times voif segments is called out
int digitcount; // A variable for storing what digit is on
int segmentcount; // A variable for storing what segment is on
int i; // A variable for calling out void digit and void segment
void setup()
{
for (int i = 0; i < sizeof(digit) / sizeof(digit[0]); ++i) { // Calculates all pins in the digit array
pinMode (digit[i], OUTPUT); // Declares them as outputs
}
for (int i = 0; i < sizeof(segment) / sizeof(segment[0]); ++i) { // Calculates all pins in the segment array
pinMode (segment[i], OUTPUT); // Declares them as outputs
}
}
void digits()
{
for (i = 0; i < 4; i++) { // Repeats the following four times
digitcount + 1; // Adds one to the digitcount variable
if(digitcount == 5){ // If digitcount is 5
digitcount = 1; // Reset digitcount to 1
}
for (int i = 0; i < sizeof(segment) / sizeof(segment[0]); ++i) { // Calculates all pins in the segment array
digitalWrite(segment[i],HIGH); // Turns them all on
}
digitalWrite(digit[digitcount], HIGH); // Turns digit X on
delay(time1); // Waits for X milleseconds
digitalWrite(digit[digitcount], LOW); // Turns digit X on
delay(time1); // Waits for X milleseconds
for (int i = 0; i < sizeof(segment) / sizeof(segment[0]); ++i) { // Calculates all pins in the segment array
digitalWrite(segment[i],LOW); // Turns them all off
}
}
}
void segments()
{
for (i = 0; i < 7; i++) { // Repeats the following 7 times
segmentcount + 1; // Adds one to segmentcount variable
if(segmentcount == 8){ // If segmentcount is 8
digitcount + 1; // Reset digitcount to 1
segmentcount = 1; // Reset segmentcount to 1
}
digitalWrite(digit[digitcount], HIGH); // Turns digit X on
digitalWrite(segment[segmentcount], HIGH); // Turns segment X on
delay(time2); // Waits for X milleseconds
digitalWrite(digit[digitcount], LOW); // Turns digit X off
digitalWrite(segment[segmentcount], LOW); // Turns segment X off
}
}
void loop()
{
for (i = 0; i < repeatdigits; i++) { // Repeat the following X times
digits(); // Call out void digits
digitcount = 0; // Reset digitcount to 0
}
for (i = 0; i < repeatsegments; i++) { // Repeat the following X times
segments(); // Call out void segments
digitcount = 0; // Reset digitcount to 0
segmentcount = 0; // Reset segmentcount to 0
}
}
EDIT 1: I am noticing some errors in this code ( :-[). I am Currently fixing them and I will update the code as I find the fixes.
EDIT 2: It SHOULD be all fixed... Email me at the adress in the code, or leave a comment here if you notice any issues.