Arduino arrays over 9 long

Hello everyone,

I am working on a program that currently uses an array of ints covering the ports used and a boolean array to control toggling them on and off. I noticed that when I enter 1234 in the serial port the pins for the first through forth spot on my array are turned on, which is fine. This clearly follows the same pattern when entering 123456789 in one line. I know that the Serial.read() does this because it reads one byte at a time. However if my array is over 9 pins how can I pass array position 10 through the condition so the tenth pin is turned on. Any advice would be appreciated. Attached is the code I am using:

(assuming I add 9 - 1 in the pinMap array and a false for each to relayState)

int pinMap[] = {13, 12, 11, 10};
byte pinCount = sizeof(pinMap) / sizeof(pinMap[0]);
boolean relayState[]= {false, false, false, false};

void setup()
{
 
  Serial.begin(38400);

  for(int count = 0; count < pinCount; count++) 
  {
    pinMode(pinMap[count], OUTPUT);
    digitalWrite(pinMap[count], LOW);
  }
}

void loop()
{
  byte val;    
  int channel; 

  if(Serial.available()) 
    {
    val = Serial.read();

    channel = (int)val - 48; // Convert ASCII value to digit
    if(channel > 0 && channel <= pinCount) {
      pulseOutput(channel);  // Pulse the appropriate button
    }
  }
}

void pulseOutput(int channel)
{
  relayState[channel-1] = !relayState[channel-1];
  digitalWrite(pinMap[channel - 1], relayState[channel-1]);
  
  Serial.print(channel);
  if(relayState[channel-1] == true)
    Serial.println(" ON");
  else
    Serial.println(" OFF");
}

Use letters.

0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ

which gives you 36 channels.

Here is an example (note that rather than subtracting 48, you can subtract ‘0’ which although it does the same, it is more readable)

    if(val > '9'){
      channel = val - 'A' + 10;// Convert ASCII value to digit
    } else {
      channel = val - '0';// Convert ASCII value to digit
    } 
    if(channel > 0 && channel <= pinCount) {
      pulseOutput(channel);  // Pulse the appropriate button
    }

Thank you very much that was just what I was looking for!