Another example when AI code works on PBP - Multiple ADC button reading and debouncing:
Code:
ReadButton:
TempE = 0
' Repeat check 5 times for debounce
For cnter = 1 to 5
ADCIN 0, adcval ' Read from AN0 (adjust channel if needed)
If (adcval >= 230) And (adcval <= 270) Then
TempE = 1
ElseIf (adcval >= 290) And (adcval <= 330) Then
TempE = 2
ElseIf (adcval >= 380) And (adcval <= 440) Then
TempE = 3
ElseIf (adcval >= 580) And (adcval <= 700) Then
TempE = 4
Else
TempE = 0
EndIf
' If reading doesn’t match previous stable result, reset debounce
If cnter = 1 Then
E = TempE
Else
If TempE <> E Then
E = 0
Return
EndIf
EndIf
Pause 10 ' small delay between samples
Next cnter
Return
However, it still needed tweaking, because it used counter as variable name, which is reserved word, so I've changed it to cnter and this code works and far more efficient than my own - shown below:
Code:
keyhandler:
babo:
adcin 0,adcval
if adcval<100 then goto resetvar 'reset internal debounce variables
if adcval>230 and adcval<270 then a=a+1
if adcval>290 and adcval<340 then b=b+1
if adcval>385 and adcval<440 then c=c+1
if adcval>570 and adcval<660 then d=d+1
cnter=cnter+1 'increase loop iteration counter
if cnter=100 then goto analyzer 'if enough time pressed, go to analyzer
goto babo
Analyzer:
' --- Comparison Logic ---
IF (A = B) OR (A = C) OR (A = D) OR (B = C) OR (B = D) OR (C = D) THEN
E = 5 ' Tie detected
ELSE
IF (A > B) AND (A > C) AND (A > D) THEN
E = 1 ' A is greatest
ELSEIF (B > A) AND (B > C) AND (B > D) THEN
E = 2 ' B is greatest
ELSEIF (C > A) AND (C > B) AND (C > D) THEN
E = 3 ' C is greatest
ELSEIF (D > A) AND (D > B) AND (D > C) THEN
E = 4 ' D is greatest
ELSE
E = 5 ' Safety fallback (should not reach here)
ENDIF
ENDIF
resetvar:
cnter=0
'a=0
b=0
c=0
d=0
return
Bookmarks