Pages: [1]   Go Down
Author Topic: Converting an int into separate bytes (segment display)  (Read 710 times)
0 Members and 1 Guest are viewing this topic.
New Zealand
Offline Offline
Newbie
*
Karma: 0
Posts: 20
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
 
Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 360
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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);
}
Logged

New Zealand
Offline Offline
Newbie
*
Karma: 0
Posts: 20
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Code:
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
  • to this ledCharSet.
Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 360
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

New Zealand
Offline Offline
Newbie
*
Karma: 0
Posts: 20
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for your help Aeturnalis,

I managed to get it sorted just before your reply.

Code:
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
« Last Edit: May 30, 2011, 03:04:46 am by aKiwi » Logged

Pages: [1]   Go Up
Jump to: