run functions through serial input

hi, I am building an Arduino (uno) controller for a smart home accessory and the program is not behaving as expected. i had expected to put in text such as:
led(red=255, green = 100, blue = 150)

I have managed to get the program to print what i input into the serial. However i am not calling the functions.

i have attached my code:

#include <Stepper.h>
//ints
int serialInput = "";
int overrideButton = false; // turn override on
int overrideOpen = false; // open curtains while in override
bool openState = false; //check if open
int ambient = ""; //collection for sensor
const int stepsPerRevolution = 212; // motor revolution
int motorMove = 3800; //how far the motor has to move to flly close the curtain
int startMarker = '<';
int endMarker = '>';
int func = "";
int answer = "";


const byte numChars = 32;
char receivedChars[numChars];
boolean newData = false;
bool curtainStateOpen = false;

//pins decleration
int sensor = A4;
int RX = 0;
int TX = 1;
int redButton = digitalRead(2);
int redPin = 3;
int blackButton = digitalRead(4);
int greenPin = 5;
int bluePin = 6;
int motorPower = 7;
int motor1 = 8;
int motor2 = 9;
int motor3 = 10;
int motor4 = 11;
int motor5 = 12;
int motor6 = 13;

//unknow
Stepper myStepper(stepsPerRevolution, 8, 11, 12, 13);


void setup() {
  myStepper.setSpeed(180);

  //arduino set up
  Serial.begin(9600); //start serial

  //pinmodes
  pinMode(2, INPUT); //button
  pinMode(3, OUTPUT); //redled
  pinMode(4, INPUT); //button
  pinMode(5, OUTPUT); //greenled
  pinMode(6, OUTPUT); //blueled
  pinMode(7, OUTPUT); //motorpower
  pinMode(8, OUTPUT); //motorcontrol
  pinMode(9, OUTPUT); //motorsheildpower
  pinMode(10, OUTPUT); //motorsheildpower
  pinMode(11, OUTPUT); //motorcontrol
  pinMode(12, OUTPUT); //motorcontrol
  pinMode(13, OUTPUT); //motorcontrol

off();
digitalWrite(7, HIGH);


}

void loop() {

  //while(!overrideButton) {
  recvWithStartEndMarkers();
  showNewData();
 serialInput = (int)receivedChars;
  //Serial.println(serialInput);


  
  switch (serialInput)
  {
    case 'a':
      off();
      break;
    case 'b':
      on();
      break;
    case 'q':
      red();
      break;

  }
  serialInput;
  //runFunc();
  serialInput = NULL;
}
  
 /* if (blackButton == HIGH) {
    overrideButton = !overrideButton;
  }*/

/*while (overrideButton == true) {
     if(blackButton == HIGH) {

     overrideButton = !overrideButton;
   }
  }
   if (redButton == HIGH) {
     overrideOpen = !overrideOpen;
   }
   if (openState == true) {
     curtainOpen();
   } else if (openState == false) {
     curtainClose();
   } else {
     for(int x = 0; x < 8; x++) {
     Serial.print("ERROR ");
     Serial.println("override function curtainopen not a bool!");
     }
   }
  }
*/
int curtainOpen() {
  if (openState == false) {
//func = serialInput;
    digitalWrite(7, LOW);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);

    myStepper.step(motorMove);

    digitalWrite(7, HIGH);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
  }
  openState = true;
 answer = "OPEN";

}


int curtainClose() {
  if (openState == true) {

    digitalWrite(7, LOW);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);

    myStepper.step(-motorMove);

    digitalWrite(7, HIGH);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
  }
  openState = false;
  answer = "close";
}

int brightness() {
  ambient = analogRead(sensor);
  Serial.print("sensor = ");
  Serial.print(ambient);
  Serial.println(";");
}

