[quote author=Nick Gammon link=topic=89212.msg675103#msg675103 date=1328161089] This works with what I presume is your data:
/*
Author: Nick Gammon
Date: 2nd February 2012
example input: .123,100,92,42,99/
*/
// how much serial data we expect before a terminator
const unsigned int MAX_INPUT = 100;
const char TERMINATOR = '/';
const char STARTER = '.';
void setup ()
{
Serial.begin(115200);
} // end of setup
int middle, thumb, ring, pointer, rotation;
// here to process incoming serial data after a terminator received
void process_data (char * data)
{
// convert strings into numbers:
middle = atoi (strtok (data, ","));
thumb = atoi (strtok (NULL, ","));
ring = atoi (strtok (NULL, ","));
pointer = atoi (strtok (NULL, ","));
rotation = atoi (strtok (NULL, ","));
// control the servos here!
// for now I am displaying what I got ...
Serial.print ("middle = ");
Serial.println (middle);
Serial.print ("thumb = ");
Serial.println (thumb);
Serial.print ("ring = ");
Serial.println (ring);
Serial.print ("pointer = ");
Serial.println (pointer);
Serial.print ("rotation = ");
Serial.println (rotation);
} // end of process_data
void loop()
{
static char input_line [MAX_INPUT];
static unsigned int input_pos = 0;
if (Serial.available () > 0)
{
char inByte = Serial.read ();
switch (inByte)
{
case TERMINATOR: // end of text
input_line [input_pos] = 0; // terminating null byte
// terminator reached! process input_line here ...
process_data (input_line);
// reset buffer for next time
input_pos = 0;
break;
case STARTER:
// reset buffer
input_pos = 0;
break;
default:
// keep adding if not full ... allow for terminating null byte
if (input_pos < (MAX_INPUT - 1))
input_line [input_pos++] = inByte;
break;
} // end of switch
} // end of incoming data
// do other stuff here like testing digital input (button presses) ...
} // end of loop
if (Serial.available () > 0) *change this to something like: if (Serial.available () > 8) because there are 9 characters *
No. It gets a byte at a time. That's the WHOLE idea.
char inByte = Serial.read (); *times this by 9? *
No. It adds to a buffer. You don't do 9 reads. How do you know it is 9? How do you know in advance if you are getting 1-digit, 2-digit, 3-digit numbers? Try to think through what you are sending. [/quote] Alright. So do i even need to send the commas? So i am sending 5 sensor values, once it reads those it store them in a buffer? but then how do i access them and move the servos with them?