Controlling an Arduino via serial

Hello, I'm trying to controll my Arduino via serial monitor, but I want to do it in specific way. I need to send instructions trough the serial monitor like "1/2/3" and this should tell Arduino, which pin do I want to controll, do I want to read from it or write to it etc, using the switch statement. Now, I came across one very good tutorial but it doesn't work for me. Ironically it is the exact thing that I need for my project. Tutorial can be found here. When I use that code, I don't get output numbers that I expect. So I changed the original function:

long getSerial()
{
  serialdata = 0;
  while (inbyte != '/')
  {
    inbyte = Serial.read(); 
    if (inbyte > 0 && inbyte != '/')
    {
     
      serialdata = serialdata * 10 + inbyte - '0';
    }
  }
  inbyte = 0;
  return serialdata;
}

with this (I added inbyte=Serial.parseInt()):

long getSerial()
{
  serialdata = 0;
  while (inbyte != '/')
  {
    inbyte = Serial.parseInt(); 
    if (inbyte > 0 && inbyte != '/')
    {
     
      serialdata=inbyte;
      Serial.println(serialdata);
    }
  }
  inbyte = 0;
  return serialdata;
}

Now the output numbers are OK, but program doesn't go trough the switch statement. Can anybody suggest me the way to do this. I tought I coud somehow separate that command and make extra variable for each swich statment I use in code, but I don't know how to do it. And yes it has to proceed as one command wich should be separated later. I want to control Arduino with PLC. I've google it and searched the forum, found simillar questions and good answers but there is nothing that could help me. Also, to understand what I need please check the link above.

Note that this is my first question on this forum so don't be too hard on me if I post it in wrong section or these question was already answered somewhere. Also, I never done anything like this on Arduino. If there is any grammar mistake in this post, blame it on English being my foreing language that I don't use often enugh. Thank you for your time and replies.

mmunic194: but program doesn't go trough the switch statement.

What switch statement? I don't see any switch statement. Am I supposed to guess at why it doesn't work?

By the way, maybe you can help me out too. I'm writing a paper and it says I've mispelled a word. How do I fix that word?

Maybe it would help if you posted the code. Go ahead and post it all becuase the problem might not be where you think it is.

Or try looking here: http://snippets-r-us.com/

There is a better serial tutorial at the second link at the bottom of this post. It's way more than I can post here.

In the state machine part, Nick shows how to use character+number text codes.

I can't recommend just grabbing a sketch from it without first learning how it works though, but I'm just funny like that.

The examples in serial input basics will probably do what you want.

...R

  while (inbyte != '/')
  {
    inbyte = Serial.parseInt();

inByte will NEVER be '/', so the test is silly.

Delta_G: What switch statement? I don't see any switch statement. Am I supposed to guess at why it doesn't work?

By the way, maybe you can help me out too. I'm writing a paper and it says I've mispelled a word. How do I fix that word?

Maybe it would help if you posted the code. Go ahead and post it all becuase the problem might not be where you think it is.

Or try looking here: http://snippets-r-us.com/

I haven't post the whole code, I've posted a link to the page where is EXACT CODE only thing that is changed is my function and that change I've posted. And here it is:

unsigned long serialdata;
int inbyte;
int digitalState;
int pinNumber;
int analogRate;
int sensorVal;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  getSerial();
  switch(serialdata)
  {
  case 1:
    {
      //analog digital write
      getSerial();
      switch (serialdata)
      {
      case 1:
        {
          //analog write
          getSerial();
          pinNumber = serialdata;
          getSerial();
          analogRate = serialdata;
          pinMode(pinNumber, OUTPUT);
          analogWrite(pinNumber, analogRate);
          pinNumber = 0;
          break;
        }
      case 2:
        {
          //digital write
          getSerial();
          pinNumber = serialdata;
          getSerial();
          digitalState = serialdata;
          pinMode(pinNumber, OUTPUT);
          if (digitalState == 1)
          {
            digitalWrite(pinNumber, LOW);
          }
          if (digitalState == 2)
          {
            digitalWrite(pinNumber, HIGH);
          }
          pinNumber = 0;
          break;
         
        }
     }
     break; 
    }
    case 2:
    {
      getSerial();
      switch (serialdata)
      {
      case 1:
        {
          //analog read
          getSerial();
          pinNumber = serialdata;
          pinMode(pinNumber, INPUT);
          sensorVal = analogRead(pinNumber);
          Serial.println(sensorVal);
          sensorVal = 0;
          pinNumber = 0;
          break;
        } 
      case 2:
        {
          //digital read
          getSerial();
          pinNumber = serialdata;
          pinMode(pinNumber, INPUT);
          sensorVal = digitalRead(pinNumber);
          Serial.println(sensorVal);
          sensorVal = 0;
          pinNumber = 0;
          break;
        }
      }
      break;
    }
  }
}

long getSerial()
{
  serialdata = 0;
  while (inbyte != '/')
  {
    inbyte = Serial.parseInt(); 
    if (inbyte > 0 && inbyte != '/')
    {
     
      serialdata = inbyte;
    }
  }
  inbyte = 0;
  return serialdata;
}

So if anyone have an idea please help.

PaulS:   while (inbyte != '/')   {     inbyte = Serial.parseInt();

inByte will NEVER be '/', so the test is silly.

I'm aware of that too..

Robin2: The examples in serial input basics will probably do what you want.

...R

Thank you, this is very useful!

I answered your question in the serial input basics thread before I realized you had this Thread. Please don’t split your questions - it just wastes time.

To save trouble, following is the answer from the other Thread

Quote from: mmunic194 on Today at 13:58:58

For example: if I send “<1><2><3>” over serial monitor, how could I store that in to variables like x=1, y=2, z=3?

It would be possible, but not so easy because the code will treat each value as the start of a new set of data.
But the demo code does the same thing using <1,2,3> so why not use that system?

Perhaps I don’t understand what you want to do?

…R

I understand that you tried <1,2,3> and it did not do what you wanted. Please post the latest version of your code and a copy of the output from it.

…R

Robin2:
I understand that you tried <1,2,3> and it did not do what you wanted. Please post the latest version of your code and a copy of the output from it.

…R

My problem has been solved. I had some problem with my Arduino board. I’m sorry for creating such a mess over nothing. THANK YOU everyone for trying to help!
Regards!