Sending X/Y Values from serial to Arduino Mega

Im a complete noob when it comes to the Arduino, and C for that matter. Come from a PHP web background.
However im writing a sketch that is controlling two LED strips from an Arduino Mega. Ive already gotten this down and everything is working great!

Each strip is an Axis, X/Y, im wanting to send an X and Y Value from the a computer to the arduino via Serial. Then use these values as my LED to light up. I can manage all the LED side of it and what not, what i cant figure out is how to send more than one byte to the Arduino at a time. As of now i can only send 1-9, and double digits (10) is processed as (1, then 0). And on top of this i don't even begin to know how to separate these values once i can get them into to sketch.

Just simplified i want to send something like (15,22) to the Arduino, then reference these values Separately (Maybe with a substring command?) as (X=15; Y=22;) and use the values in my Sketch to control LEDS.

Any help is appreciated!

Well, the easiest way: you could use the Serial.read() and use Arduino console to enter input.

The OP in this Thread has a similar question and I have made some suggestions there that may also be of interest to you.

...R

Edit to correct link - apologies

Robin2:
The OP in this [urlhttp://forum.arduino.cc/index.php?topic=269917.0]Thread[/url] has a similar question and I have made some suggestions there that may also be of interest to you.

...R

Using the CSV Demo i have gotten a little closer to what im trying to do. However this runs non stop, i just want it to run when i send it the data (x,y) Then stop until i send it new data. Right now it takes my x/y then continues as 0,0 over and over till the next command.

And when i try to say
if (Serial.available () > 0) {
readCSV();
delay(800);
}

Then it splits my data up and does not interpret my x,y correctly.

Any help is appreciated!

Heres my code based on your demo, all i want is to pass it (15,22) and it to set my X as 15 and Y as 22. Until i send it a new set of values then update. Not continues updates like it is now.

// Written by Robin2 April 2014


//===================
// global variables used in the various functions

char inputChar = 'X';
byte inputByte = 255;

const byte buffSize = 32;
char inputSeveral[buffSize]; // space for 31 chars and a terminator

byte maxChars = 12; // a shorter limit to make it easier to see what happens
                           //   if too many chars are entered

int inputInt = 0;
float inputFloat = 0.0;
char inputCsvString[12];


//===================

void setup() {
  Serial.begin(9600);
  Serial.println("Starting DemoDataInput.ino");
}

//===================

void loop() {


readCSV();
  
  delay(800);

  
}

//===================
void readCSV() {

      // this function expects a series of comma separated values
      // for this demo the sequence of items must be a string, an integer, a float
      // for example testing, 123 , 4567.89
      // spaces around the commas are optional
   
        // first read severalChars into the array inputSeveral

    inputSeveral[0] = 0;
    maxChars = buffSize - 1; // use full size of buffer for this function
    byte charCount = 0;  
    byte ndx = 0;        
    
    if (Serial.available() > 0) {
      while (Serial.available() > 0) { 
        if (ndx > maxChars - 1) {
          ndx = maxChars;
        } 
        inputSeveral[ndx] = Serial.read();
        ndx ++;        
        charCount ++;
      }
      if (ndx > maxChars) { 
        ndx = maxChars;
      }
      inputSeveral[ndx] = 0; 
    }

      // now we need to split the received string into its parts
      // this is done by strtok() which looks for the token - the comma in this case

    char * partOfString; // this is used by strtok() as an index
    int x;
    int y;
    
    partOfString = strtok(inputSeveral,",");      // get the first part - the string
    x = atoi(partOfString);
    
    partOfString = strtok(NULL, ","); // this continues where the previous call left off
    y = atoi(partOfString);
    

    

    Serial.print("x -- ");
    Serial.print(x);
    Serial.print("y -- ");
    Serial.print(y);


}

//===================

I know it is not what you want but the code is doing what it is told to do (as it usually does).

Part of the problem is that you don't have anything to "consume" the data you enter.

The revised version below has moved the printing into a separate function called showResults() to give you an idea of how you might add on a function that consumes the input data. Note that I have moved the definition of X and Y to the top so they are global variables that can be accessed by the showResults() function.

I have also added an IF in showResults() so it only prints the data if one of the values in > 0. Again this is just intended as a crude illustration - obviously it would be useless if values 0,0 are important.

By the way my code for reading the data is rather crude compared to the version in this demo and, because it is crude, it really does need a short delay() in loop(). The demo you are using was really only intended to show the parsing.

To move forward you will have to decide what you want to do with the input data as that will determine how you control the repeating inputs.

// Written by Robin2 April 2014
// modified 01 Oct 2014


//===================
// global variables used in the various functions

const byte buffSize = 32;
char inputSeveral[buffSize]; // space for 31 chars and a terminator

byte maxChars; 

int inputInt = 0;
float inputFloat = 0.0;
char inputCsvString[12];
int x;
int y;


//===================

void setup() {
  Serial.begin(9600);
  Serial.println("Starting DemoDataInput.ino");
}

//===================

void loop() {
  readCSV();
  showResults();
  
  delay(100);
}

//===================
void readCSV() {

      // this function expects a series of comma separated values
      // for this demo the sequence of items must be a string, an integer, a float
      // for example testing, 123 , 4567.89
      // spaces around the commas are optional
   
        // first read severalChars into the array inputSeveral

    inputSeveral[0] = 0;
    maxChars = buffSize - 1; // use full size of buffer for this function
    byte charCount = 0;  
    byte ndx = 0;        
    
    if (Serial.available() > 0) {
      while (Serial.available() > 0) { 
        if (ndx > maxChars - 1) {
          ndx = maxChars;
        } 
        inputSeveral[ndx] = Serial.read();
        ndx ++;        
        charCount ++;
      }
      if (ndx > maxChars) { 
        ndx = maxChars;
      }
      inputSeveral[ndx] = 0; 
    }

      // now we need to split the received string into its parts
      // this is done by strtok() which looks for the token - the comma in this case

    char * partOfString; // this is used by strtok() as an index

    
    partOfString = strtok(inputSeveral,",");      // get the first part - the string
    x = atoi(partOfString);
    
    partOfString = strtok(NULL, ","); // this continues where the previous call left off
    y = atoi(partOfString);
}    

void showResults() {
    if (x > 0 || y > 0) {
      Serial.print("x -- ");
      Serial.print(x);
      Serial.print("y -- ");
      Serial.println(y);
    }

}

//===================

...R