Bug - help request
g3cwi
Posts: 262
The following code is a lap counter that looks for a reedswitch being triggered. It mostly works but never get to the very end line which indicates the race is over. Any ideas?
Thanks.
PUB Count | FLAG_C, Lap dira [LED]~~ 'Set the LED port as output dira [SWITCH]~ 'Set SWITCH port as input Lap:=0 'Initialise Lap Counter Repeat until Lap == 6 FLAG_C:=0 'Initialise Flag_C Repeat Until Flag_C > 20 'Debounce transition If ina[SWITCH] == 0 FLAG_C += 1 'Increment Flag Counter Else FLAG_C:=0 'Reset while bouncing Lap += 1 FLAG_C:=0 'Initialise Flag_C Repeat Until Flag_C > 20 'Debounce transition If ina[SWITCH] == 1 FLAG_C += 1 'Increment Flag Counter Else FLAG_C:=0 'Reset while bouncing outa[LED]~~ 'Race end
Thanks.
Comments
I don't want to spoil you fun so I want to warn you.
*** Spoiler Alert ***
Here's my attempt.
Edit: You'd want to adjust the value of "_OffThreshold" where it's sort enough to still register counts at fast rotations but not so short it will count a bouncing switch.
So, to keep your LED switched on you need an endless repeat loop at the very end.
Sorry Duane, but I think your code is not much better in this sense ;o)
You're always welcome to critic my code (I consider it a favor) but in this case, I assumed this wasn't the whole program. I figured flow would return to the calling method.
If this is the only method, then there would need to be a repeat added after the outa statement.
I didn't compile the above method since it used constants (and I assumed methods) not declared in the posted code.
I personally like to add a serial debug to programs I'm having trouble with. You can have the debug running in a separate cog so it doesn't interfere with the timing. Even if the debug is in the same cog with debug statements inline with the code, you could rotate the table slowly to compensate for the debug statements to see if the branching of the code is working as you expect it to. The debug statements can be removed as you feel more confident with the code.
Thanks for the responses. The answer was that addition of a repeat worked perfectly. This cog can then be shut down once its state has been reported. I transfer the end of race flag to the main program simply by looking at the output pin of the Lap counter cog.
I am very happy with the program. It makes use of several cogs to do different tasks and really does show the flexibility of the Propellor. It's rather cool.
Thanks again.
Richard
I'm concerned about a single off bouce causing a lap to be missed.
So if the reed switch bouces it will cause "FLAG_C" to be reset. I'd think this could cause a lap to be missed if the switch bouces.
I don't see a check to make sure the switch has turned off prior to counting the next lap.
What if you turn the turntable really slow? Doesn't it count additional laps?
As it is now, it looks like the switch has to be on between 20 and 39 loops in order for the lap to be counted correctly.
I estimate the on-time to be around 100ms and the loop runs much faster so there are no problems. It really does seem to work just fine as it is!
Regards
Richard