Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 07:24 25 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Help - Number to Character String Convert

Author Message
Ray B
Senior Member

Joined: 16/02/2007
Location: Australia
Posts: 219
Posted: 02:35am 02 Feb 2012
Copy link to clipboard 
Print this post

Got a mind block converting a number to a string as follows for displaying a temperature value from a DS18B20 to an LCD display using recently published subroutines, as follows:

timestring$ = "Time is: " + Time$
PrintLCD 1, timestring$
Pause 1000

above works fine but when I try adding a Temperature value it comes to grief. i.e.

GetTemp 4, Temp
Temp$ = Chr$(Temp)
temperature$ = "Temp = " + Temp$ + "degrees at: " + Time$ ' ignore character exceeds 20 characters for the LCD this is just an example
PrintLCD 2, temperature$
Pause 2000

OK Chr$ will returns a one-character string consisting of the character corresponding to the ASCII code indicated by argument 'number'.

BUT the temperature reading will be more than one character e.g. 25.0625 degrees
so how do we convert this to a simple character string suitable for display ?

Help welcome....Cheers
RayB from Perth WA
 
CircuitGizmos

Guru

Joined: 08/09/2011
Location: United States
Posts: 1425
Posted: 02:49am 02 Feb 2012
Copy link to clipboard 
Print this post

How about converting the number to several single digit variables?

temphi = int(temp/10)
templo = temp - (temphi * 10)

then chr$ the temphi and templo values.
Micromites and Maximites! - Beginning Maximite
 
djuqa

Guru

Joined: 23/11/2011
Location: Australia
Posts: 447
Posted: 02:50am 02 Feb 2012
Copy link to clipboard 
Print this post

Temperature$=STR$(temp)

[Quote=maximite manual]Str$(number) function returns a String in the (Base 10) Representation of 'number' [/quote]Edited by djuqa 2012-02-03
VK4MU MicroController Units

 
CircuitGizmos

Guru

Joined: 08/09/2011
Location: United States
Posts: 1425
Posted: 03:29am 02 Feb 2012
Copy link to clipboard 
Print this post

  djuqa said   Temperature$=STR$(temp)

[Quote=maximite manual]Str$(number) function returns a String in the (Base 10) Representation of 'number' [/quote]


That's it. I shouldn't watch TV and try to think at the same time...
Micromites and Maximites! - Beginning Maximite
 
Ray B
Senior Member

Joined: 16/02/2007
Location: Australia
Posts: 219
Posted: 03:38am 02 Feb 2012
Copy link to clipboard 
Print this post

djuga not up with my basic programming - overlooked Str$ - this solution only works with integers (i.e. ignores fractions of degrees) but thanks for the quick response.

Gizmos gets the prize for looking at Hi & Lo parts .

Following code works great - I've left a few delays and print statements in for dignostic purposes.

GetTemp 4, Temp
temphi = Int(Temp)
templo = temp - (temphi)
temphi$ = Str$(temphi)
Print "temphi$ = " + temphi$ : Pause 1000
templo$ = Str$(templo)
templo$ = Mid$(templo$,3) ' this is to ignore leading 0.
Print "templo$ = " + templo$ : Pause 1000
'Temp$ = Str$(Temp)
temperature$ = "Temp = " + temphi$ + "." + templo$ + "C"
PrintLCD 2, temperature$
Pause 2000

James is about to release some 4 x 20 LCD code using subroutines that I've been testing with him & I'm using this to obviously obtain temperature from DS18B20 & displaying on LCD. After James releases his code (maybe later today) I'll tidy this application up for others.

Next stage will be to incorporate it into some if the previously posted Data Logger code.

Cheers all
RayB from Perth WA
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3194
Posted: 05:47am 02 Feb 2012
Copy link to clipboard 
Print this post

Str$() should print fractions. If does not it is a severe bug. (EDIT: or have I misunderstood?)

You can use the Format$() function which has greater control of the formatting. But (on the downside) it does take some time to understand its syntax.

GeoffEdited by Geoffg 2012-02-03
Geoff Graham - http://geoffg.net
 
BobDevries

Senior Member

Joined: 08/06/2011
Location: Australia
Posts: 266
Posted: 07:00am 02 Feb 2012
Copy link to clipboard 
Print this post

Hi all,

Indeed STR$ *does* convert floating point numbers. Example:


a=1.25:PRINT STR$(a)
1.25


Works fine.

Regards,

Bob Devries
Dalby, QLD, Australia
 
Ray B
Senior Member

Joined: 16/02/2007
Location: Australia
Posts: 219
Posted: 07:22am 02 Feb 2012
Copy link to clipboard 
Print this post

Interestingly I've looked at the code, made a few changes and STR$ does show fractions.

Anyway I learn a lot from Gizmos' code offering.

For those interested my final code extract is as follows:

for loop = 1 to 10
GetTemp 4, Temp
Print "original num = " + Str$(Temp) ' prints on PC for diagnostic purposes
temperature$ = "Temp = " + Str$(Temp) + " C"
PrintLCD 2, temperature$
PrintLCD 3, "Loop Count = :" + Str$(loop)
Pause 1000
Next loop

All demonstrates how great it is today to have subroutines. Thanks guys
RayB from Perth WA
 
djuqa

Guru

Joined: 23/11/2011
Location: Australia
Posts: 447
Posted: 07:27am 02 Feb 2012
Copy link to clipboard 
Print this post

Code looks good
Subroutines RULE!



VK4MU MicroController Units

 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024