# how to convert int value into a 4 digit value

i am creating a counter for my 7 seg display and so it will stop at zero and so I can use the blink without delay tutorial, I am making the number to be displayed a variable. I was wondering if there was a way to convert a variable's value to be 4 digits

Do you mean if your variable is 1234, you want digit1 =1, digit2 = 2, digit3 = 3 and digit4 = 4?

Modulus Operator in C and C++ - Programming Tutorials - Cprogramming.com. Hint: think in terms of 1000s, 100s, 10s, and 1s.

That link doesn't work, and I want if the number was 27, it to be converted to something like 0027 or --27

Follow the redirect link, it goes there. By the way, there are a boatload of seven segment libraries that have code for this. You can use the library, or just copy the code.

It shows how to divide numbers and check if they are divisible by 2, not how to convert numbers to four digits.

Arodd2000:
It shows how to divide numbers and check if they are divisible by 2, not how to convert numbers to four digits.

That what you do. If you divide by ten then the remainder is the ones digit. Divide by ten again and the remainder is the tend digit. Etc etc. look to the modulo operator to find that remainder.

The whole enchilada:http://forum.arduino.cc/index.php?topic=316731.0(look towards the end of the posts)
Did you investigate 7 segment libraries, as I suggested?

Arodd2000:
It shows how to divide numbers and check if they are divisible by 2, not how to convert numbers to four digits.

Remember in elementary school when your learned about the 1's place and the 10's place and the 100's place etc.

Or were you never taught?

I figured out that I can change the position of the cursor on the display. I don't get a library because it is a sparkfun serial display. Here is the code I use.

``````s7s.write(0x76); //clear display
s7s.write(0x79); //set cursor
s7s.write(0x2); //cursor position
s7s.write(timeansw/10); //write the tens place of time
if (timeansw >= 10){ //check if time left is more than 10
s7s.write(0x79); //set cursor
s7s.write(0x3); //cursor position
s7s.write(timeansw - 10); //write the one's place of time
} else{ //if time left is less than 10
s7s.write(0x79); //set cursor
s7s.write(0x3); //cursor position
s7s.write(timeansw); //set time
``````

You don't have to specify your cursor position in hex. Also, if you had looked at (or even run) the example sketches that the Sparkfun library provides, such as this one: Serial7SegmentDisplay/S7S_Example_I2C_Basic.ino at master · sparkfun/Serial7SegmentDisplay · GitHub, you would have quickly seen that it has exactly the code you were looking for. You should have a look, because it's better than what you wrote.

Arodd2000:
i am creating a counter for my 7 seg display and so it will stop at zero and so I can use the blink without delay tutorial, I am making the number to be displayed a variable. I was wondering if there was a way to convert a variable’s value to be 4 digits

``````    uint8_t val;
val = ((value / 1000) % 10);
val = ((value / 100) % 10);
val = ((value / 10) % 10);
val = ((value / 1) % 10);

// val == 1000's place
// val == hundreds place
// val == tens place
// val == ones place
``````

Then use an ISR with a uint8_t index and each time the ISR is called, do something like this:

digit [idx % 4] = segments [val [idx % 4]];
idx++;

You see? “idx” will go from 0 to 0xFF, then loop around again, but using it with “%4” will give you 0,1,2,3,0,1,2,3… etc… and “scan” through each of the 4 seven segment displays, putting the proper value (i.e. val [idx % 4]) into each one.

The ISR just runs, and each time you update “value” and call the code above, the display matches it.

Make sense?