Serial commands

I've been trying to control trigger actions for my arduino to do with specific commands through the serial monitor. I haven't been able to get anything close to success though. Pretty much I'm starting out lighting an LED by typing "on" or even just a number. But when I make a for case to check the serial port and check if it matches the command the arduino thinks anything matches.

example: I try to set variable A as an integer so when I enter it in serial it makes it a 65. If I make a case saying light this LED if I type something equal to that, it triggers not just for A, but for every single number and letter on the keyboard.

Any help?

When you send a number or a char to the arduino, it gets it as ascii code of the char there is a that can help you with it: http://www.asciitable.com/index/asciifull.gif

here is an example:

switch(Serial.read()) { case 48: //when arduino get '0' it will be executed break; case 104: //when arduino get 'w' it will be executed break; }

here is an example:

switch(Serial.read())
{
case 48:
  //when arduino get '0' it will be executed
break;
case 104:
  //when arduino get 'w' it will be executed
break;
}

which would be a lot easier to read (and to know what to send to the Arduino) as:

switch(Serial.read())
{
case '0':
  // Do something
break;
case 'w':
  // Go west
break;
}

example: I try to set variable A as an integer so when I enter it in serial it makes it a 65. If I make a case saying light this LED if I type something equal to that, it triggers not just for A, but for every single number and letter on the keyboard.

Any help?

You can't enter anything in serial. Only computers can do that. So, you need to be explicit as to what application is taking what you type and putting it on the serial port.

If it is the Serial Monitor, then the problem is most likely with your code. Unfortunately, my son's cat knocked my crystal ball off the counter. Until it gets back from the repair shop, I can't help you. Unless you wanted to post your code, that is.

is that the only way to use typed input to execute actions? I'd really like to use an if control structure if possible.

PaulS:

example: I try to set variable A as an integer so when I enter it in serial it makes it a 65. If I make a case saying light this LED if I type something equal to that, it triggers not just for A, but for every single number and letter on the keyboard.

Any help?

You can't enter anything in serial. Only computers can do that. So, you need to be explicit as to what application is taking what you type and putting it on the serial port.

If it is the Serial Monitor, then the problem is most likely with your code. Unfortunately, my son's cat knocked my crystal ball off the counter. Until it gets back from the repair shop, I can't help you. Unless you wanted to post your code, that is.

well yea, I can show my code, but really my issue is more of how can I make my arduino do something from typing in a full word like "on"

is that the only way to use typed input to execute actions? I’d really like to use an if control structure if possible.

You’ll need to elaborate on what this means.

well yea, I can show my code, but really my issue is more of how can I make my arduino do something from typing in a full word like “on”

How will you distinguish “on” from “only once”? You can collect all the serial data in an array, until the array contains something that you recognize (“on”) but that leads to the problem that every command must be unique (i.e. no common letters at the start of a command). How would you deal with serial data that got corrupt/lost?

Now, if you were willing to add a start of packet and end of packet marker ("", “”, etc.), then it is a lot easier. Read (and discard) serial data until a start of packet marker arrives. When a start of packet marker arrives. set a flag, initialize the array, and set the index to 0.

Keep reading serial data, adding to the array or discarding, depending on whether or not the start of packet marker has arrived, checking each character for the end of packet marker. If it is not the end of packet marker, add it to the array (if there is room). If it is the end of packet marker, set a flag.

Once all pending serial data has been read, on each pass through loop, see if the started and ended flags are set. If so, parse the array, and do whatever. If not, the data in the array is not ready to be parsed.

I could post an example, if you need.

PaulS:

is that the only way to use typed input to execute actions? I’d really like to use an if control structure if possible.

You’ll need to elaborate on what this means.

well yea, I can show my code, but really my issue is more of how can I make my arduino do something from typing in a full word like “on”

How will you distinguish “on” from “only once”? You can collect all the serial data in an array, until the array contains something that you recognize (“on”) but that leads to the problem that every command must be unique (i.e. no common letters at the start of a command). How would you deal with serial data that got corrupt/lost?

Now, if you were willing to add a start of packet and end of packet marker ("", “”, etc.), then it is a lot easier. Read (and discard) serial data until a start of packet marker arrives. When a start of packet marker arrives. set a flag, initialize the array, and set the index to 0.

Keep reading serial data, adding to the array or discarding, depending on whether or not the start of packet marker has arrived, checking each character for the end of packet marker. If it is not the end of packet marker, add it to the array (if there is room). If it is the end of packet marker, set a flag.

Once all pending serial data has been read, on each pass through loop, see if the started and ended flags are set. If so, parse the array, and do whatever. If not, the data in the array is not ready to be parsed.

I could post an example, if you need.

I was talking about wanting to use if (conditions){action} to check if something i typed matched any stored options rather than use the “switch case” thing. Examples are always appreciated man.

I was talking about wanting to use if (conditions){action} to check if something i typed matched any stored options

Easy. How will you be storing these options?

Some sample code:

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

int index = 0;
#define ASIZ 24
char inData[ASIZ]; // Size as needed

void loop()
{
   while(Serial.available() > 0)
   {
      int inChar = Serial.read();
      if(inChar == SOP)
      {
         started = true;
         ended = false;
         index = 0;
         inData[index] = '\0';
      }
      else if(inChar == EOP)
      {
         ended = true;
      }
      else
      {
         if(index < ASIZ)
         {
            inData[index] = inChar; // Append the character
            index++;
            inData[index] = '\0'; // Append a NULL
         }
      }
   }

   if(started && ended)
   {
      if(strcmp(inData, "on") == 0)
      {
         // Turn something on
      }
      else if(strcmp(inData, "off") == 0)
      {
         // Turn something off
      }
   }
}

This can easily be expanded to be able to handle input like “Pin 7 on” or “Pin 9 127”. Look at strtok() for extracting one or more tokens from a string.

but really my issue is more of how can I make my arduino do something from typing in a full word like "on"

Some simple code to try.

// 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(1);  
    char c = Serial.read();
    readString += c; 
  }

  if (readString.length() >0) {
    Serial.println(readString);

    if (readString == "on")     // assuming you send character '1', not a byte with value 1 
    {
      digitalWrite(ledPin, HIGH);
    }
    if (readString == "off")
    {
      digitalWrite(ledPin, LOW);
    }

    readString="";
  } 
}