Go Down

Topic: How to make the serial read more than one value? (Read 1 time) previous topic - next topic

sushyoshi

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?

bubulindo

Like this?

Code: [Select]


#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.
This... is a hobby.

sushyoshi

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.

Nick Gammon

Part of the code, huh?

This line:

Code: [Select]
    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:

Code: [Select]
if (Serial.available() > 1) {
   
    int freq = Serial.read ();   
    int duration = Serial.read ();
}

Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

PaulS

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.

http://jhaskellsblog.blogspot.com/2011/05/serial-comm-fundamentals-on-arduino.html

Nick Gammon

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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

bubulindo


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.


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.
This... is a hobby.

AWOL

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

sp. '1'       ;)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

sushyoshi

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?


PaulS

Quote
First I need to convert the ASCII char to hexadecimal.

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

Quote
How do I do that?

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

Quote
Something like a ASCII to hex converter.

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

Quote
but as expected i became very confused.

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

Quote
Anyone knows a good solution?

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

zoomkat

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.

Code: [Select]
// 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="";
 }
}

Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

sushyoshi

Thanks all for the help.

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

AWOL

Just be aware that zoomkat's example may not work reliably if you change the serial line speed.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up