Help optimizing and fixing code

mofofo900

New Member
Sorry if the thread title is nondescript but there wasn't enough room describe my project in the title.

The basic summation of my project is to create a game show style buzzer system that determines who buzzed in first, and relays that information back to a display unit that that game show host has. The entire system contains 2 units, one being the display unit which indicates to the host who buzzed in first, and the second being the team unit, which contains 5 buttons and lights.

The main function of the team unit is to determine what player has pressed their button first, then relay they player and team number to the display unit. This at first glance might seem like a simple project but the system as it exists still has many flaws.

The first issue i partially resolved is with player favoritism. Being a single core platform the picaxe can only address one line of code at a time, meaning that if player 1 holds down his button, he will always register as first. The way i remedied this is by associating a player specific penalty to any player that holds the button down, this works but it also delays all other players on that team and is rather calculation intensive. If anyone has a better idea for this part of the system i would be greatly appreciative.

The second problem i am having is with an LED turning on that should not turn on. I ran through my physical connections and all of my code with a fine tooth comb, and i still, cannot for the life of me find the error. An example of this problem would be if i run through the code, and i press player 2's button, player 2's led should, and does light up, but the problem is player 1's led also lights up. If anyone has any solutions for this again i would be greatly appreciative.

I would gladly take any suggestions about any aspect of this project, and thanks in advance.

{CODE}

symbol unit = b0
symbol timeout = w0
symbol p1pen = w1
symbol p2pen = w2
symbol p3pen = w3
symbol p4pen = w4
symbol p5pen = w5
symbol ledtime = w9

let ledtime = 500
do
begin:
let timeout = 100
let p2pen = 0
let p1pen = 0
low 1
low 2
waiting:
if pin1 = 1 then
let p1pen = p1pen + 1
endif

if pin2 = 1 then
let p2pen = p2pen + 1
endif

if timeout = 0 then
'SOUND 3,(127,86)
high 6
pause 200
toggle 6
goto start
endif

let timeout = timeout - 1
goto waiting

start:
if p1pen > 0 then
let p1pen = p1pen - 1
endif

if p2pen > 0 then
let p2pen = p2pen - 1
endif

if p1pen = 0 or p2pen = 0 or pin3 = 1 or pin4 = 1 or pin5 = 1 then
goto win
endif

'if pin2 = 1 then
'goto win
' endif

goto start
win:
let unit = 10

if pin1 = 1 then

if p1pen = 0 then
let b0 = b0 + 1
serout 1,n2400,(b0)
high 1
pause ledtime
high 4
low 5
goto begin
endif
endif

if pin2 = 1 and p2pen = 0 then
'if p2pen = 0 then
let b0 = b0 + 2
serout 1,n2400,(b0)
high 2
low 1
pause ledtime
high 5
low 4 'for some reason the ide thinks this is 1 and 2 must fix later, for now low 1 fixes and all values correct
goto begin
endif
' endif

goto start
loop
{CODE}
 

BCJKiwi

Senior Member
Not studied the code in detail but b0 is half of w0.
The code appears to use both b0 and w0 independently so they will interfere with each other.

Try changing b0 to b20 (b20 is half of w10).
 

hippy

Ex-Staff (retired)
Likewise I've not studied the code, but a simple method to detect only the first button pushed is to read all buttons as bits (thisPush), 'compare' with the last button bits (lastPush) and look for new pushes (newPush) ...

Code:
Do
  thisPush = pins & %1111
  newPush = thisPush ^ lastPush & thisPush
  LookDown newPush,(%0000,%0001,%0010,%0100,%1000),whoPushedFirst
  If whoIsFirst <> 0 Then
    SerTxd( "Player ",#whoPushedFirst," pushed first",CR,LF)
  End If
  lastPush = thisPush 
Loop
That needs some modifications to handle when two people really do push their buttons at the same time, but is the general principle.
 
Top