Breaking Digits Down

I'm drawing a blank on this. I used to do it in BASIC (many moons ago), but now I'm thrown off.
I just want to grab each of the digits individually from an analog read.
Can I do it mathematically, or do I have to convert to Char form?

I know it's simple, but I'm outputting data on a single 7-seg display.

Maybe that's why I'm not getting any feedback.

Thanks!

The analogRead(A3) returns 16-bit data with upper 6-bit always zeros. That means that the actual data is 10-bit, and it can range from 000 to 3FF in hexadecimal number system (0 to 1023 in decimal number system). If you want to show them on 7-segment display devices at the same time, you need 3/4 display devices. If you have only one 7-segment display device, then you have to show the digits one after another with a time gap (say, 500 ms) among them.

You can use one or more of the following approaches to strip out the digits from the number (3FF/1023):
1. Use right shift method to extract hex digits.
2. Use %16 and /16 operations to extract hex digits.
3. Use %10 and /10 operations to extrcat decimal digits
4. Use itoa(int, buffer, base) function to extract ASCII codes for the digits of the number in hex/decimal.

The modulus operator (%) gives you the remainder when dividing by a number. (n % 10) will give you the least significant digit of n.
(n / 10) will strip off that digit, and the usual way to convert a number to individual digits (whether those are char or binary) is a loop that does successive modulus and division operations.

Original number: 31416
31416 % 10 = [color=red]6[/color]
31416 / 10 = 3141
 3141 % 10 = [color=red]1[/color]
 3141 / 10 = 314
  314 % 10 = [color=red]4[/color]
  314 / 10 = 31
   31 % 10 = [color=red]1[/color]
   31 / 10 = 3
    3 % 10 = [color=red]3[/color]
    3 / 10 = 0   [color=limegreen]done![/color]

westfw:
The modulus operator (%) gives you the remainder when dividing by a number. (n % 10) will give you the least significant digit of n.

Original number: 31416
31416 % 10 = 6
31416 / 10 = 3141
3141 % 10 = 1
3141 / 10 = 314
314 % 10 = 4
314 / 10 = 31
31 % 10 = 1
31 / 10 = 3
3 % 10 = 3
3 / 10 = 0 done!

Golly and Gosh :slight_smile:

If you just need the digit characters, why not an itoa() call and then just pick off what you need. If you need them as numerics, just subtact '0':

  • val = strResult[0] - '0';*