Pages: [1]   Go Down
Author Topic: Reading multiple chars, converting string to int (Serials)  (Read 359 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey!
I'm trying to controll my stepper motor without using a library.
I want to control it via serial. I need three variables - direction (left, right), number of steps and speed.
OK.
Here's my problem - I would like to tell all this to arduino via serial. For example write in Serial Monitor: right, 100, 100   and watch my stepper moving.
It can also be: right [enter] 100 [enter] 100 [enter].
I have few problems here - how can I read multiple chars from serial? When i noramlly type '100', arduino treats it as 1, 0, 0. Second thing - even if I sucseed to read the data into an array, I still do not know how to use it later. I have no idea how to convert an array or string to integer, which i need :/.

Thank You for any help and please, forgive me all language mistakes, as I'm not an english speaker.
Logged

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

Quote
I'm trying to controll my stepper motor without using a library.
Why?

Quote
I want to control it via serial. I need three variables - direction (left, right), number of steps and speed.
Same inputs you'd need if you used the Stepper library.

Quote
Here's my problem - I would like to tell all this to arduino via serial. For example write in Serial Monitor: right, 100, 100   and watch my stepper moving.
Right is not a direction a stepper motor could understand. In case you haven't noticed, stepper motors rotate. Clockwise and counterclockwise are the only directions they understand.

Quote
I have few problems here - how can I read multiple chars from serial? When i noramlly type '100', arduino treats it as 1, 0, 0.
Yes. It is up to you to store them in an array until the end of packet marker (comma, enter, whatever) arrives, then deal with the string you have stored. If the string represents a single number, atoi() will convert it to an int for you.

Quote
Second thing - even if I sucseed to read the data into an array, I still do not know how to use it later. I have no idea how to convert an array or string to integer, which i need :/.
Well, now you do.
Logged

Dallas, Texas
Offline Offline
God Member
*****
Karma: 3
Posts: 718
Old, decrepit curmugeon
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Right is not a direction a stepper motor could understand. In case you haven't noticed, stepper motors rotate. Clockwise and counterclockwise are the only directions they understand.

While I agree with you, there are a number of folks who use left and right, even for rotation.  One phrase that comes to mind; "Lefty loosy, righty tighty..."  It never made much sense to me, but it does to some.
« Last Edit: August 02, 2012, 02:24:49 pm by wanderson » Logged

New true random number library available at: http://code.google.com/p/avr-hardware-random-number-generation/

Current version 0.7.2

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Surely that's 'righty tighty'
Logged

Dallas, Texas
Offline Offline
God Member
*****
Karma: 3
Posts: 718
Old, decrepit curmugeon
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Surely that's 'righty tighty'
Yep..  R.. T.. there right next to each other on the keyboard! smiley
Logged

New true random number library available at: http://code.google.com/p/avr-hardware-random-number-generation/

Current version 0.7.2

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Why?
I'm learning the code. For the sake of practice.

Quote
Right is not a direction a stepper motor could understand. In case you haven't noticed, stepper motors rotate. Clockwise and counterclockwise are the only directions they understand.
Oh, thanks for info. As I said - I'm not perfect english-speaker, but I'm sure You understood what i meant by "left".

Quote
Yes. It is up to you to store them in an array until the end of packet marker (comma, enter, whatever) arrives, then deal with the string you have stored. If the string represents a single number, atoi() will convert it to an int for you.
Do i store them in array by Serial.readBytesUntil()?

Quote
Well, now you do.
Well, thank You for that.
Logged

New Hampshire
Offline Offline
God Member
*****
Karma: 13
Posts: 779
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Do i store them in array by Serial.readBytesUntil()?

Questions on how to handle Serial comms come up on a daily basis, often multiple times a day.  Should be pretty easy to search out and read those threads.  Most of them are on the very topic of reading  in data and parsing it into ints, floats, strings, etc.
Logged


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

Quote
Questions on how to handle Serial comms come up on a daily basis,
So often, in fact, that someone (not mentioning any names) put together a blog on the topic:
http://jhaskellsblog.blogspot.com/2011/05/serial-comm-fundamentals-on-arduino.html
Logged

Pages: [1]   Go Up
Jump to: