Need to display a float < 1 to LCD, Help Needed, using SPIN
DRMorrison
Posts: 81
I'm trying to display a decimal number on a serial LCD, but can't quite get the sequence. I have a stepper that moves 2.5 microns per step,
and would like this displayed on the LCD during the stepping. I have something that works, but it seems like a kludge to me.
The value always starts at zero, and then increases by the 2.5 microns (0.0025) each step. I'm having a time figuring a way to display the
fractional decimal number on the LCD as it increases each step. I've multiplied the number of steps by 25, and then using a the case command,
pad the resultant value with zeros, like this...
prints "-1.xxx", where the xxx is the fractional part determined by the steps.
I've looked into F32 and StringtoChar, but I just can't figure how to use them. I've looked for samples, all I need is one, and then I could figure
how to get this done. I'll keep trying to figure this out on my own, but any and all help will be greatly appreciated.
Daniel
and would like this displayed on the LCD during the stepping. I have something that works, but it seems like a kludge to me.
The value always starts at zero, and then increases by the 2.5 microns (0.0025) each step. I'm having a time figuring a way to display the
fractional decimal number on the LCD as it increases each step. I've multiplied the number of steps by 25, and then using a the case command,
pad the resultant value with zeros, like this...
if Zcnt =< 799 Zcnt += 1 case Zcnt 0..7 : tx(148) str(string("-0.00")) 8..79 : tx(148) str(string("-0.0")) 80..799 : tx(148) str(string("-0.")) DispDisp is a sub that prints the the decimal value to the LCD just following the text printed above. If the count gets to 800, the number becomes > 1, and another routine
prints "-1.xxx", where the xxx is the fractional part determined by the steps.
I've looked into F32 and StringtoChar, but I just can't figure how to use them. I've looked for samples, all I need is one, and then I could figure
how to get this done. I'll keep trying to figure this out on my own, but any and all help will be greatly appreciated.
Daniel
Comments
Taking your steps and multiplying by 25 is correct. now you have your microns in a 32bit integer long. say Zcnt
What you need to do is to first separate the whole part from the fractional part
so take another variable 'Zcnt2' to help you
we still need the case, but just one time
Enjoy!
Mike
That sort of works, but 20 iterations, or steps in my case, should result in a value of .05. But this ends up giving me .5--off by a factor of 10.
I've been trying to do this with Float32 and FloatString, but can't get the string to print to the LCD. This is a small test app. I think that I'm not
getting something correct with the string pointer of the returned value from FloatString obj.
Here is a quick test in Tachyon:
and again:
and done!
mike, this works!
Is there a way to do this using the Float32 and the FloatString objects?
Just curious.
Daniel
One thing I notice is that you trim trailing zeros but on LCD displays sometimes it's better to have fixed length fields for numbers otherwise the reading could jump about. It's easy enough to do this in Spin with just a few lines without complicating it with Floating point though.
Each step is 0.0025mm, so after 200 steps the display will show -0.5000. 400 steps is 1mm. This is the reading of the
z-axis, which moves down, hence the neg. number. It always starts from zero at the beginning of a cut cycle. The saw will cut
no more than 1-2 mm in depth.
I Would like to have this read an encoder and display the position like a DRO, but for now this will work. As long as I keep count
of the steps, I can display the value (position) on the LCD.
Now I need to get this to work in the other direction.
Thanks for your help, everyone. Daniel
that is moved with a stepper motor. I've tweeked things around a little to fit into my current program.
Thanks a million!
Daniel
step-by-step of one example, I could then use that in my code. I've never used floats up to this point, so I'm not sure
of the proper sytax. And converting and then printing is beyond me at this point.
1. create necessary variables
2. do math on variables
3. convert variables to string
4. print string to LCD
Daniel
It will multiply X1A by the G constant up in CON 2.1. Or change it to 1.0 or 4.4 etc. don't forget the decimal or it doesn't work.
I don't know your skill level maybe your a pro, semipro, beginner, etc. Your going to need the FloatMath and Floatstring object's for it to work.
Sample output:
the serial 4-line LCD from Parallax, but that shouldn't matter.
Will using Float32 work in place of FloatMath?
Daniel
str(string(FS.FloatToString(numPI))) 'Display string of float value
The str(string( ) is used by the Parallax serial LCD to print to
the LCD, and works great when printing text like this:
str(string("Hello")) But the above does not work.
All of the values are coming back from FloatMath as 0!
Daniel
Remove 'string()', you don't need to use that since the FloatToString routine already returns a zero delimited string pointer. The statement should look like:
You only need to use the 'string()' routine when you need to create/define a zero delimited string.
LCD displays nothing. I've put lines of code to look at the numbers by printing them at the bottom of the LCD, and they
always come out as zeros.
Daniel Also, I'm including a simple LCD driver in the file, it's not shown here. It includes 4 methods: Dec(Value), DecF(value,divider,places), str(stringptr) and Tx(Tx_byte)
If you're keeping track of the number of steps already, then just use that as you did earlier in the thread.
If it's the method I'm thinking of, using DecF(Zcnt * -25,20000,5) shows -1.00000 when Zcnt is 800.
able to do so. It seems that everyone else can get it to work, so I figure I too should be able to make
it work. I'm kind of stubborn that way.
Daniel
I would still like to know how to use the FloatString object to
print to the LCD.
This is what I ended up using. Very elegant. Works in both directions, and have added an Up & Down
button to move the stepper, and the DRO reads correctly.
Thanks to everyone for your help, Daniel