Curing my newbness

I can't figure out how to break a multi-digit number into single digit ones. I have googled it, and other people have had the same question, but the answers include functions and symbols that I am not familiar with. If someone could break it down into baby language for me, I would be most appreciative.

Here is the code that supposedly does it:

#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*=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*=seg_def[number%10];
    Serial.println(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/_

    What is a char? Why not use int i instead of char i?
    How do you use bytes? I understand what they are, but how would you call them to light certain segments of a 7-segment led?
    What is the %?
    I am trying to break down a multi-digit integer and seperate it into single-digit integers. I understand there are multiple ways to do this, but I don't understand the functions used to do it. Is there an index of commands that I can use to look up the correct usage of these functions I don't understand? Anyway, any help would be stellar.

Is there an index of commands that I can use to look up the correct usage of these functions I don't understand?

Yeah, it's called google.

The two usual ways to convert a number into a string are sprintf and itoa.

int someNum = 259;
char numAsStg[10];

atoi(someNum, numAsStg, 10);

or

sprintf(numAsStg, "%d", someNum);

...under the tab reference at arduino.cc there is an index of commands. Wow.

There is, but it is by no means an exhaustive list of functions that can be used on the Arduino. Not by long ways.

There was another post that said the sprintf function wasn't a good one to use because it could cause buffer overflow errors. And I believe I read that strings in general are more burdensome than integers, etc. Also, the problem with atoi is, if I understand this C string handling - Wikipedia correctly, is it can't distinguish between a true zero and a blank character. The code that I submitted is supposed to not turn a multi-digit number into a string at all.

electrosheep:

Have you forgotten how base-10 works? I am beginning to wonder if we need a tutorial (facepalm) on how base-10 works, because this question comes up on a regular basis, it seems...

Lets say you have a number: int num = 2957;

You want the '2', '9', '5', and '7' separate...

Divide it by 1000: int num1 = num / 1000;

Since num and num1 are integer variables, num1 will be "2" - there's your first number.

Multiply that by 1000: int temp = num1 * 1000;

Subtract the result (2000) from your original number: num = num - temp;

The variable num is now "957".

I'll leave it to you as to how to get the rest, but realize you have a "hundred's place", a "ten's place", and a "one's place"...

/I feel like I am in grade school...

Perfect. I appreciate the response. I thought I remembered integers being rounded. I didn't see any threads that particularly made sense to me, but my discovery of the reference tab has helped me. I think there should be more tutorials outlining basic C++ principles. Perhaps one day, when I am more experienced, I'll make some.