What it is supposed to do is wait for input from the serial monitor, change that into a string, and then compare it and perform different actions based on what is sent through the serial. I am a beginner, please help!
But you never programmed a simple sketch on Arduino ? For example a simple blink of a led?
For a sketch in Arduino you must write 2 functions, setup() and loop()
void setup()
{ // executed once at start
}
void loop()
{ // executed continuously
}
I dont think that is the problem, Im having a hard time declaring the serial input as a variable that can be compared. But I do not just want to compare a single character. I want whole words like hello, help.
HereString input = String(Serial.read()); you have code that is executable, but not in a function..
There a many samples of reading a string or a String from a serial interface available.
Not only (as Awol says) your code that reads serial data is not in a function, but look at the Serial.read() page :
Returns :
the first byte of incoming serial data available (or -1 if no data is available)
how could input be "help" ?
you must read the incoming datas byte after byte while they are available and add them to your String. When all the characters are read (you need to know which characters ends the string ), then you can make the comparison .
You can hack this... It takes a verb, 1 or more parameters, looks up the command index, and executes. It's not designed to be efficient, but designed to be instructive ... And very hackable http://forum.arduino.cc/index.php/topic,147550.0.html
A very simple example of capturing a character string sent from the serial monitor into a String, then evaluating what is captured in the String and acting on what is contained in the String.
// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later
int ledPin = 13;
String readString;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
Serial.println("serial on/off test 0021"); // so I can keep track
}
void loop() {
while (Serial.available()) {
delay(3);
char c = Serial.read();
readString += c;
}
if (readString.length() >0) {
Serial.println(readString);
if (readString == "on")
{
digitalWrite(ledPin, HIGH);
}
if (readString == "off")
{
digitalWrite(ledPin, LOW);
}
readString="";
}
}