School Project - suggestions welcomed
Some lads at school have made a game with a racing car. Basically it consists of a rotating arm with a magnet on each end. This rotates below an acrylic racetrack. A small car is carried round by the magnet. The idea is to see how fast you can do 10 laps. If you go too fast the car flies off. I said that i would make a timing system for this using a Propellor Platform USB and the UI module. Jonny Mac's timer and LCD driver looked useful so I kludged together the following code:
This only sort of works. The race will be monitor with a reed switch. 20 closures will equate to 10 laps at which point the timer must stop. Should I use a separate cog to count the pulses (and if so, how?) or should I do the detection in the main loop. If I use a separate cog, how do I use it to signal to the main object that the race has ended?
All ideas welcome.
Cheers
Richard N
obj
ui : "jm_lcd_ui"
Timer :"jm_etimer"
var
Long mytimer[5]
pub main | b
ui.init ' initialize LCD UI
Timer.init(16) ' initialise timer
ui.cls 'A few graphics to start
ui.scrollstr(1, 1, 8, 250, @Congleton)
ui.scrollstr(1, 2, 8, 250, @High)
Waitcnt(clkfreq + cnt)
ui.cls 'display starting instructions
ui.moveto(1, 1)
ui.str(string(" Press"))
ui.Moveto(1,2)
ui.str(string(" Button"))
repeat
b := ui.getbtns ' check buttons
ui.moveto(1, 1)
if (b & ui#BTN_ESC)
ui.cls 'countdown from button press to race start
ui.str(string("3"))
Waitcnt(clkfreq + cnt)
ui.cls
ui.str(string("2"))
Waitcnt(clkfreq + cnt)
ui.cls
ui.str(string("1"))
Waitcnt(clkfreq + cnt)
ui.cls 'the race starts
ui.str(string("GO!"))
Timer.Clear 'timer set to zero
Waitcnt(clkfreq + cnt)
Repeat 'this displays the time while the race is going
'need some way to count 20 reedswitch closures
'from race track and use that to halt timing
ui.cls
ui.moveto(1, 1)
Timer.read(@mytimer)
ui.dec(mytimer[1])
ui.moveto (5,1)
ui.str(string("Secs"))
ui.moveto(1, 2)
Timer.read(@mytimer)
ui.dec(mytimer[0])
ui.moveto (5,2)
ui.str(string("ms"))
Waitcnt(clkfreq/4 + cnt) 'update display every 250ms
Timer.Hold 'race end detected somehow!
ui.cls 'display the final time
ui.moveto(1, 1)
Timer.read(@mytimer)
ui.dec(mytimer[1])
ui.moveto (5,1)
ui.str(string("Secs")) 'this does not work for some reason
ui.moveto(1, 2)
Timer.read(@mytimer)
ui.dec(mytimer[0])
ui.moveto (5,2)
ui.str(string("ms"))
else
ui.str(string(" ")) 'do I need this?
waitcnt(clkfreq / 25 + cnt)
dat
Congleton Byte " Conglton", 0
High Byte " High", 0
This only sort of works. The race will be monitor with a reed switch. 20 closures will equate to 10 laps at which point the timer must stop. Should I use a separate cog to count the pulses (and if so, how?) or should I do the detection in the main loop. If I use a separate cog, how do I use it to signal to the main object that the race has ended?
All ideas welcome.
Cheers
Richard N

Comments
Yes you could create a separate cog for this and check it on each pass of your repeat loop..
Might be even easier to create a "lapcount" variable and do a lapcount++ at each pass of your repeat loop, then an IF statement to see if lapcount > 20, meaning end of race.
OBC
Or if they're not clean you can clean them up with a little circuitry similar to a button debounce circuit.
A 555 in single-shot mode is easy to implement. Or, one could write a debouncing cog to clean-up input pulses and count them.
I like this solution. It means no extra circuitry for the op.
a) SPDT reeds wired to a CMOS or PinKeeper input, has no bounce effect as the contacts either connect to Vcc or GND - any open time, holds the previous state.
b) Use 2 SPNO reeds, spaced so they cannot both be on, one to Vcc, one to GND, operates as in a).
in both cases the leading edge will be the precise contact closure