Go Down

Topic: Reading multiple chars, converting string to int (Serials) (Read 428 times) previous topic - next topic

eLAN

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.

PaulS

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.

wanderson

#2
Aug 02, 2012, 08:22 pm Last Edit: Aug 02, 2012, 09:24 pm by wanderson Reason: 1

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.
New true random number library available at: http://code.google.com/p/avr-hardware-random-number-generation/

Current version 1.0.1

dxw00d


wanderson


Surely that's 'righty tighty'

Yep..  R.. T.. there right next to each other on the keyboard! :)
New true random number library available at: http://code.google.com/p/avr-hardware-random-number-generation/

Current version 1.0.1

eLAN

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.

jraskell

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.

PaulS

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

Go Up