Back to Step by Step Course by Robojax
If you don't like to see ads while video is being played you can purchase YouTube premium Here
Please select other codes for this lecture from the links below.
Related or required files and link to this lesson
This vode is to introduct the 74HC595 to driver 8 LED lights and turn them ON from 0 to 8 one by one while keeping each LED ON.
In Project 6: Measuring DC voltage and dispalying on the LED display Now have a up to 8 digists seven sgment dispaly, it is time to measure DC voltage of Arduino and dispay it on our display. We used a veriable resistor to change the votlage and see the effect immediatly on the display.
/*
This code is to driver two Digits Seven Segment display using two pices of 74HC595
Watch full video instruction on YouTube https://youtu.be/xhPXovgFhso
written and explained by Ahmad Shamshiri on https://youtube.com/@robojax
*/
/*
This is arduino sketch to use 74HC595 shift register to drive 2 to 8 LED Seven Segment Display
it can display integer like 38765 or floatping point number such as 345.7162
written by Ahmad Shamshiri
www.Robojax.com https://youTube.com/@robojax
writen on March 9 to March 16, 2024
*/
int CLOCK_SHCP_PIN11 = 12;//SHcp pin of 74HC595
int LATCH_STCP_PIN12 = 11;//STcp pin of 74HC595
int DATA_DS_PIN14 = 10;//ds pin of 74HC595
#define DIGITS 5
#define FLOAT_PRECISION 3
#define SHOW_ZERO false
const char type= 'C'; //A for common anode, C for common cathode
int myDigits[DIGITS], digitsLength;
long timeToRemember, decimalPosition=NULL;
bool debug , isFloatingNumber;
int debugDelay = 0;//set the debugging value in milliseonds like 2000. to turn OFF set it to zero
byte digits[] = {B00111111, //0
B00000110, //1
B01011011, //2
B01001111, //3
B01100110, //4
B01101101, //5
B01111101, //6
B00000111, //7
B01111111, //8
B01101111 //9
};
void showNumber(long, long);
int intLength(long);
void setup() {
Serial.begin(9600);
pinMode(CLOCK_SHCP_PIN11, OUTPUT);
pinMode(LATCH_STCP_PIN12, OUTPUT);
pinMode(DATA_DS_PIN14, OUTPUT);
digitalWrite(CLOCK_SHCP_PIN11, LOW);
digitalWrite(LATCH_STCP_PIN12, LOW);
digitalWrite(DATA_DS_PIN14, LOW);
if(debugDelay > 0)
{
debug= true;
}else{
debug= false;
}
}
void loop() {
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
showNumberDecimal(voltage, 50);
}
/*
//code has been take from
https://forum.arduino.cc/t/digit-brightness-issue-7-segment-4-digit-display-2-x-74hc595/658032/16
and modified
* @brief shows number on the Seven Segment Display
* @param "num" is integer like 5, or 78 to display
* @param "t" is the delay time to display the digit.
* @return returns none
* Usage to show 18 for 2000 milliseconds: showNumber(18, 2000);
* Written by Ahmad Shamshiri on 08 Mar 2024
* in Ajax, Ontario, Canada
* www.Robojax.com
*/
void showNumber(long number, long t)
{
timeToRemember = millis();
isFloatingNumber = false;
digitsLength = intLength(number);//get the length of digits needed for leading zeros removal
int k=0;
//5658
for(int i=DIGITS-1; i >=0; i--)
{
int divisionValue = (int)(pow(10, k)+.5);
myDigits[i] = (number/ divisionValue) % 10;
k++;
}
// myDigits[4]=(number/1) % 10 ;//59847 gets 7
// myDigits[3]=(number / 10) % 10 ;//59847 gets 4
// myDigits[2]=(number / 100) % 10 ;//59847 gets 8
// myDigits[1]=(number / 1000) % 10 ;//59847 gets 9
// myDigits[0]=(number / 10000) % 10 ;//59847 gets 5
//Serial.println(number);
while( millis() - timeToRemember < t)
// int k=0;
// while( 1)
{
// k++;
shiftDigits();
}
}
void shiftDigits()
{
int digitBits =B11111110;
int leadingZeros = DIGITS - digitsLength;
if(debug)
{
Serial.print("digitsLength:");
Serial.print(digitsLength);
Serial.print(" leadingZeros :");
Serial.println(leadingZeros);
}
for(int i=0; i< DIGITS+1; i++)
{
//Serial.print("Digit: ");Serial.println( myDigits[i]);
//delay(1000);
}
for(int i=0; i < DIGITS; i++)
{
switch (i)
{
case 0:
digitBits =B11111110;
break;
case 1:
digitBits =B11111101;
break;
case 2:
digitBits =B11111011;
break;
case 3:
digitBits =B11110111;
break;
case 4:
digitBits =B11101111;
break;
default:
break;
}
if(i == decimalPosition && isFloatingNumber)
{
digits[myDigits[i]] |= (1 << 7);//set 7th bit to 1 to display decimal
}
//digits[myDigits[i]] |= (1 << 7);//set 7th bit to 1 to display decimal
// Serial.print("myDigits[");
// Serial.print(i);
// Serial.print("] =");
// Serial.println(myDigits[i]);
//this loop below is to turn off the leading zeros if set the at the top of the code
int segmentsToDisplay =digits[ myDigits[i]] ;//remember the segments value for leading zero removal
if(!SHOW_ZERO)
{
for(int k=0; k < leadingZeros; k++)
{
if(i==k)
{
segmentsToDisplay =B00000000;//turn off all segments
break;
}
}
}
if(type =='A')
{
segmentsToDisplay = ~segmentsToDisplay;//toggle all bits for common anode display type
}
if(debug)
{
Serial.print("i-:");
Serial.print(i);
Serial.print(" digitBits:");
Serial.print(digitBits, BIN);
Serial.print(" segmentsToDisplay:");
Serial.println(segmentsToDisplay, BIN);
Serial.print(" digits[ myDigits[");
Serial.print(i);
Serial.print("]]) = ");
Serial.println(digits[ myDigits[i]], BIN);
}
digitalWrite(LATCH_STCP_PIN12, LOW);
shiftOut(DATA_DS_PIN14, CLOCK_SHCP_PIN11, MSBFIRST, digitBits);
shiftOut(DATA_DS_PIN14, CLOCK_SHCP_PIN11, MSBFIRST, segmentsToDisplay);
digitalWrite(LATCH_STCP_PIN12, HIGH);
delay(1);
if( i == decimalPosition && isFloatingNumber)
{
digits[myDigits[i]] ^= (1 << 7);//toggle the decimal bit to OFF
}
if(debug)
{
delay(debugDelay);
}
}
// int digitBits =B00100000;//keeps all digits ON with ditigtBits << 1 for [3] and << 2 for [2]
//digitBits =B00000001;
// Serial.print(number);
// Serial.print("digitBits:");
// Serial.print(digitBits, BIN);
// Serial.print(" for digit0: ");
// Serial.println(myDigits[0]);
}
void showNumberDecimal(float floatNumber, long t)
{
timeToRemember = millis();
isFloatingNumber = true;
// //if we have 2.8
// int d2 = num;//2.8 becomes 2
// int d1 = (int) (num *10);
// d1 = d1 % 10;// gets 8
// digits[d2] |= (1 << 7);//set 7th bit to 1 to display decimal
floatToInt(floatNumber);
if(debug)
{
Serial.print("Decimal position:");
Serial.println(decimalPosition);
}
while( millis() - timeToRemember < t)
{
shiftDigits();
}
//digits[d1] ^= (1 << 7);//toggle the decimal bit to OFF
}
void floatToInt(float floatNumber)
{
char charNumber[DIGITS+1];//create char array for one extra char for decimal place
dtostrf(floatNumber, DIGITS+1, FLOAT_PRECISION, charNumber);//
//now charNumber holds the float value as char array
String stringNumber =charNumber;//convert it to String to amke it easy to work with
if(debug)
{
Serial.print("===== ");Serial.print(floatNumber, FLOAT_PRECISION); Serial.println(" ===");
Serial.print("charNumber: ");
Serial.println(charNumber);
Serial.print("stringNumber: ");Serial.println(stringNumber);
}
////get the position of decimal place
for(int i=0; i< DIGITS+1; i++)
{
if(charNumber[i] =='.')
{
decimalPosition =i;//get the position of decimal point
break;
}
}
//this array is used to compare each char in the stringNumber variable
//so we assign integer for that value
char charDigits[]={'0','1','2','3','4','5','6','7','8','9'};
stringNumber.remove(decimalPosition, 1);//remove . the decimal point from string
decimalPosition--;//after removing the decimal point, then we should subtract the position
if(debug)
{
Serial.print("stringNumber(. removed): ");Serial.println(stringNumber);
}
digitsLength =0;//need this to turn off leadzing zeros
// "8763"
for(int i=0; i< stringNumber.length(); i++)
{
//check each stringNumber.charAt(i) agains charDigits[n]
for(int n=0; n <=9; n++)
{
if(stringNumber.charAt(i) ==charDigits[n] )
{
myDigits[i]=n;//save the digit in the array
digitsLength++;
break;
}
}//inner loo end
}//for loop end
}//floatToInt end
/*
gets length of long integer
@param number is long integer to the its length
@return the lenght from 0 to 9
*/
int intLength(long number)
{
int Length = 0;
if(number > 99999999)Length = 9;
else if(number > 9999999)Length = 8;
else if(number > 999999)Length = 7;
else if(number > 99999)Length = 6;
else if(number > 9999)Length = 5;
else if(number > 999)Length = 4;
else if(number > 99)Length = 3;
else if(number > 9)Length = 2;
else if(number < 9 && number > 0)Length = 1;
else Length = 0;
return Length;
}//intLength ends
The least I expect from you is to thumb up the video and subscribe to my channel. I appriciate that. .I have spent months making these lectures and writing code. You don't lose anything by subscribging to my channel. Your subscription is stamp of approval to my videos and more people can find them and in it turn it helps me. Thank you
If you found this tutorial helpful, please support me so I can continue creating content like this. support me via PayPal
**** AFFILIATE PROGRAM **** We are a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for us to earn fees by linking to Amazon.com and affiliated sites.