look this code over, seems like where you're headed
- it better to read a complete line (up to a \n) and then extract fields from that line
- rather than have unique IDs for each LED, consider an "led" cmd with 2 arguments: an led idx and value
// simple cmd processing
const int NUM_LEDS = 2;
const int LED_pins[] = { 13, 12 };
// -----------------------------------------------------------------------------
void cmdProcessor ()
{
if (Serial.available ()) {
char buf [90];
int n = Serial.readBytesUntil ('\n', buf, sizeof(buf)-1);
buf [n] = '\0';
char cmd [10];
int id;
int arg;
sscanf (buf, "%s %d %d", cmd, &id, &arg);
if (! strcmp (cmd, "led"))
if (NUM_LEDS > id)
digitalWrite (LED_pins [id], arg);
// other cmds
}
}
// -----------------------------------------------------------------------------
void loop ()
{
cmdProcessor ();
}
void setup ()
{
Serial.begin (115200);
for (int i = 0; i < NUM_LEDS; i++) {
pinMode (LED_pins[i], OUTPUT);
}
}