Math Issue
Earl Foster
Posts: 185
I have a procedure that is not producing the results I was expecting.· The procedure steps through a string of characters, convert the ones I want to decimal, and stores that value to a variable called "number".
An example:
I read a string in is as "a456d" and I only care about the number portion. If that number is less then 500 I want to do something
The results I am expecting is that something will be done since the "if" statement is true
if 456 < 500
·· Do something
Can someone explain to me why this shouldn't work?
Thanks
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
WWW.HAPB.NET
"Don't ask yourself what the world needs - ask yourself what makes you come alive, and then go do it." - H.T.Whitman
An example:
I read a string in is as "a456d" and I only care about the number portion. If that number is less then 500 I want to do something
number := 0 repeat i from 1 to 3 number := number * 10 + string[noparse][[/noparse]i] - "0" if number < 500 Do something
The results I am expecting is that something will be done since the "if" statement is true
if 456 < 500
·· Do something
Can someone explain to me why this shouldn't work?
Thanks
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
WWW.HAPB.NET
"Don't ask yourself what the world needs - ask yourself what makes you come alive, and then go do it." - H.T.Whitman
Comments
Some questions:
- How is "string" declared? For this to work, it needs to be a byte array.
- Are you certain that "string" contains "a456d"? (How did you verify this?)
- Can you post the complete program?
Also, if you only care about checking that the number is less than 500, why not just test that the digit in string is < "5"?
--Micah
Try doing "repeat i from "1" to "3"", "number:= number*10 + i-"0""
The actual string is a byte array of 82 characters with the numbers always changing.· Using just a "5" will not work because the number could be 510 or 5000 so looking for 500 is important.
I am thinking it might be the way the propeller handles run time variables.· The manuals seems to reference run time variables·as being handled differently then contants.· If I declare a contant or assign a value to the variable (num := 350)··the procedure works, however, as soon I as put the run time variable back in place it no longer works.
The actually number is a float (ex. 234.5) but I am not concerned with the fractional portion of the number so I use the period as my delimiter.· Assume that holder[noparse][[/noparse]82] is populated correctly.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
WWW.HAPB.NET
"Don't ask yourself what the world needs - ask yourself what makes you come alive, and then go do it." - H.T.Whitman
Post Edited (Earl Foster) : 8/23/2008 2:21:22 AM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Michael Park
PS, BTW, and FYI:
To search the forum, use search.parallax.com (do not use the Search button).
Check out the Propeller Wiki: propeller.wikispaces.com/
number := number * 10 + byte[noparse][[/noparse] myString ][noparse][[/noparse] i ] - "0"
This will become an endless loop.
One thing that is not clear to me in some of these examples is,
If there are letters within this string, how are you separating them from the numbers?
Usually the propeller would store the string in ascii format. So if I have a string that is 123 and want to store that as a number I should be able to convert and store it.
Number := 0
Repeat 1 to 3 times
Number := number * 10 + (holder[noparse][[/noparse]index]-“0”[noparse];)[/noparse]
This would step through as
Number := 0 + (49 – 48) = 1
Number := 10 + (50 – 48) = 12
Number := 120 + (51 – 48) = 123
Now I have stored the actual integer value and should be able to use directly with any math expressions.
Number < 200 = True, therefore do something
Number := number + 50 = 173
Etc
Is my understanding correct?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
WWW.HAPB.NET
"Don't ask yourself what the world needs - ask yourself what makes you come alive, and then go do it." - H.T.Whitman
number := number * 10 + charArray[noparse][[/noparse]index] - "0"
Your index most probably needs to start at zero as mpark suggested earlier, so "repeat index from 0 to 2", and, as also suggested, make sure number is zeroed before starting.
Here is the actual code.· Its rather large that is why I didn't want to get bogged down in it.· Everything works except my altitude check which is suppose to flash external lights below a 1000 meters.· It is currently set to 200 for testing.· I will have to step through the code again this weekend.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
WWW.HAPB.NET
"Don't ask yourself what the world needs - ask yourself what makes you come alive, and then go do it." - H.T.Whitman
The code you're having problems with is this ... ?
For 'satellites' check the characters are at 40 to 41, not 39 to 40 or similar. Likewise for 'altitude' check that 'index' does point to the first character of the number you want to read. Is it after the ninth comma ? Check that your earlier code which moves the received data into the 'gga' array is doing as you want it to.
Since I store the GPS value starting with GPGGA the ninth comma is the correct location for altitude. I already display the code to an LCD so the character placement is correct.
I have already been using Debug.DEC(Altitude) for troubleshooting and it shows that I am capturing and manipulating the data correctly. Like I said earlier, I will step through the code this weekend. I wanted to make sure my understanding was correct before do that. It is probably something simple lost amongst the code. Perhaps I am toggling the altitude between 0 and the actual value when it is being read by the other cog.
Erik
I haven’t seen that version of gps code before. I will take a look at for my next version. I noticed that you had GGA set to 64. GGA can be up to 82 characters although I haven’t seen 82 characters yet with the GPS units I use. I like the fact that it is using the simple serial object which frees up a cog. My code uses 2 cogs for gps functions.
I will keep troubeshooting. Thanks
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
WWW.HAPB.NET
"Don't ask yourself what the world needs - ask yourself what makes you come alive, and then go do it." - H.T.Whitman
Works like a champ - Code complete.· Launch is scheduled for next weekend.
Thanks for the help.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
WWW.HAPB.NET
"Don't ask yourself what the world needs - ask yourself what makes you come alive, and then go do it." - H.T.Whitman