Pages: [1]   Go Down
Author Topic: Convert a decimal number into a String?  (Read 2719 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Yarrrrrrrrduinooooooo!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey there, I'm trying to build a basic counter as a test for figuring out how to interface with multiple 7-segment LEDs.

I've got a basic setup running as per the tutorial on the arduino site, and can send arbitrary character bytes to both LEDs, and have created a basic lookup table for characters 0 to 9, and blank (all segments off).

What I'm trying to do now is increment a variable (the counter), and extrapolate the appropriate bytes to shift out based on the individual numerals that make up the counter decimal number.

For example, if I'm trying to write the number "72" to the LEDs, I'd like to  be able to convert the number 72 to a string, grab the digits one-by-one using some kind of string operation, convert each single digit individually back into an integer,  use that integers to assign the appropriate byte from the lookup table, and repeat until all of the digits have been shifted into the shift registers.

Problem is (and I find this SUPER surprising), I'm discovering that Arduino has no convenient way to either:  a) convert an integer to a string, or b) grab a substring of a given string.  

Most of the programming I've done has been using flash/actionscript and I guess I sort-of took for granted that Arduino/C++ would have similar string operation functions and/or integer to string conversion (arduino seems to map converted integers to an ascii character value or something instead of converting the digits literally)

Which leads me to 3 possible conclusions:

1. I'm missing something with regards to working with ints/strings in the reference material

2. I'm thinking about this problem all wrong, and there's another, perfectly logical and straightforward way to take a decimal number and extrapolate the appropriate bytes to send to the shift registers

3.  Both of the above..!

Off the top of my head it occurs to me that I might be able to solve this problem by running a series of cascading incrementing variables that count from 0-9 (one variable for each shift numeral I'm trying to display), but that seems a bit... hacky and indirect.  Being able to display an arbitrary multi-digit integer directly seems a lot more elegant and useful.

Any advice would be deeply appreciated!

-T   :-?
« Last Edit: August 22, 2009, 12:14:07 am by timichango » Logged

Safety Third!

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25861
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Problem is (and I find this SUPER surprising), I'm discovering that Arduino has no convenient way to either:  a) convert an integer to a string, or b) grab a substring of a given string.

Problem is, the Arduino has very little memory, and a full implementation of sprintf, with all those left/right justification formats, hex/decimal/string formats is very memory-hungry, as is general string handling. But, it does have these functions.

However, yes, there are other ways around these, not at all hacky.
Constant tables spring to mind.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Edison Member
*
Karma: 3
Posts: 1001
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There is no need to use strings for this. Converting an integer to an array of individual digits can be accomplished as follows:

Code:
#define NUM_DIGITS 4

// table of 7-segment patterns
const byte seg_def[]  = // segment patterns for digits 0-9
  {//GFEDCBA
    B0111111,  // '0'
    B0000110,  // '1'
    B1011011,  // '2'
    B1001111,  // '3'
    B1100110,  // '4'
    B1101101,  // '5'
    B1111101,  // '6'
    B0000111,  // '7'
    B1111111,  // '8'
    B1100111   // '9'
  };

byte digit_array[NUM_DIGITS];

void NumberToArray(word number)
{
  char i;

  // initialize all digits to the empty pattern (no segments lit)
  for (i=0;i<NUM_DIGITS;i++) digit_array[i]=0;

  // extract each digit (right to left)
  for (i=NUM_DIGITS-1;i>=0;i--) {
    // extract least significant digit as an index into the
    // segment pattern table
    digit_array[i]=seg_def[number%10];
    // get next digit
    number=number/10;
    // exit if there are no more digits to avoid leading zeroes
    if (number==0) break;
  }/*for*/
}/*NumberToArray*/

Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Yarrrrrrrrduinooooooo!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks so much for the help - the code above works marvelously... I always forget about modulo!  This was extremely easy to adapt to my needs.

Cheers! smiley-grin
Logged

Safety Third!

0
Offline Offline
Full Member
***
Karma: 1
Posts: 179
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am expieriencing with the same problem....please spend some time to read my thread so you can help me...
I am rookie and I need it for a project at my university and I need to finish it as soon as possible...

The thing is that i have a byte and I cannot print it to my lcd, because (i think) it contains 2 digit numbers...And it prints some strange characters.
I am explaining everything to my thread...

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1278090664
I am looking forward to your replies!
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks for this post,  i like it ....
Logged

Pages: [1]   Go Up
Jump to: