Function and array, passing values not working...?

Dear all,
I have been struggling now for a looong time with this code. I think that I am nearly there, but alas, something is still not working...

I am collecting input from the serial monitor, running it through a conversion function, and then returning the output to the serial monitor.

The input is three numbers separated by commas.

The conversion function is intended to return an array of three values. For now it is only multiplying each of the original values by 10 to keep it simple.

However, it seems to return only three 0s.

I am pretty sure that I am doing something wrong in passing the array, and have been reading up, but I just cannot see what is wrong.

I do not get any compilation errors.

See code below.

I hope to end up with a function, which can convert HSV values to RGB, but this is the first step. I need to be able to master the basics first, and then expand.

So I'd much appreciate if you can tell what is wrong with my code.

Steps to reproduce:
Connect your Arduino.
Upload the code.
Open the Serial Monitor.
Enter into the Serial Monitor: 15,15,15
The Serial Monitor Output will show:
F
F
F
0
0
0
(the F's are ok, they are the hex value of 15, but the 0's are just plain wrong)

Best regards,
Lars

// LM:
// Read input, calculate, post output
// Testing basis for HSV converter and other general testing

int result[3];

void setup() {
  Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
  // Serial.setTimeout(3000);
}

void loop() {
  Serial.println("Enter RGB values, separated by commas: ex: 123,123,123");

  // if there's any serial available, read it:
  if (Serial.available() > 0) {
    // look for the next valid integer in the incoming serial stream:
    int red = Serial.parseInt();
    // do it again:
    int green = Serial.parseInt();
    // do it again:
    int blue = Serial.parseInt();

    // End of input is detected with newline
    if (Serial.read() == '\n') {
      // print the three numbers in one string as hexadecimal:
      Serial.println(red, HEX);
      Serial.println(green, HEX);
      Serial.println(blue, HEX);
      int ConversionFunction(int red, int green, int blue);
      Serial.println(result[0]);
      Serial.println(result[1]);
      Serial.println(result[2]);
    }
    delay(10000);
  }
}

int ConversionFunction(int x, int y, int z) {
  result[0] = x * 10;
  result[1] = y * 10;
  result[2] = z * 10;
  return result[3];
}

You are not calling the conversion function properly.

//int ConversionFunction(int red, int green, int blue);
ConversionFunction( red,  green,  blue);

return result[3];You are returning the undefined fourth element of the result array.

As result is a global array why return anything ?

      int ConversionFunction(int red, int green, int blue);Does not call the function. Rather it declares it

I tried your sketch.
It doesn't do anything after I enter 15,15,15 in the Serial Monitor even with the fixes cattledog and UKHeliBob suggested.

It doesn't do anything after I enter 15,15,15 in the Serial Monitor even with the fixes cattledog and UKHeliBob suggested.

Is your monitor sent for new line ending?

cattledog:
Is your monitor sent for new line ending?

That was it. Thanks.
Not sure how it got out of whack.

cattledog:
You are not calling the conversion function properly.

//int ConversionFunction(int red, int green, int blue);

ConversionFunction( red,  green,  blue);

Thanks a lot, Cattledog!
This solved the problem. For completeness, I will post the working code below.

Thank you also to UKHeliBob and ieee488.

Do you mean that I should just leave out this line:

return result[3];

?


Working code after change as suggested by Cattledog:

// LM:
// This works 2017-07-17
// Read input, calculate, post output
// Testing basis for HSV converter and other general testing

int result[3];

void setup() {
  Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
}

void loop() {
  Serial.println("Enter RGB values, separated by commas: ex: 123,123,123");

  // if there's any serial available, read it:
  if (Serial.available() > 0) {
    // look for the next valid integer in the incoming serial stream:
    int red = Serial.parseInt();
    // do it again:
    int green = Serial.parseInt();
    // do it again:
    int blue = Serial.parseInt();

    // End of input is detected with newline
    if (Serial.read() == '\n') {
      // print the three numbers in one string as hexadecimal:
      Serial.println(red, HEX);
      Serial.println(green, HEX);
      Serial.println(blue, HEX);

      ConversionFunction(red, green, blue);

      Serial.println(result[0]);
      Serial.println(result[1]);
      Serial.println(result[2]);
    }
    delay(10000);
  }
}

int ConversionFunction(int x, int y, int z) {
  result[0] = x * 10;
  result[1] = y * 10;
  result[2] = z * 10;
  return result[3];
}

Do you mean that I should just leave out this line:

return result[3];

?

Answering that question myself: I can see that it works even when I comment out that line, so thank you!

--
Updated full code:

// LM:
// This works 2017-07-17
// Read input, calculate, post output
// Testing basis for HSV converter and other general testing

int result[3];

void setup() {
  Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
}

void loop() {
  Serial.println("Enter RGB values, separated by commas: ex: 123,123,123");

  // if there's any serial available, read it:
  if (Serial.available() > 0) {
    // look for the next valid integer in the incoming serial stream:
    int red = Serial.parseInt();
    // do it again:
    int green = Serial.parseInt();
    // do it again:
    int blue = Serial.parseInt();

    // End of input is detected with newline
    if (Serial.read() == '\n') {
      // print the three numbers in one string as hexadecimal:
      Serial.println(red, HEX);
      Serial.println(green, HEX);
      Serial.println(blue, HEX);

      ConversionFunction(red, green, blue);

      Serial.println(result[0]);
      Serial.println(result[1]);
      Serial.println(result[2]);
    }
    delay(10000);
  }
}

