I want to Split a 4 digit integer in its Parts, so for example 3457 to 3 and 4 and 5 and 7, so that i can show it on a 7 segment 4 digit display. How do i do this?
Use integer division by 10 and the "%" (modulus) operator.
3547%10 = 7
3547/10 = 354
354%10 = 4
etc.
What is the variable type that holds the alleged four digit number?
You can use integer division with powers of 10
3547 / 1000 = 3
3547 - 3 * 1000 = 547
547 / 100 = 5
547 - 5 * 100 = 47
and so forth. This will fail if you go past 9999.
HTH
a7
int n = 3547; // or whatever number
// split number n into its digits
byte ones = n % 10;
byte tens = (n / 10) % 10;
byte hund = (n / 100) % 10;
byte thou = (n / 1000) % 10;
why not a simple
int number = 3457;
display.print(number);
Assume that you have the following 4-digit cc-type 7-segment Display Unit (Fig-1) and you want to show 3457 over there.
Figure-1:
Given:
int y = 3457;
1. Perform %10 (modulus) operation on y to get remainder 7.
byte indexDP3 = y%10; // indexDP3 = 7
2. Divide y by 10 to get quotient 345.
y = y/10; //y = 345
Repeat the above process until quotient is 0.
3. Sketch to find 3 4 5 7 and save in an array.
int y = 3457;
byte indexDP[4];
int i = 3;
void setup()
{
Serial.begin(9600);
do
{
indexDP[i] = y%10; //indexDP[3] = 7, indexDP[2] = 5, ....
y = y/10;
i--;
}
while(y !=0);
for(int j = 0; j < 4; j++)
{
Serial.print(indexDP[j], HEX);
Serial.print(' ');
}
}
void loop()
{
}
Output:
3 4 5 7
4. Use the value of indexDP[0] (3 = 0x03) to consult the following array (which contains cc-codes vs digit) to get cc-code of 3 and show it on DP0 position of the display unit.
byte lupTable[] =
{
0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, //0, 1, 2, 3, 4, 5, 6, 7
0x7F, 0x6F, 9x77, 0x7C, 0x39, 0x5E, 0x79, 0x71 //8. 9, A, b, C, d, E, F
};
Codes:
byte ccDP0 = lupTable(indexDP[0]); //ccDP0 = 0x4F ;; cc-code of 3
PORTB = ccDP0; //lower 6-bit goes to Display unit for segments: f, ..., a
digitalWrite(6, bitRead(ccDP0, 6)); bit-6 of ccDP0 goes to segment g of display unit
digitalWrite(7, bitRead(ccDP0, 7)); bit-67 of ccDP0 goes to segment p of display unit
PORTC = 0b111110; //3 appears on DP0 position of display unit;
delay(10);
5. If you use SevSeg.h Library, the tasks of Step-3/4 will be done by the Library. You may study the example of this thread.
Perhaps the following will work!
#include<SevSeg.h>
SevSeg sevSeg;
int number = 3457;
sevSeg.setNumber(number, 0, 0);
sevSeg.refreshDisplay();
Wait, is it 3457 or 3547?
a7
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.