Converting an int into separate bytes (segment display)

Hey there.

Am using a 4 digit, 7seg display with a 74HC595 bitshifter.

I have seen examples of where people have used a bitshifter for each digit.
Which seems overkill.

I have it displaying random numbers without any issues of flickering.

However i want to use a analogRead varible and display that.

Anyone have any tips on how to approach this?

This is a common anode display, so i am sending the bits to the shifter then setting pin high and then low on the anode (with a small delay) before moving to the next digit.

so some how getting an int 1024 to be a char 1, char 0, char 2, char 4?

Any help would be appreciated.

Try this? It should split 1024 into {'1', '0', '2', '4'}, but it's not tested whatsoever.

char digits[4];
int val = analogRead(0);

for (uint8_t i = 0; i < 4; i++) {
  digits[i] = (val / (10 * (3 - i))) + '0';
  val %= 10 * (3 - i);
}

Thanks, when i print out the binary with your code it seems to be doing what i want.

The way my code works is it uses this character set

const byte ledCharSet[10] = 
{
  	// Binary Digits
  	B01111110, B00110000, B01101101, B01111001,	// 0123
  	B00110011, B01011011, B01011111, B01110000,	// 4567
  	B01111111, B01111011       // 89     
};

hence if i want a '3' to display i call bitShift(ledCharSet[3])

So i am having trouble linking your digits[x] to this ledCharSet.

Oh. You implied you wanted characters - just remove the + '0' from the digits line - that converts it from the number 0 to the character '0', and you need the number to use as an index.

Thanks for your help Aeturnalis,

I managed to get it sorted just before your reply.

 int digits[4] ;
  
  for (int i = 3; i >= 0; i--) {
    digits[i] = val % 10;
    val /= 10;
  }

then used that to access the array of digits.

I tried yours (without the zero) for test sake, but it makes the digits go into all sorts of weird characters.
(Im assuming it is due to passing a char instead of a byte to my shift function -> might try out some bitwise operators to get it working.

Thanks again