Better way to program

can any one think/apply a better way to do this?
I need to do from A01 to A15 (inc ON & OFF)
and then also B01 to B15 (inc ON & OFF)

#include <SPI.h>
#include <Ethernet.h>
//#include <RCSwitch.h>
#include <NexaCtrl.h>

// Advance home easy :
#define TX_PIN 10
#define RX_PIN 8
const static unsigned long controller_id = 1234567;
unsigned int device = 1;
NexaCtrl nexaCtrl(TX_PIN, RX_PIN);

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
byte mac[] = { 0x00, 0x15, 0x26, 0x05, 0x71, 0x25 };
IPAddress ip(192,168,1, 3);
IPAddress gateway(192,168,1, 254);
IPAddress subnet(255, 255, 255, 0);

EthernetServer server(23);// Telnet defaults to port 23
boolean alreadyConnected = false; // whether or not the client was connected previously

String commandString;
// RCSwitch mySwitch = RCSwitch();

void setup() {

  Ethernet.begin(mac, ip, gateway, subnet);  // initialize the ethernet device
  server.begin();// start listening for clients
 
  // Transmitter is connected to Arduino Pin #10  
//  mySwitch.enableTransmit(10);

  // Optional set pulse length.
  // mySwitch.setPulseLength(320);
  
  // Optional set protocol (default is 1, will work for most outlets)
  // mySwitch.setProtocol(2);
  
  // Optional set number of transmission repetitions.
  // mySwitch.setRepeatTransmit(15);
    
  
//  Serial.print("Chat server address:");
//  Serial.println(Ethernet.localIP());
}

void loop() {
    
  // wait for a new client:
  EthernetClient client = server.available();

  // when the client sends the first byte, say hello:
  if (client) {
    if (!alreadyConnected) {
      // clead out the input buffer:
//      client.flush();  
      commandString = ""; //clear the commandString variable

//      client.println("Connected"); 
      alreadyConnected = true;
    } 

    while (client.available()) {      
    
      char newChar = client.read();  // read the bytes incoming from the client:

     if (newChar == 0x0D)  //If a 0x0D is received, a Carriage Return, then evaluate the command
     {
         processCommand(commandString);
     } else
     {
//       Serial.println(newChar);
         commandString += newChar;
     }

    }
  }
}

