Testing program designed to modify an array returns incorrect values

Hello all

I've been writing a relatively large program to control an RGB LED in various different ways. A significant part of this program is to be able to convert a single numerical value to three values in an array (red green and blue). For this I have attempted to create my own function to fulfill this purpose.

However, this function has been returning incorrect values. I have placed it inside it's own script for testing purposes.

int CycleOut[] = {0, 0, 0};

void ValueToColour(int ColourNumber) 
{
  if (ColourNumber < 11) {
    CycleOut[0] = map(ColourNumber, 0, 10, 20, 10);
    CycleOut[1] = map(ColourNumber, 0, 10, 0, 10);
    CycleOut[2] = 0;
  }
  else if (ColourNumber < 21) {
    CycleOut[0] = map(ColourNumber, 11, 20, 9, 0);
    CycleOut[1] = map(ColourNumber, 11, 20, 11, 20);
    CycleOut[2] = 0;
  }
}


void setup() {
  Serial.begin(2000000);
}

void loop() {
  if (Serial.available()) {
    int CNum = Serial.read();
    ValueToColour(CNum);
    for (int i=0; i < 3; i++) {
      Serial.println(CycleOut[i]);
    }
  }
}

I expect different values to be returned via serial when I input a value between 1-20 however instead I just receive 0. Any ideas?

On another note, I believe I should be using pointers in my function however I cannot get them to work without returning errors, any help on this would be greatly appreciated.

Serial.read(); return a char value.

You can try:

int CNum = Serial.parseInt();

to make your code works.

If these are the expected output values:

1: 19 1 0 
2: 18 2 0 
3: 17 3 0 
4: 16 4 0 
5: 15 5 0 
6: 14 6 0 
7: 13 7 0 
8: 12 8 0 
9: 11 9 0 
10: 10 10 0 
11: 9 11 0 
12: 8 12 0 
13: 7 13 0 
14: 6 14 0 
15: 5 15 0 
16: 4 16 0 
17: 3 17 0 
18: 2 18 0 
19: 1 19 0 
20: 0 20 0

then there is nothing wrong with your function, tested as follows (but there are much easier ways to get those values):

int CycleOut[] = {0, 0, 0};

void ValueToColour(int ColourNumber)
{
  if (ColourNumber < 11) {
    CycleOut[0] = map(ColourNumber, 0, 10, 20, 10);
    CycleOut[1] = map(ColourNumber, 0, 10, 0, 10);
    CycleOut[2] = 0;
  }
  else if (ColourNumber < 21) {
    CycleOut[0] = map(ColourNumber, 11, 20, 9, 0);
    CycleOut[1] = map(ColourNumber, 11, 20, 11, 20);
    CycleOut[2] = 0;
  }
}


void setup() {
  Serial.begin(9600);
  for (int j = 1; j < 21; j++) {
    Serial.print(j);
    Serial.print(": ");
    ValueToColour(j);
    for (int i = 0; i < 3; i++) {
      Serial.print(CycleOut[i]);
      Serial.print(" ");
    }
    Serial.println();
  }
}

void loop() {
}

Check out Serial Input Basics.

Thank you for your help, and yes those are the expected values, however i am unable to see a functional difference between your code and mine that would prevent my code from working correctly. Could you clarify which part of my code prevents it from working as intended?

Edit: didn't read arduino_new's comment properly, thank you again, it works now

Here is a simpler approach to arrive at the same values (valid if 1 <= j <=20);

    CycleOut[0] = 20-j;
    CycleOut[1] = j;
    CycleOut[2] = 0;