How to make the serial read more than one value?

Hello

I want my serial to read diferent stuff that I input like:

int [x, y, z] = Serial.read();

So then on Serial Monitor I may use values for my variables like : "1, 2, 3"

All in all what I dont know is how to make the serial read more than one value?

Anyone can help me?

Like this?

#define X    0
#define Y    1
#define Z    2

char array[3]

for (char i = 0; i <3 ; i++) array [i] = Serial.read();

Although this is not what I would call data transfer.

Ok I will get more specific, heres the code:

void setup() {

Serial.begin(9600); // initialize serial communication: }

void loop() {

if (Serial.available() > 0) {

int freq, duration = Serial.read();

// play a note on pin 8 for 300 ms: tone(8, freq, duration); delay(300);

I just want to input the frequency and the duration on the serial monitor. But the code above does nothing. Sorry, I am kind of new to programming.

Part of the code, huh?

This line:

    int freq, duration = Serial.read();

... defines two variables. freq has an undefined value, and duration is assigned the results from Serial.read ().

Closer to your objective would be:

if (Serial.available() > 1) { 
   
    int freq = Serial.read ();   
    int duration = Serial.read ();
}

Note that all of these suggestions so far will read a one digit number for freq and duration.

“1200, 30” will NOT result in freq = 1200 and duration = 30. That is a completely different process.

Good point. My code would read from your example of "1, 2, 3" a "1" into the first variable and a "," into the second one.

[quote author=Nick Gammon link=topic=76109.msg574624#msg574624 date=1319108508] Good point. My code would read from your example of "1, 2, 3" a "1" into the first variable and a "," into the second one. [/quote]

In case the op doesn't know... "1" = 49.

Arduino is probably the highest level programming language for 8 bit microcontrollers, but it is still not that high. So you have to do some of the dirty work to achieve what you want.

In case the op doesn't know... "1" = 49.

sp. '1' ;)

Ok this is all still very confusing to me.

Two things I think I got it:

First I need to convert the ASCII char to hexadecimal.

Second I am still confused as to read 2 values on the serial.read.

So: "1200, 30" will NOT result in freq = 1200 and duration = 30

How do I do that?

I wish Arduino Software would have a better interface using Serial Monitor. Something like a ASCII to hex converter. I mean my programming skills are based on trial and error ( much of it) and interaction with the serial comm is the best way to test things.

Anyway, I read the article on the blog, but as expected i became very confused.

The point is I want to test tones at diferent frequencies and durations using serial interface. Anyone knows a good solution?

First I need to convert the ASCII char to hexadecimal.

Why do you think you need to do this? What is sending the data?

How do I do that?

I'm not in the habit of posting random useless links. Read the link I posted.

Something like a ASCII to hex converter.

The Arduino can convert a character array containing hex characters to a value.

but as expected i became very confused.

Read it again, then. It is pretty straightforward. If you have questions, ask.

Anyone knows a good solution?

If you mean "does anyone want to write the code for me", no.

The below code is for servos, but it shows a simple way to send two values together from the serial monitor, split the values, and convert the vaues into numbers for use with the servos.

// zoomkat 10-20-11 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 0022 and later
// Powering a servo from the arduino usually DOES NOT WORK.
// two servo setup with two servo commands
// send eight character string like 15001500 or 14501550

#include <Servo.h> 
String readString, servo1, servo2;
Servo myservo1;  // create servo object to control a servo 
Servo myservo2;

void setup() {
  Serial.begin(9600);
  myservo1.attach(6);  //the pin for the servo control 
  myservo2.attach(7);
  Serial.println("servo-test-22"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(1);  
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    } 
  }

  if (readString.length() >0) {
      Serial.println(readString); //see what was received
      
      // expect a string like 07002100 containing the two servo positions      
      servo1 = readString.substring(0, 4); //get the first four characters
      servo2 = readString.substring(4, 8); //get the next four characters 
      
      Serial.println(servo1);  //print to serial monitor to see results
      Serial.println(servo2);
      
      int n1; //declare as number  
      int n2;
      
      int n1 = servo1.toInt();
      int n2 = servo2.toInt();
      
      myservo1.writeMicroseconds(n1); //set servo position 
      myservo2.writeMicroseconds(n2);
    readString="";
  } 
}

Thanks all for the help.

zoomkat: Straight to the point! I like it! Your example is very much appreciated, and serves me right ;)

Just be aware that zoomkat's example may not work reliably if you change the serial line speed.