int ConversionFunction(int x, int y, int z) {
  result[0] = x * 10;
  result[1] = y * 10;
  result[2] = z * 10;
  //return result[3];
}

result array is a global.

You could easily have declared ConversionFunction to return void and the sketch would work.

int ConversionFunction(int x, int y, int z) {
  result[0] = x * 10;

You promised the compiler that you'd return an int, but didn't.
Change the return type to "void"

AWOL:

int ConversionFunction(int x, int y, int z) {

result[0] = x * 10;



You promised the compiler that you'd return an int, but didn't.
Change the return type to "void"

Interestingly enough, the compile did not burp an error when there was no return of any kind.

AWOL:

int ConversionFunction(int x, int y, int z) {

result[0] = x * 10;



You promised the compiler that you'd return an int, but didn't.
Change the return type to "void"

You're so right!

I have now changed it to "void", and it works just fine.

Thanks again!

--
Question arises: could I have avoided the global declaration of "result", and still had the sketch working as intended?
I will think about that ...tomorrow; it is getting late here...

--
Updated working code, just for completeness sake:

// LM:
// This works 2017-07-17
// Read input, calculate, post output
// Testing basis for HSV converter and other general testing

int result[3];

void setup() {
  Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
}

void loop() {
  Serial.println("Enter RGB values, separated by commas: ex: 123,123,123");

  // if there's any serial available, read it:
  if (Serial.available() > 0) {
    // look for the next valid integer in the incoming serial stream:
    int red = Serial.parseInt();
    // do it again:
    int green = Serial.parseInt();
    // do it again:
    int blue = Serial.parseInt();

    // End of input is detected with newline
    if (Serial.read() == '\n') {
      // print the three numbers in one string as hexadecimal:
      Serial.println(red, HEX);
      Serial.println(green, HEX);
      Serial.println(blue, HEX);

      ConversionFunction(red, green, blue);

      Serial.println(result[0]);
      Serial.println(result[1]);
      Serial.println(result[2]);
    }
    delay(10000);
  }
}

int ConversionFunction(int x, int y, int z) {
  result[0] = x * 10;
  result[1] = y * 10;
  result[2] = z * 10;
  //return result[3];
}

THANKS AGAIN, ALL, for your great help!

Br, Lars

ieee488:
Interestingly enough, the compile did not burp an error when there was no return of any kind.

I'll bet it burped a warning that was suppressed.

aarg:
I'll bet it burped a warning that was suppressed.

The default setting for the IDE apparently.

You can save a few bytes with a few minor changes, but moving the array from being a global piece of data to a local array can help you debug your programs, since the array only lives within the function where it is defined:

// LM:
// This works 2017-07-17
// Read input, calculate, post output
// Testing basis for HSV converter and other general testing


void setup() {
  Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
  Serial.println("Enter RGB values, separated by commas: ex: 123,123,123");
}

void loop() {
  int result[3];

  // if there's any serial available, read it:
  if (Serial.available() > 0) {
    // look for the next valid integer in the incoming serial stream:
    result[0] = Serial.parseInt();
    // do it again:
    result[1] = Serial.parseInt();
    // do it again:
    result[2] = Serial.parseInt();

    // End of input is detected with newline
    if (Serial.read() == '\n') {
      // print the three numbers in one string as hexadecimal:
      for (int i = 0; i < 3; i++) {
        Serial.println(result[i]);
      }

      ConversionFunction(result, 3);

      for (int i = 0; i < 3; i++) {
        Serial.println(result[i]);
      }
   }
    delay(10000);
  }
}

void ConversionFunction(int result[], int elementCount) {
  int i;

  for (i = 0; i < elementCount; i++) {
    result[i] *= 10;
  }
}

The process of encapsulation will make debugging a lot simpler for you.

econjack:
You can save a few bytes with a few minor changes, but moving the array from being a global piece of data to a local array can help you debug your programs, since the array only lives within the function where it is defined:

// LM:

// This works 2017-07-17
// Read input, calculate, post output
// Testing basis for HSV converter and other general testing

void setup() {
  Serial.begin(9600);    // opens serial port, sets data rate to 9600 bps
  Serial.println("Enter RGB values, separated by commas: ex: 123,123,123");
}

void loop() {
  int result[3];

// if there's any serial available, read it:
  if (Serial.available() > 0) {
    // look for the next valid integer in the incoming serial stream:
    result[0] = Serial.parseInt();
    // do it again:
    result[1] = Serial.parseInt();
    // do it again:
    result[2] = Serial.parseInt();

// End of input is detected with newline
    if (Serial.read() == '\n') {
      // print the three numbers in one string as hexadecimal:
      for (int i = 0; i < 3; i++) {
        Serial.println(result[i]);
      }

ConversionFunction(result, 3);

for (int i = 0; i < 3; i++) {
        Serial.println(result[i]);
      }
  }
    delay(10000);
  }
}

void ConversionFunction(int result[], int elementCount) {
  int i;

for (i = 0; i < elementCount; i++) {
    result[i] *= 10;
  }
}




The process of encapsulation will make debugging a lot simpler for you.

Thank you, Econjack,
I aim to code to best practices, getting into good habits from the beginning, so your advice is very useful.

Best regards,
Lars