Command.substring(); command use

Particle photon the best board i ever worked on. I have controlled devices and motor for a robot using some of the command like command.substring(); but dont know what exactly doing. Can any one please explaint me how it is working.

int ledControl(String command) {
    int state =0;
    int pinNumber = (command.charAt(1)-'0') - 1;
    if(pinNumber < 0 || pinNumber > 7) return -1;
    if(command.substring(3,7) == "HIGH") state = 1;
    else if(command.substring(3,6) == "LOW") state = 0;
    else return -2;
    
    digitalWrite(pinNumber,state);
    return 1;
} 

Thank you in advance please help.

A quick search in the Particle docs would bring you here
https://docs.particle.io/reference/firmware/photon/#substring-

In short, you take the part of the command string (=sub-string) between the two string positions to work with.

Hey @ScruffR thank you for a quick reply. Still i am not able to understand why i am passing 3,7 and 3,6 in the above code.
if(command.substring(3,7) == “HIGH”) state = 1;
else if(command.substring(3,6) == “LOW”) state = 0;

Because the command string will be D1=HIGH or D1=LOW
So count the string positions 3~7 for the first and 3~6 for the second …

… clearer?

3 Likes

thanks @ScruffR :smile: got the point :joy:

1 Like