Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

displaying a decimal on LCD

Status
Not open for further replies.

MrDEB

Well-Known Member
want to display the elapsed time between when green led turns on and button is pressed.
My target is to display say 4.025 seconds or 10.675 seconds. note 6 digits
experimenting with different math. Does not need to be real accurate but would be nice.

Code:
While true
         blue = 0
      DelayMS(2000)
         blue = 1
         green = 0  //go
Repeat
  DelayUS(time)
  Inc (time)
  Until button = 0
        green  = 1
        time = time * 2
  WriteAt(1,1,"YOUR REACTION",4)
   WriteAt(2,1,"Time = ",Dectostr(time)," ms",6)
     DelayMS(2000)
  time = 0 //reset to zero

Wend
 
Last edited by a moderator:
Think about your loop. If you have defined "time" – let's say you set it to 1 before the loop – the first time through the loop, you delay 1uS. The second time, you delay 2uS, then 3, 4, 5, etc. This is not what you want. If you want 1uS resolution, the delay statement should be

Code:
DelayUS(1)

The variable Time keeps track of the number of times through the loop.

Time represents, in this case, the number of uS of reaction time. What units do you want for the display? Probably mS, which is uS/1000.

To get the integer number of mS:

Code:
TimeIntegerPart = int(time/1000)

If you want to display the fractional part (i.e., the remaining uS), you need to subtract the integer number of mS from the Time variable to be left with the remainder of uS.

Code:
TimeFractionalPart = Time - (TimeIntegerPart * 1000)

//TimeIntegerPart * 1000 = the whole number of mS in Time

If you manage to get this far, all that's left is to display it.

Code:
LCD.Write (DecToStr(TimeIntegerPart), ".", DecToString(TimeFracrionalPart))

I don't remember the exact syntax for writing to an LCD, but the above gets you the numeric part.
 
One thing I will say is, try and get your indenting right, it'll make it much easier to spot mistakes.
I assume the above should be,
Code:
While true
    blue = 0
    DelayMS(2000)
    blue = 1
    green = 0  //go
    Repeat
        DelayUS(time)
        Inc (time)
    Until button = 0
    green  = 1
    time = time * 2
    WriteAt(1,1,"YOUR REACTION",4)
    WriteAt(2,1,"Time = ",Dectostr(time)," ms",6)
    DelayMS(2000)
    time = 0 //reset to zero
Wend
Notice how much easier it is to see the repeat-until loop.

Mike.
 
Shouldn't the Delay_US() value be a constant, so the loop counting "time" is incrementing equally?
eg. 1000 to increment the count in milliseconds? (or 999 or lower to if the loop & increment time is long enough to mess up the timing).

As it is, each delay is longer (as time increments) so the count gets slower and slower.

For a tidy display, use a float variable for the millisecond count, divide by 1000.0 to get decimal seconds and convert to a string.

If the string conversion does not have leading zero suppression, loop through the string starting from the left, compare the character to zero and if it matches, replace by a space.
As soon as you get a non-zero character, exit the loop and display the string.
 
Well it now works, thanks to all contributors for your help.
In the mean time I found during research an adrunio code for a STOPWATCH and like it but in my application it would be to distracting and not nessary.
I want to find out how to clan up the LCD display. It has 5 bars per writeat command at end of each line? Going to make some small changes to discover whee the lines are coming from
[swordfish basic]
While True
mS = 0
green = 0 //turn on green until button is pressed

Timer.Start //start timing reaction time

Repeat
Until
button = 0 // button pressed then turn off green, turn on blue
green = 1 //off
blue = 0 //on
Timer.Stop //stop timer
Timer.Stop //stop timer
s=dectostr(ms/1000)+"."+dectostr((ms mod 1000),2)

WriteAt(1,1,"reaction ",4)
writeat(2,1,"seconds ",s,4)

DelayMS(3000)
blue = 1 //led is off
DelayMS(1000)
ms = 0 //reset ms to zero

Wend
[end of code]
 
after doing some code clean up I see I have 1 too many Timer.Stop commands and found out how to eliminate the small bars after the writeat text. just remove the extra fluff.
WriteAt(1,1,"reaction ",4)
writeat(2,1,"seconds ",s,4)
corrected code

WriteAt(1,1,"reaction ")
writeat(2,1,"seconds ",s)
 
Here's how you would convert ms to a secs string, broken up into its steps
Code:
include "convert.bas"
dim ms as word
dim s as string

// convert word ms to secs string (ie "D.DDD")
s = DecToStr(ms/1000)               ' print whole portion (number before decpt)
s = s + "."                         ' add decpt
s = s + DecToStr((ms MOD 1000), 3)  ' add three digits fractional part
s = s + "secs"

// or all in one shot
s = DecToStr(ms/1000) + "." + DecToStr((ms MOD 1000), 3) + "secs"

To write it to the LCD you could just use
Code:
// write string to LCD
s = DecToStr(ms/1000) + "." + DecToStr((ms MOD 1000), 3) + "secs"
WriteAt(1,1,"reaction=",s,2)
or put it all into the WriteAt statement

Tumbleweed's print method is very slick and elegant. Good job!
 
I used Tumbleweeds suggestion with some slight editing to clean up the LCD display.
works great
thanks
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top