convert serial read to action

Is it possible to have the Arduino read the serial input and use it to call a command?

I am using this as a template…

#include <Wire.h>                 //library for oled one wire
#include <Arduino.h>
#include <ZtLib.h>                //library for oled display
#define OLED_ADDRESS    0x27      //assign the oled address 

String inputString = "";          // a string to hold incoming data
boolean stringComplete = false;   // whether the string is complete
#define RELAY 7                   //Set variable to Relay Pin  


void setup() {          
  Serial.begin(9600);              // initialize serial:
  inputString.reserve(200);        // reserve 200 bytes for the inputString:
  pinMode(RELAY, OUTPUT);          //set relay as ouput 

  ZT.I2cInit();                    //initiate oled display 
  ZT.ScI2cMxReset(OLED_ADDRESS);   //reset oled display          
  delay(5);                       //delay for 5 millisec 
}

void loop() {
  if (stringComplete) {            // print the string when a newline arrives:
    //Serial.println(inputString); // prints the input string for debugging  
    triggerRelay();                //calls the function trigger Ralay
    oledDisplay();                 //calls the function Display to oled
    inputString = "";              // clear the string:
    stringComplete = false;        //set the stringcomplete to false to stop serial reading
  }
}

/*
  SerialEvent occurs whenever a new data comes in the
 hardware serial RX.  This routine is run between each
 time loop() runs, so using delay inside loop can delay
 response.  Multiple bytes of data may be available.
 */
void serialEvent() {
  while (Serial.available()) {            // get the new byte:
    char inChar = (char)Serial.read();    // add it to the inputString:
    inputString += inChar;                // if the incoming character is a newline, set a flag so the main loop can do something about it:
    if (inChar == '\n') {                 // when the end of line is reached, assign it to inputString
      stringComplete = true;
    }
  }
}

/*
triggerRelay function checks if incoming data from serial is ~1
then put the relay pin to high turning the relay on, if data is ~0
then set the relay pin to low turning relay off.Relay work on invert. ie High turns it OFF. 
 */
void triggerRelay() {
  if (inputString == String("~1\n")) {
    digitalWrite(RELAY, LOW);
  }

  if (inputString == String("~0\n")) {
    digitalWrite(RELAY, HIGH);
  }

/*
oledDisplay function changes the inputString to String Array and 
displays it to oled screen.
 */
void oledDisplay(){
  if ((inputString != String("~1\n")) && (inputString != String("~0\n"))){  //only display if inputString is not ~1 and not ~0
    int i = inputString.length();
    char stringArray[i];

    inputString.toCharArray(stringArray,i);

    ZT.ScI2cMxDisplay8x16Str(OLED_ADDRESS,0,0,(char*)stringArray); //print to oled display
  }
}

But I am having trouble when I try to use certain input strings, like “~w2\n”

Is it possible to just send a code like “action5();” and just have it call that action?

Thank you!

Yes, quite possible.

Please tell me how! It would solve all of my problems!

If you mean can you send "action5()" (crappy name for a function), store that in a String, and then just execute the String to call a function, no, you can't do that.

You could send "action5()", store that in a String, and then compare that to a constant, and then call a function if there is a match:

if(inputString == "action5()")
   action5();

Specifically, what trouble are you having with what you are sending now?

I have an Android app that sends info to the arduino via bluetooth. For these inputs everything works the way it is supposed to. However, if I modify any piece of this, that is to say, change a number or change a letter or add another if statement, none of the if statements work.

if (inputString == String("~x9\n")){
    m6b4action() ;
   }
   if (inputString == String("~y2\n")){
    m6b5action() ;
    }
    if (inputString == String("~x1\n")){
    m5b3action();
    }
   if (inputString == String("~x2\n")){
    m5b1action();
    }
   if (inputString == String("~x3\n")){
    m5b4action();
     }
   if (inputString == String("~x4\n")){
    m5b2action();
     }
   if (inputString == String("~x5\n")){
    m6b4action();
      }
   if (inputString == String("~x6\n")){
    m5b6action();
      }

Very simple serial control setup you can try with the serial monitor.

// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial on/off test 0021"); // so I can keep track
}

void loop() {

  while (Serial.available()) {
    delay(3);  
    char c = Serial.read();
    readString += c; 
  }

  if (readString.length() >0) {
    Serial.println(readString);

    if (readString == "on")     
    {
      digitalWrite(ledPin, HIGH);
    }
    if (readString == "off")
    {
      digitalWrite(ledPin, LOW);
    }

    readString="";
  } 
}

OMG! YOU DID IT! THANK YOU THANK YOU THANK YOU THANK YOU!

I had a programmer friend with me trying to sort out the problem and after almost 2 hours he gave up... then I saw your response, changed my code a little, and BINGO! It works!

THANK YOU THANK YOU THANK YOU THANK YOU!THANK YOU THANK YOU THANK YOU THANK YOU!THANK YOU THANK YOU THANK YOU THANK YOU!THANK YOU THANK YOU THANK YOU THANK YOU!THANK YOU THANK YOU THANK YOU THANK YOU!THANK YOU THANK YOU THANK YOU THANK YOU!

But do you know why? The code that zoomkat posted, in spite of using the String class, uses only ONE instance of the string class.

Your code was pissing resources away right and left, creating and destroying, uselessly, instances of the String class all over the place. That WILL lead to memory problems.