Pages: [1]   Go Down
Author Topic: Serial Method/Function To Wait/Read Multiple Character Input?  (Read 870 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 71
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello guys! I'm really sorry if I got the topic wrong and if this is a noob question. I'm a little new.
I'll start off with my code:
Code:
/*
Simple Serial Communication
*/

int val;

int ledPin = 13;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  while(Serial.available()==0);
  val = Serial.read()-'0';
      Serial.print("The value you entered is: ");
      Serial.println(val);
}

If I enter 215, I get back:
The value you entered is: 2
The value you entered is: 1
The value you entered is: 5

I appreciate all the help! I figure this is important for when I attach sensors or want to set manual analog outputs using my computer because if a sensor's resolution is 10bit, or I want to turn a motor 180 degrees, it won't do for me to send 1024 or 180 and it to be received as 1, 0, 2, 4 or 1,8,0.

Thanks a lot!

Moderator edit: [code] [/code] tags added.
« Last Edit: February 05, 2013, 09:15:30 pm by Coding Badly » Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 27
Posts: 1186
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The serial port receives a series of bytes that are interpreted by your code. The bytes have no inherent meaning - you give it the meaning.  The byte you are receiving in your example are being interpreted as chars, in other words ASCII codes. The byte gives you a number between 0 and 255. If you send two bytes one could be the low and the other the high byte of a 16 bit value or in fact they may be 2 ASCII characters.

If you enter data into the serial monitor then this will be sent as ASCII chars. However, between devices there is no restriction on what the bits transmitted mean. All you need to do is agree at both end on the protocol that will be used to send, receive and interpret the data.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
it won't do for me to send 1024 or 180 and it to be received as 1, 0, 2, 4 or 1,8,0.
Then, don't send "1024" or "180". Send those strings AND a packet terminator, like carriage return (which the Serial Monitor can add for you). Then, read and store, in an array, the data read (and append a NULL terminator), until the end of packet marker arrives. When that marker arrives, use atoi() to convert the array to a number, and use the number.

That's what everyone else does.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 71
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Phew... okay...I'm really sorry. What does this mean:
Quote
Send...a packet terminator, like carriage return (which the Serial Monitor can add for you).

And:
Quote
use atoi() to convert the array to a number, and use the number.
What class does that function apply to? Or else what does the method atoi() do? I couldn't find it on the reference page.

Anyway, I'll try to figure this out myself but I'd be mighty greatful if someone could give me the code or a link to a similar code.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What does this mean:
It means that when you are reading what I type, for instance, that you stop when you get to a period.

The Arduino doesn't stop reading data from the serial port, just like you don't stop reading when you get to a period. But, you do take special action, like think "Aha, that is the end of a sentence, and a thought."

You need to insert something in the data stream that the Arduino is reading that tells it that it has reached the end of a thought/sentence/packet/value, so that it knows that it needs to do something different - like convert the string to a value and use that value.

Quote
What class does that function apply to?
It doesn't. It applies to a NULL terminated array of chars.

Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 71
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Okay! I appreciate all the help! Absolutely not anywhere near my goal yet, but I have another question:
How do I check (from the arduino end) when a carriage return occurs.
if(serial.read()==13) ? (As per decimal conversion of ASCII)?

I really really really appreciate it! I am absolutely new.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
How do I check (from the arduino end) when a carriage return occurs.
if(serial.read()==13) ? (As per decimal conversion of ASCII)?
If you use Serial, instead of serial, that's one way. Of course, that is throwing away the character, so you won't be able to use it if it isn't a carriage return. Better would be something like:

Code:
   char c = Serial.read();
   if(c != '\n')
   {
      // Not a carriage return
   }
   else
   {
      // A carriage return finally got here...
   }

Keep in mind that carriage returns and line feeds often come together, and you probably don't want to store either one. Replace the second line of that snippet with if(c != '\n' && c != '\r') if that is the case.
Logged

Pages: [1]   Go Up
Jump to: