I want to use the Serial Monitor and give commands to the Arduino, so I can control the OUTPUT pins.
I am using if/else statements. Which works fine for single character char variables. But when I use multiple characters, I can not compare anything in the statements.
char data[] = 'a'; //this would work, I can compare this.
char data[] = {"arduino"}; //this I can not compare
So I wanted to use the String Class. But I can not save Serial.Read() to a String. No problem for character strings but for String class it warns me that I can not save an intiger to a String. ('int' to 'String' is ambiguous)
Any ideas how I can save Serial.Read() to a String?
Here is my code:
#include <LiquidCrystal.h> //LCD liblary
// LCD connection rs E do d1 d2 d3
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//pin nomenclature
int aAft = 10;
int aFwd = 9;
int bFwd= 8;
int bAft = 7;
//control inputs for the motor
String ctrl1 = "A FWD"; //A motor FWD
String ctrl2 = "A AFT"; //A motor AFT
String ctrl3 = "B FWD"; //B motor FWD
String ctrl4 = "B AFT"; //B motor Aft
String ctrl5 = "STOP"; //Stop motors
void setup() {
lcd.begin(16,2); //specify the dimensions of the LCD
Serial.begin(9600); //initialize serial port
//pin 10-7 are set as OUTPUT
int pinModes[5] = {10, 9, 8, 7};
for(int i= 0; i < 4; i++)
{
pinMode(pinModes[i], OUTPUT);
}
}
void loop() {
//check if serial is available
if(Serial.available())
{
//read serial and save it to control variable as char type
String control = Serial.read();
//If 1, A motor FWD
if (control.equals(ctrl1)){
lcd.clear();
lcd.print("A motor FWD");
digitalWrite(aAft, LOW);
delay(500);
digitalWrite(aFwd, HIGH);
}
//If 2, A motor AFT
else if (control.equals(ctrl2)){
lcd.clear();
lcd.print("A motor AFT");
digitalWrite(aFwd, LOW);
delay(500);
digitalWrite(aAft, HIGH);
}
//If 3, B motor FWD
else if(control.equals(ctrl3)){
lcd.clear();
lcd.print("B motor FWD");
digitalWrite(bAft, LOW);
delay(500);
digitalWrite(bFwd, HIGH);
}
//If 4, B motor AFT
else if (control.equals(ctrl4)){
lcd.clear();
lcd.print("B motor AFT");
digitalWrite(bFwd, LOW);
delay(500);
digitalWrite(bAft, HIGH);
}
//If 5, all motors stop
else if (control.equals(ctrl5)){
lcd.clear();
lcd.print("Stop all motors");
digitalWrite(aFwd, LOW);
digitalWrite(aAft, LOW);
digitalWrite(bFwd, LOW);
digitalWrite(bAft, LOW);
delay(1000);
lcd.clear();
lcd.print("Enter command");
}
}
}