My Arduino Software Solutions provide numerous ways to read text from Serial with their pros and cons
The non-blocking readLine sketch could be used
String input;
// Motor A
int enA = 9;
int in1 = 8;
int in2 = 7;
// Motor B
int enB = 3;
int in3 = 5;
int in4 = 4;
void setup() {
Serial.begin(9600);
for (int i = 10; i > 0; i--) {
Serial.print(' '); Serial.print(i);
delay(500);
}
Serial.println();
Serial.println("Type A or B followed by time in sec, e.g. A55 or A 55");
input.reserve(10); // pre-allocate some space
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}
// read Serial until until_c char found, returns true when found else false
// non-blocking, until_c is returned as last char in String, updates input String with chars read
bool readStringUntil(String& input, char until_c) {
while (Serial.available()) {
char c = Serial.read();
input += c;
if (c == until_c) {
return true;
}
}
return false;
}
void loop() {
if (readStringUntil(input, '\n')) {
// go a line of input
input.trim(); // remove any \r and leading/trailing space
input.toUpperCase(); // a -> A etc
char AB_type = input[0];
input.remove(0, 1); // remove the char
input.trim();
long time = input.toInt(); // to int actually returns a long
if (input != String(time)) {
// invalid time
Serial.print(input); Serial.println(F(" not a valid time"));
} else {
if (AB_type == 'A') {
Serial.print(F("Picked A")); Serial.print(F(" with time:")); Serial.println(time);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(enA, 200);
delay (time * 1000);
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
} else if (AB_type == 'B') {
Serial.print(F("Picked B")); Serial.print(F(" with time:")); Serial.println(time);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enB, 200);
delay (3000);
delay(time * 1000);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
} else {
Serial.print(AB_type); Serial.println(F(" invalid type, must be A or B followed by time in sec"));
}
}
input = ""; // ALWAYS clear for next read
}
// other loop stuff here
}