Switch-Case does not run my function?? (ESP8266 + Arduino over softwareserial)

Hello all, I'm in the process of building a simple Wi-Fi controlled robot, and trying to get the electronics and programming together before I start 3d printing parts.

So far, I have established a working Wi-Fi connection on an ESP8266. I can access a webpage, on which I can click a link and tell the robot to go forward, reverse, left, right, or idle. Clicking a link makes the ESP send a char (i, f, b, l, r) over the serial, to my Nano's d11 and d12 pins (softwareserial).

I can get the values into the Arduino just fine, but once I try comparing the char in a switch-case statement, the Arduino hardware serial (USB for debugging) just prints the char and nothing else. I've tried using separate functions, and even just getting the switch-case statement to print. Not sure why it's not working, anyone know what I'm doing wrong here?

Here's my current Arduino code, including all my trial-and-error stuff, so ignore the FuncCalls:

#include <SoftwareSerial.h>

SoftwareSerial ESPSerial(11, 12); // RX, TX
//Function prototypes - required by compiler
String loading;
char command;

/*
typedef void (* Caller)();
Caller FuncCall[] = {&i, &f, &b, &l, &r}; //initialize addresses for pointers
char func_list[] = {'i', 'f', 'b', 'l', 'r'};  //use this instead - if you wish
*/

//Setup functions
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  // set the data rate for the SoftwareSerial port
  ESPSerial.begin(9600);

  Serial.println("Waiting for ESP...");
  
  loading = "loading";

  while (!loading.equals("Ready.")) {   //Wait for ESP to send ready before running loop
    loading = ESPSerial.readString();
    Serial.print(loading);
  }
}

//Movement Functions
void i() {    //Idle
  Serial.println("Called Function: i");
}

void f() {    //Move forward
  Serial.println("Called Function: f");
}

void b() {    //Move backward
  Serial.println("Called Function: b");
}

void l() {    //Turn left function
  Serial.println("Called Function: l");
}

void r() {    //Turn right function
  Serial.println("Called Function: r");
}

//Loop functions
void loop() {
  if (ESPSerial.available()) {
    switch (ESPSerial.read()) {
      case 'i':
        //  FuncCall[0]();
        i();
        break;

      case 'f':
        //  FuncCall[1]();
        f();
        Serial.println("Running function: f");
        break;

      case 'b':
        //  FuncCall[2]();
        b();
        Serial.println("Running function: b");
        break;

      case 'l':
        //  FuncCall[3]();
        l();
        Serial.println("Running function: l");
        break;

      case 'r':
        //  FuncCall[4]();
        r();
        Serial.println("Running function: r");
        break;

      default:
        Serial.println("There was an error\n");
        break;
    }


  }
}

This is what I get in over my Arduino hardware serial. It should print "Running function : r" in this case, but no dice.

Image

Any help would be appreciated, thanks!

Before you go much further, make sure you're fully familiar with the F() macro, and forget you ever heard about the String class.

Have you tried printing what ESPSerial.read() returns ?

Don't use single character names - they may be confused with Arduino usage. In any case you should always use meanigful names for everything. eg void moveBackward()

You have no code to tell you if a suitable character has been received before you try to SWITCH with it.

Print the received character and the problem may be obvious.

incomingChar = ESPSerial.read();
Serial.print("IncomingChar = ");
Serial.println(incomingChar);

switch(incomingChar) {

...R

Wow, I got it to work. Your tips got me on the right track, really appreciate the help!

Apparently I created an infinite while loop, and the Arduino just kept waiting for the Ready but interpreted it wrong (?).

Got it past the setup now and all is well. Changed my code to this:

//Setup functions
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  // set the data rate for the SoftwareSerial port
  ESPSerial.begin(9600);

  Serial.println(F("Waiting for ESP..."));

  loading = F("");
  while (!loading.equals(F("Ready."))) {   //Wait for ESP to send ready before running loop
    if (ESPSerial.available()) {       
      loading = ESPSerial.readStringUntil('\n');
      Serial.println(loading);
    }
  }
  //Wait for Serial to clear
  while(ESPSerial.available()){
    ESPSerial.read();
    }
}

Thanks for the tips, learn something new every day :slight_smile: Gotta love being a student.