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.
Any help would be appreciated, thanks!