void processCommand(String command)
{
  
  if (command.indexOf("A01ON") > -1){
    nexaCtrl.DeviceOn(controller_id, 1);
    commandString = "";
    return;
  } 
  
  if (command.indexOf("A01OFF") > -1){
    nexaCtrl.DeviceOff(controller_id, 1);  
    commandString = "";
    return;
  } 
  
  if (command.indexOf("A01D01") > -1){
    nexaCtrl.DeviceDim(controller_id, 1, 1);
    commandString = "";
    return;
  } 
  
  if (command.indexOf("A01D02") > -1){
    nexaCtrl.DeviceDim(controller_id, 1, 2);
    commandString = "";
    return;
  } 
  
  if (command.indexOf("A01D03") > -1){
    nexaCtrl.DeviceDim(controller_id, 1, 3);
    commandString = "";
    return;
  } 
  
  if (command.indexOf("A01D04") > -1){
    nexaCtrl.DeviceDim(controller_id, 1, 4);
    commandString = "";
    return;
  } 
  
  if (command.indexOf("A01D05") > -1){
    nexaCtrl.DeviceDim(controller_id, 1, 5);
    commandString = "";
    return;
  } 
  
  if (command.indexOf("A01D06") > -1){
    nexaCtrl.DeviceDim(controller_id, 1, 6); 
    commandString = "";
    return;
  } 
  
  if (command.indexOf("A01D07") > -1){
    nexaCtrl.DeviceDim(controller_id, 1, 7);
    commandString = "";
    return;
  } 
  
  if (command.indexOf("A01D08") > -1){
    nexaCtrl.DeviceDim(controller_id, 1, 8); 
    commandString = "";
    return;
  } 
  
  if (command.indexOf("A01D09") > -1){
    nexaCtrl.DeviceDim(controller_id, 1, 9);
    commandString = "";
    return;
  } 
  
  if (command.indexOf("A01D10") > -1){
    nexaCtrl.DeviceDim(controller_id, 1, 10);
    commandString = "";
    return;
  } 
  
  if (command.indexOf("A01D11") > -1){
    nexaCtrl.DeviceDim(controller_id, 1, 11);
    commandString = "";
    return;
  } 
  
  if (command.indexOf("A01D12") > -1){
    nexaCtrl.DeviceDim(controller_id, 1, 12);
    commandString = "";
    return;
  } 
  
  if (command.indexOf("A01D13") > -1){
    nexaCtrl.DeviceDim(controller_id, 1, 13);
    commandString = "";
    return;
  } 
  
  if (command.indexOf("A01D14") > -1){
    nexaCtrl.DeviceDim(controller_id, 1, 14); 
    commandString = "";
    return;
  } 
  
  if (command.indexOf("A01D15") > -1){
    nexaCtrl.DeviceDim(controller_id, 1, 15);
    commandString = "";
    return;
  } 

  if (command.indexOf("A02ON") > -1){
    nexaCtrl.DeviceOn(controller_id, 2);
    commandString = "";
    return;
  } 
  
  if (command.indexOf("A02OFF") > -1){
    nexaCtrl.DeviceOff(controller_id, 2);  
    commandString = "";
    return;
  } 
  
  if (command.indexOf("A02D01") > -1){
    nexaCtrl.DeviceDim(controller_id, 2, 1);
    commandString = "";
    return;
  } 
  
  if (command.indexOf("A02D02") > -1){
    nexaCtrl.DeviceDim(controller_id, 2, 2);
    commandString = "";
    return;
  } 
  
  if (command.indexOf("A02D03") > -1){
    nexaCtrl.DeviceDim(controller_id, 2, 3);
    commandString = "";
    return;
  } 
  
  if (command.indexOf("A02D04") > -1){
    nexaCtrl.DeviceDim(controller_id, 2, 4);
    commandString = "";
    return;
  } 
  
  if (command.indexOf("A02D05") > -1){
    nexaCtrl.DeviceDim(controller_id, 2, 5);
    commandString = "";
    return;
  } 
  
  if (command.indexOf("A02D06") > -1){
    nexaCtrl.DeviceDim(controller_id, 2, 6); 
    commandString = "";
    return;
  } 
  
  if (command.indexOf("A02D07") > -1){
    nexaCtrl.DeviceDim(controller_id, 2, 7);
    commandString = "";
    return;
  } 
  
  if (command.indexOf("A02D08") > -1){
    nexaCtrl.DeviceDim(controller_id, 2, 8); 
    commandString = "";
    return;
  } 
  
  if (command.indexOf("A02D09") > -1){
    nexaCtrl.DeviceDim(controller_id, 2, 9);
    commandString = "";
    return;
  } 
  
  if (command.indexOf("A02D10") > -1){
    nexaCtrl.DeviceDim(controller_id, 2, 10);
    commandString = "";
    return;
  } 
  
  if (command.indexOf("A02D11") > -1){
    nexaCtrl.DeviceDim(controller_id, 2, 11);
    commandString = "";
    return;
  } 
  
  if (command.indexOf("A02D12") > -1){
    nexaCtrl.DeviceDim(controller_id, 2, 12);
    commandString = "";
    return;
  } 
  
  if (command.indexOf("A02D13") > -1){
    nexaCtrl.DeviceDim(controller_id, 2, 13);
    commandString = "";
    return;
  } 
  
  if (command.indexOf("A02D14") > -1){
    nexaCtrl.DeviceDim(controller_id, 2, 14); 
    commandString = "";
    return;
  } 
  
  if (command.indexOf("A02D15") > -1){
    nexaCtrl.DeviceDim(controller_id, 2, 15);
    commandString = "";
    return;
  }   

  commandString = "";
}

this screams for a nested for loop, something like

for (int i=0; i< 15; i++)
{
  for (int j = 0; j < 15; j++)
  {
    char buf[8];
    sprintf(buf, "A%02dD%02d", i, j);
    if (command.indexOf(buf) > -1)
    {
      nexaCtrl.DeviceDim(controller_id, i, j);
      commandString = "";
      return;
    } 
  }
}

You could put the test values into an array and use a FOR loop to iterate through the array? Save a lot of repetitive typing with the associated risk of errors.

You may be able to simplify the tests by first testing for AOID or AO2. Maybe you only need to check if the 3rd character is 1 or 2?

Have you any control over the format of the incoming data? If so, simplify it.

...R

I can set the control string to what I want, As I'm using Irule as the user GUI.
I have been thinking about what you said, and I was going to use A0100 as off but 00 is a dim level!
But I can still use A0116 as on ...

sscanf might be a good starting point.

richard1982:
I can set the control string to what I want,

Then think carefully about what you actually need to send.

How many different "things" are there - you seem only to have 2 designated by AO1 and AO2. Just call them A and B (which gives you 24 more options if you need them - or 60 if you use the lower case characters and numeric characters).

Then how many settings do you need for each thing?
If you can get by with 26 options just use A,B,C for them as well - so you get AA to AZ and BA to BZ.

If you want to send numbers to A and B you could do it like this
A,2
B,102
and use the parsing code in serial input basics to extract the different parts.

...R