Extracting integers from serial communcation

I've written python code that will transmit a series of integers through the serial interface. For the moment, all integers will be single digit, until i decide they ought be larger. The code i've written will transmit numbers in sets of 4, but delimiters can vary as per my choosing. A couple of possible transmittance options are listed below:

-1 -1 -1 -1
1,1,1,1
3,5,-4,6
....9,,,,-9,,,,,,8,,,,,6

Basically, i need to separate integers, identify if they are positive or negative (and make them such), and assign them to variables. I've attempted to write code that does this for one variable, but cannot solve the problem for negative integers or multiple values. My code is below. I suspect that once i've solved it for one integer it can be replicated for multiple variables, so the trick is to identify and isolate individual integers. Ignore LED2-LED4, they are placeholders for the other integers i intend to send.

I defer to your expertise, since my experience is limited. Any ideas?

/* Use a variable called byteRead to temporarily store
   the data coming from the computer */
int byteRead;
int LED1;
int LED2;
int LED3;
int LED4;

int num1 = 2;
int num2 = 3;
int num3 = 4;
int num4 = 5;
void setup() {                
// Turn the Serial Protocol ON
  Serial.begin(9600);
}

void loop() {
   /*  check if data has been sent from the computer: */
  while (Serial.available() > 0) {
    /* read the most recent byte */
    byteRead = Serial.read();  // grabs first integer
    while (byteRead > 57 || byteRead != 45 && byteRead < 48){
      byteRead =Serial.read();
    }
    switch (byteRead) {
      case 45:
          byteRead = Serial.read();
          Serial.println(byteRead);
          LED1 = -1 * (byteRead - 48);
          break;
      default: 
          LED1 =  byteRead - 48;
 
     
  }
  Serial.println(LED1);
  }
  
 if (LED1 > 0){
  digitalWrite(num1, HIGH);
 } 
 else{
 digitalWrite(num1, LOW);
 }
}

I would save all the incoming data into a char array and when it is all received I would go through the array to pick out the number characters between the delimiters. You can use parseInt() to convert characters to integers.

By saving the data before you try to interpret it you can go through the data a few times if necessary.

Keep your delimiters as simple as possible - the comma is very common.

...R

    while (byteRead > 57 || byteRead != 45 && byteRead < 48){

Your order here is all wrong. First, replace the ASCII values with characters, so it is obvious what you are doing:

    while (byteRead > '9' || byteRead != '-' && byteRead < '0')
    {

Now, it's clear that the order of tests is wrong, and that some of the tests are wrong. What you want is:

    while((byteRead >= '0' && byteRead <= '9') || byteRead == '-')
    {

Then, in the body of the while loop, you deal with the character read, and possibly read another one.

I've corrected the criteria for the while loop (shown below), but still cant get in code to produce the proper integers. I suspect it's something to do with converting the ASCII characters that are sent via the serial interface into integers. My code thus far is just echoing back jibberish, that i can't seem to make sense of. Ideas?

/* Use a variable called byteRead to temporarily store
   the data coming from the computer */
int byteRead;
int LED1;
int LED2;
int LED3;
int LED4;

int num1 = 2;
int num2 = 3;
int num3 = 4;
int num4 = 5;

boolean neg = false;
void setup() {                
// Turn the Serial Protocol ON
  Serial.begin(9600);
}

void loop() {
   /*  check if data has been sent from the computer: */
  while (Serial.available() > 0) {
    /* read the most recent byte */
    byteRead = Serial.read();  // grabs first integer
    Serial.println(byteRead);
    while (byteRead == '-' || (byteRead >= '0' && byteRead <= '9')){
      byteRead =Serial.read();
      Serial.println(byteRead);
    }
    if (byteRead == '-') {
          byteRead = Serial.read();
          Serial.println(byteRead);
          LED1 = -1 * (byteRead - '0');
    }
      else{ 
          LED1 =  byteRead - '0';
 
      }  
      Serial.println(LED1);
  }
  

  
 if (LED1 > 0){
  digitalWrite(num1, HIGH);
 } 
 else{
 digitalWrite(num1, LOW);
 }
}

Serial.read() reads a char or a byte not an integer. Integers are 2 bytes long.

You need to tell us what is the exact format of the data being sent to the Arduino. In your earlier post you had three or four alternatives.

You are reading the first char before the while loop and you are discarding it. There is no need to read a char before the while loop. You could start with byteRead = 'Z' or anything that won't upset the while test.

...R

I was under the impression that Serial.read() will pull the most recent byte of data, which in this case should be the ASCII code for the data being sent. I.e. if i send 1, it codes that to 49. If i send -1, it codes that into 2 bytes, 45 followed by 49.

I have to call Serial.read() before the 2nd while loop (which checks for negatives and integers) so that it can disregard anything that isn't in that category. I suspect the issue is something to do with the data types (converting char to int or vice versa).

The data is being transmitted via python code, and the format is negotiable. The critical part is that it will come as a series of 4 numbers, some positive and some negative. Right now my code sends in this format:

3 -1 2 1
6 3 -1 0

The 4th digit is always 1 or 0. I can include any delimiters that i see fit between the integers. The format is whatever i'd prefer to send.

lets say for argument's sake, i send this:

4, -1, 2, 1

I need these assigned to 4 individual variables. After the variables are used, they are discarded and a new set of 4 are assigned to those variables.

You are correct in describing how the data will be received except that you forgot to note that the space character will also be received. Your plan to read a single character before the 2nd while loop won't do what you want, except by chance. Most of the time it will probably lose a piece of your data. On the other hand if there are several garbage characters in the input buffer ahead of your data it will only remove one of them.

In the Thread in the following link I have posted a pair of demo programs (Python and Arduino) that send data between a PC and an Arduino.
http://forum.arduino.cc/index.php?topic=225329.0

It illustrates how to surround the data with marker bytes to enable the Arduino (or the PC) to ignore any garbage that arises before the actual message.

Once you have received all the data it should be a simple matter to go through it to extract and convert characters to integers.

...R

Using starting and ending characters is a great idea for "encapsulating" the data i'm sending. Hopefully i can get that working. Thanks for the tip!