Lesson 110-5: 74HC595 Adjust Delay Sequence Time for a 7-Segment LED Display Using a Potentiometer
This code is to introduce the 74HC595 to drive 8 LED lights and turn them on from 0 to 8 one by one while keeping each LED on. In Project 5: Adjusting delay of each digit using a potentiometer To learn better or to have fun, I have added a potentiometer (or variable resistor) to the project so we can adjust the delay time between each digit (multiplexing) so we can see how each digit is turned on. For example, when we want to display 876, we first see 8, then a delay, then 7 with a delay, and then 6 with a delay, which can be adjusted using the variable resistor.
377-Arduino code for 74HC595 chip code 5/6: using potentiometer to control speed of sequences
语言: C++
/*
This is Arduino code to control speed of displaying each digit using potentiometer and learn better
Build up to 8 digits Seven-Segment display using 74HC595 Shift Register
Watch full video instruction on YouTube https://youtu.be/xhPXovgFhso
resource page and wiring diagram https://robojax.com/RTJ394
Written and explained by Ahmad Shamshiri on https://youtube.com/@robojax
written by Ahmad Shamshiri
www.Robojax.com https://youTube.com/@robojax
written 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[]= "CC"; //CA for common anode, CC for common cathode
int myDigits[DIGITS], digitsLength;
long timeToRemember, decimalPosition=NULL;
bool debug , isFloatingNumber;
int debugDelay = 0;//set the debugging value in milliseconds 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() {
showNumber(84321, 3000);
// showNumber(72, 3000);
// showNumberDecimal(1.654, 2000);
// showNumber(205, 3000);
// showNumberDecimal(90.548, 2000);
// for(int i=0; i < 131; i++)
// {
// showNumber(i, 100);
// }
// // //delay(2000);
// //delay(1);
}
/*
//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;
for(int i=DIGITS-1; i >=0; i--)
{
int divisionValue = (int)(pow(10, k)+.5);
myDigits[i] = (number/ divisionValue) % 10;
k++;
}
// myDigits[4]=number % 10 ;//147 gets 7
// myDigits[3]=(number / 10) % 10 ;//147 gets 4
// myDigits[2]=(number / 100) % 10 ;//147 gets 1
// myDigits[1]=(number / 1000) % 10 ;//147 gets 4
// myDigits[0]=(number / 10000) % 10 ;//147 gets 4
//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 =="CC")
{
segmentsToDisplay = ~segmentsToDisplay;//tottle 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);
}
int potValue = analogRead(A0);
int delayValue = map(potValue, 0, 1024, 0, 2000);
if(delayValue > 20)
{
delay(delayValue);
}
// Serial.print(" delayValue:");
// Serial.println(delayValue);
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]);
// digitalWrite(LATCH_STCP_PIN12, LOW);
// shiftOut(DATA_DS_PIN14, CLOCK_SHCP_PIN11, MSBFIRST, digitBits );
// shiftOut(DATA_DS_PIN14, CLOCK_SHCP_PIN11, MSBFIRST, digits[ myDigits[4]]);
// digitalWrite(LATCH_STCP_PIN12, HIGH);
// Serial.println(myDigits[4]);
// delay(3000);
// digitalWrite(LATCH_STCP_PIN12, LOW);
// shiftOut(DATA_DS_PIN14, CLOCK_SHCP_PIN11, MSBFIRST, digitBits << 1);
// shiftOut(DATA_DS_PIN14, CLOCK_SHCP_PIN11, MSBFIRST, digits[ myDigits[3]]);
// digitalWrite(LATCH_STCP_PIN12, HIGH);
// Serial.println(myDigits[3]);
// delay(3000);
// digitalWrite(LATCH_STCP_PIN12, LOW);
// shiftOut(DATA_DS_PIN14, CLOCK_SHCP_PIN11, MSBFIRST, digitBits << 2);
// shiftOut(DATA_DS_PIN14, CLOCK_SHCP_PIN11, MSBFIRST, digits[ myDigits[2]]);
// digitalWrite(LATCH_STCP_PIN12, HIGH);
// Serial.println(myDigits[2]);
// delay(3000);
// digitalWrite(LATCH_STCP_PIN12, LOW);
// shiftOut(DATA_DS_PIN14, CLOCK_SHCP_PIN11, MSBFIRST, digitBits << 3);
// shiftOut(DATA_DS_PIN14, CLOCK_SHCP_PIN11, MSBFIRST, digits[ myDigits[1]]);
// digitalWrite(LATCH_STCP_PIN12, HIGH);
// Serial.println(myDigits[1]);
// delay(3000);
// digitalWrite(LATCH_STCP_PIN12, LOW);
// shiftOut(DATA_DS_PIN14, CLOCK_SHCP_PIN11, MSBFIRST, digitBits << 4);
// shiftOut(DATA_DS_PIN14, CLOCK_SHCP_PIN11, MSBFIRST, digits[myDigits[0]]);
// digitalWrite(LATCH_STCP_PIN12, HIGH);
// Serial.println(myDigits[0]);
// delay(3000);
}
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();
// digitalWrite(LATCH_STCP_PIN12, LOW);
// shiftOut(DATA_DS_PIN14, CLOCK_SHCP_PIN11, MSBFIRST, B00000001);
// shiftOut(DATA_DS_PIN14, CLOCK_SHCP_PIN11, MSBFIRST, digits[d2]);
// digitalWrite(LATCH_STCP_PIN12, HIGH);
// delay(1);
// digitalWrite(LATCH_STCP_PIN12, LOW);
// shiftOut(DATA_DS_PIN14, CLOCK_SHCP_PIN11, MSBFIRST, B00000010);
// shiftOut(DATA_DS_PIN14, CLOCK_SHCP_PIN11, MSBFIRST, digits[d1]);
// digitalWrite(LATCH_STCP_PIN12, HIGH);
// delay(1);
}
//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 leading zeros
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 length 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
资源与参考
-
外部
文件📁
没有可用的文件。