problem with passing char array to function

Hello,

The problem i have is that i have a function thats reads serial data into a char array. That data is passed to another function that reads part of that array and returns the numeric value of the data.

Example: if i send 255123 i need two numbers (255 and 123). That works almost as expected. THe function is enclosed below:

int GetColorInfo(char data[128],int start,int end)
{

int output =0;

for(int i=start; i <= end; i++)
{
int color = data*;*

  • int number = color -48;*

  • if(number >= 0 && number <=9)*

  • {*

  • switch (i)*

  • {*

  • case 0:*

_ output += (number * 100);_

  • break;*

  • case 1:*
    _ output += (number * 10);_

  • break;*

  • case 2:*

  • output += number;*

  • break;*

  • }*

  • }*

  • }*
    Ok, when in the main loop i do this:
    void loop()
    {

  • int firstColor = GetColorInfo(buffer,0,2);*

  • Serial.println(firstColor);*

  • int secondColor = GetColorInfo(buffer,1,3);*

  • Serial.println(secondColor);*
    }
    when input in the following string (123456) in the serial port i receive the following: firstcolor prints 123 (thats ok) but secondColor prints 23 thats not good. It seems that it doesn't make a copy of the array or something.
    But when i change it to:
    int firstColor = GetColorInfo(buffer,0,2);

  • Serial.println(firstColor);*

  • int secondColor = GetColorInfo(buffer,0,2);*

  • Serial.println(secondColor);*
    It will print 123 two times. It seems that my array is shortened after reading it. But that should not be as far as i know. Can you guys give me some help?
    Thanks!

Take a look at this tutorial here, only one I know of atm, should give you an idea.

I'm still learning myself, or I would offer up some code :stuck_out_tongue:

Sorry dumb question. I made an error in the for: the solution is below:

for(int i=start; i <= end; i++)
{
int color = data*;*

  • int number = color -48;*

  • if(number >= 0 && number <=9)*

  • {*

  • switch (teller)*

  • {*

  • case 0:*

_ output += (number * 100);_

  • break;*

  • case 1:*
    _ output += (number * 10);_

  • break;*

  • case 2:*

  • output += number;*

  • break;*

  • }*

  • }*

  • teller++;*

  • }*

What is 'teller' and where is it declared?

Teller is a counter (in dutch).

try:
for(int i=start, teller=0; i <= end; i++, teller++)