I want to send a set of values like this 0,0,0 from the serial monitor to the Arduino and then separate it to go to three LED's how can I do this?
The problem with the serial monitor is that it doesn't automatically send any delimiters, like carriage-return / line-feed.
If you want to send multiple values, it is best to add your own, so sending something like:
<12,34,56>
So, your sketch needs to look for a '< then some digits, then a commas, some more digits, a comma, some more digits, then a '>'
If you are sending characters from the serial monitor, you really don't need anything special to end your string. The arduino can capture the string and parce parce it faster than you can type the the string and hit the enter key.
The arduino can capture the string and parce parce it faster than you can type the the string and hit the enter key.
But how does the Arduino know the string has finished?
(other than "I don't appear to have received a character in 10 / baud-rate milliseconds")
(sp. "parse")
But how does the Arduino know the string has finished?
(other than "I don't appear to have received a character in 10 / baud-rate milliseconds")(sp. "parse")
For what he is wanting to do, he probably doesn't need a lot of over kill that just tends to add confusion. You did get the "parse" right. ![]()
so all I need to do is a serial read and it wil parse itself? I guess that doesn't make sense to me
You can capture a whole string and parse it using the string functions, or use code like below to capture what is between the commas and deal with each segment individually. AWOL can post his code showing his use of the start and stop delimiters.
// zoomkat 10-29-10 simple delimited ',' string parce
// from serial port input (via serial monitor)
// and print result out serial port
// CR/LF could also be a delimiter
// for IDE 0019 and later
String readString;
void setup() {
Serial.begin(9600);
Serial.println("serial-delimit-21"); // so I can keep track of what is loaded
}
void loop() {
//expect a string like wer,qwe rty,123 456,hyre kjhg,
//or like hello world,who are you?,bye!,
while (Serial.available()) {
delay(1); //small delay to allow input buffer to fill
char c = Serial.read(); //gets one byte from serial buffer
if (c == ',') {
break; //breaks out of capture loop at ,
}
readString += c; //makes the string readString
}
if (readString.length() >0) {
Serial.println(readString); //prints string to serial port out
// do other stuff with string here
readString=""; //clears variable for new input
}
}
Something like this? I havn't tested it as I am not near a Arduino if someone could help it would be appriciated:
String readString;
int Index = 0;
char str[Index] = {0,0,0};
int ledPin[3] = {13,8,9};
void setup() {
Serial.begin(9600);
Serial.println("Hope this works"); // so I can keep track of what is loaded
for (int i = 0, i <= 2, i++)
{
pinMode(ledPin[i], OUTPUT);
}
}
void loop() {
//expect a string like wer,qwe rty,123 456,hyre kjhg,
//or like hello world,who are you?,bye!,
while (Serial.available()) {
delay(1); //small delay to allow input buffer to fill
char c = Serial.read(); //gets one byte from serial buffer
if (c == ',') {
break; //breaks out of capture loop at ,
Index++;
}
readString += c; //makes the string readString
}
if (readString.length() >0) {
Serial.println(readString); //prints string to serial port out
// do other stuff with string here
readString = str[Index];
digitalWrite(ledPin[Index], readString);
Index++;
if (Index >= 2)
{
Index = 0;
}
readString=""; //clears variable for new input
}
}
I think I figured it out here is what i am using:
const int NUMBER_OF_FIELDS = 3; // how many comma separated fields we expect
int ledPins[] = {13,12,8};
int fieldIndex = 0; // the current field being received
int values[NUMBER_OF_FIELDS]; // array holding values for all the fields
void setup()
{
Serial.begin(9600); // Initialize serial port to send and receive at 9600 baud
for (int i; i <= 2; i++)
{
pinMode(ledPins[i], OUTPUT);
}
}
void loop()
{
if( Serial.available())
{
char ch = Serial.read();
if(ch >= '0' && ch <= '9') // is this an ascii digit between 0 and 9?
{
// yes, accumulate the value
values[fieldIndex] = (values[fieldIndex] * 10) + (ch - '0');
}
else if (ch == ',') // comma is our separator, so move on to the next field
{
if(fieldIndex < NUMBER_OF_FIELDS-1)
fieldIndex++; // increment field index
}
else
{
// any character not a digit or comma ends the acquisition of fields
// in this example it's the newline character sent by the Serial Monitor
Serial.print( fieldIndex +1);
Serial.println(" fields received:");
for(int i=0; i <= fieldIndex; i++)
{
Serial.println(values[i]);
digitalWrite(ledPins[i], values[i]);
values[i] = 0; // set the values to zero, ready for the next message
}
fieldIndex = 0; // ready to start over
}
}
}
I hope this helps ppl in the future
ardlab, your wish has just been fulfilled. It helped me a lot in a robot hand project.
For new readers, have a look at the examples in Serial Input Basics - simple reliable ways to receive data.
...R