int recvWithStartEndMarkers() {
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '<';
  char endMarker = '>';
  char rc;

  while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();

    if (recvInProgress == true) {
      if (rc != endMarker) {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars) {
          ndx = numChars - 1;
        }
      }
      else {
        receivedChars[ndx] = '\0'; // terminate the string
        recvInProgress = false;
        ndx = 0;
        newData = true;
      }
    }

    else if (rc == startMarker) {
      recvInProgress = true;
    }
  }
}

int showNewData() {
  if (newData == true) {
    serialInput = (int)receivedChars;
    newData = false;
  }
}
int yellow() {
  
digitalWrite(3, LOW);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
  answer = "ON";
printer();
}

void blue() {
  
digitalWrite(3, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
  answer = "ON";
printer();
}
int off() {
  
digitalWrite(3, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
  answer = "ON";
printer();
}
int on() {
  
digitalWrite(3, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
  answer = "ON";
printer();
}
int cyan() {
  
digitalWrite(3, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
  answer = "ON";
printer();
}
int violet() {
  
digitalWrite(3, LOW);
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
 answer = "ON";
printer();
}

int green() {
  
digitalWrite(3, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
 answer = "ON";
printer();
}

int red() {
  func = "red";
digitalWrite(3, LOW);
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
  answer = "ON";
printer();
}
int printer() {
  Serial.write( (int) startMarker);
  Serial.print( (int) func);
  Serial.print(" = ");
  Serial.print(answer);
  Serial.println( (int) endMarker);
}
int runFunc() { 
  if (serialInput == "open") { 
    curtainOpen();
    Serial.println('1');
  } else if (serialInput == "close") {
    curtainClose();
    Serial.println('1');
  } else if (serialInput == 'on') {
    on();
    Serial.println('1');
  } else if (serialInput == "off") {
    off();Serial.println('1');
  } else if (serialInput == red) {
    red();
  } else if (serialInput == green) {
    green();
  } else if (serialInput == blue) {
    blue();
  } else if (serialInput == yellow) {
    yellow();
  } else if (serialInput == cyan) {
    cyan();
  } else if (serialInput == violet) {
    violet();
  } else if (serialInput == (int)brightness) {
    brightness();
  } else if (serialInput == "") {
  }else {
      func = "runfunc";
      answer = "error not accepted";
 printer();
  }
}

any suggestions?

There are a lot of things about this program that confuse me, such as why variables that are going to hold a C string of five characters are declared as int (which can hold two characters if you are careful), why names are declared for pin numbers and then pin numbers are used instead of names, what the wiring is so that inputs may be declared INPUT instead of INPUT_PULLUP, and why this line

  serialInput;

exists.

The OP needs to fix the program, explain which functions are not being called, and how the OP knows that.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The simplest way to send the data would be <255, 100, 150> and rely on the order of the numbers to identify which is red, green and blue. That will make the Arduino program very much simpler.

…R

vaj4088 - the line:
serialInput;

was interred to call the function. for example if i sent <open()> i had hoped that at the point it would have called the ‘open()’ function. how could i adapt my code to make this better?

Robin2 - that tutorial has helped me very much, however as i will be sending serial from one arduino to another over the TX and RX pins, i was hoping i would be able to declare variables and trigger the response in separate inputs:
<red = 0>
<green = 255>
<blue = 0>
<led()>

is this at all possible?

i had hoped that at the point it would have called the 'open()' function. how could i adapt my code to make this better?

The function has a name at compile time. At run-time, it does not. It has only an address.

You have to parse the serial input to determine what function to call and to get the arguments for the function AS THE CORRECT TYPE (that is, NOT as strings, unless string is the correct type).

Then, you need to call the correct function, based on the string you sent.

Stew:
i was hoping i would be able to declare variables and trigger the response in separate inputs:
<red = 0>
<green = 255>
<blue = 0>
<led()>

That is certainly possible, but I would send the messages as, for example <R,0>

However I still think it would be easier to send all 3 values every time even if some of them have not changed - <0,255,0>

The fact that you are sending from a program you will be writing means that you have full control of the system you use.

…R

serialInput is a globally defined variable declared as int. It is a legitimate (although useless) expression but by itself will not cause any functions to be called.