Convert string to a number

Hi, I need to convert a text string to a number, so I can use a switch.
For example, I have 4 commands like "turn a on, turn a off, turn b on, turn b off" stored in string and I need arduino to know that "turn a on = 1, turn a off = 2,turn b on = 3, turn b off = 4...". Thanks for your help.

byte whatToDo = 0;
if(strcmp(theString, "turn a on") == 0)
   whatToDo = 1;
if(strcmp(theString, "turn a off") == 0)
   whatToDo = 2;
if(strcmp(theString, "turn b on") == 0)
   whatToDo = 3;
if(strcmp(theString, "turn b off") == 0)
   whatToDo = 4;

Obviously, defining a better protocol would make things easier.

I think you need the strcmp() function.

However if you have the option it would make a lot more sense to reduce the commands to a single character. For example 'B' means turn B on and 'b' means turn B off.

...R

Well, Arduino receives SMS commands so it's more user friendly using "turn sth on.."

it's more user friendly using "turn sth on.."

Then you need an expression parser.

That sounds interesting, could you show me an example? Google doesn't seem to know much...

btw this is what I imagined

turn a on => 1
turn a off => 2

switch (number) {
case 1:
  do sth;
  break;
case 2:
  do sthelse;
  break;
}

anno9:
Well, Arduino receives SMS commands so it's more user friendly using "turn sth on.."

How is the SMS generated ?
If it is generated by a computer there is no requirement for "friendly" - just convenience.

...R

It's a code used for remote controlling our boiler and some other things when we're not at home.. It really needs to be user friendly..

Mashing PaulS & your pseudo-code?

void mainFunction{

byte number = convertString("turn a on");

switch (number) {
case 1:
  do sth;
  break;
case 2:
  do sthelse;
  break;


}


byte convertString(String theString){
byte whatToDo = 0;
if(strcmp(theString, "turn a on") == 0)
   whatToDo = 1;
if(strcmp(theString, "turn a off") == 0)
   whatToDo = 2;
if(strcmp(theString, "turn b on") == 0)
   whatToDo = 3;
if(strcmp(theString, "turn b off") == 0)
   whatToDo = 4;
return whatToDo;
}

But I don't want to do that....

Get over it.

You have to put the work in somewhere if you want to convert strings to numbers. If not in a function that you can use in any part of your code you could define keywords as ints in a header file. But it still requires typing it all out.

Unless you are saying you are going to give commands to your controller via text, and the "user friendliness" mean that if someone types it in wrong it still understands. Then parsing is what you want, which will still require plenty of coding to make it user friendly.

Not sure if that's what you want though, cause there would be easier ways to sends commands remotely.

sscanf may be helpful.

anno9:
It's a code used for remote controlling our boiler and some other things when we're not at home.. It really needs to be user friendly..

That does not answer the question of how the message is generated.

Also, it is possible to be both user-friendly and simple for the Arduino to use without needing to parse anything.

...R

Some servo code that receives a numeric servo command value and a servo designator for the command. You probably use a similar setup to control the boiler.

//zoomkat 11-22-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added 
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 

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

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control 
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
        }
         readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}