Hey, I'm trying to get the Arduino to take in a 6-digit numerical string from a processing sketch. Since the Arduino reads serial one character at a time, what would be the best way to assemble the 6 characters into a single integer variable?
Sorry about deleting the post, I thought I had it for a second. The array is not being manually defined, it's being defined one digit at a time from the loop shown in the second post of this thread.
Here's an example of what I'm having a problem with:
This outputs "12345" which is fine, that will work perfectly for what I need. The problem is that the highest end of the range of the number is over 100000, so say the number I'm trying to read is "123456", if I try:
In this example, rather than getting "123456", I'm getting "-7616". I can't for the life of me understand where this value is coming from unless "atoi()" has some kind of limit.
That makes sense that that would have been over the limit for int, so I tried using "long" instead of "int". Even though I'm well within the range for a "long" variable, I'm still getting the "-7616", any ideas?
EDIT: I was starting to fry my brain trying to figure this out, then it hit me, if "atoi()" stands for "ASCII to Int", what would happen if I changed the "i" to an "l" for long, low and behold it worked.
Be very careful - your array is only working here because the compiler is filling unused elements with zero.
In a running system, this advantage may disappear, as string lengths change.
Well, like I said, I wasn't defining the array manually, the array is being created by reading serial one character at a time. I only posted the "{'1', '2', '3'}" example to show what the array contains, not how it's created. The "atol()" function seems to be working well for what I need.
I wasn't defining the array manually, the array is being created by reading serial one character at a time.
So that you don't waste people's time, it is best for everyone if you post the code that you're actually using.
Passing unterminated strings to library routines is a common noob bug.