please help me make my code more reliable. SOS !
Francis160791
Posts: 28
' {$STAMP BS2pe}
' {$PBASIC 2.5}
counter VAR Nib
TX PIN 15
Spkr PIN 15
#SELECT $stamp
#CASE BS2PE
T2400 CON 396
T9600 CON 84
T19K2 CON 32
#ENDSELECT
LcdBaud CON T19K2
LcdCls CON $0C ' clear LCD (use PAUSE 5 after)
LcdBLon CON $11 ' backlight on
SEROUT TX, Lcdbaud, ["Key in PIN",CR, LcdBLon]
main:
GOSUB top
IF (IN6=0 AND (IN7=0 OR IN0=0 OR IN1=0)) THEN
GOSUB top
PAUSE 250
ELSEIF (IN7=0 AND (IN8=0 OR IN1=0 OR IN0=0 OR IN2=0))THEN
GOSUB top
PAUSE 250
ELSEIF (IN8=0 AND (IN9=0 OR IN2=0 OR IN1=0 OR IN3=0))THEN
GOSUB top
PAUSE 250
ELSEIF (IN9=0 AND (IN10=0 OR IN3=0 OR IN2=0 OR IN4=0))THEN
GOSUB top
PAUSE 250
ELSEIF (IN10=0 AND (IN11=0 OR IN4=0 OR IN3=0 OR IN5=0))THEN
GOSUB top
PAUSE 250
ELSEIF (IN11=0 AND (IN12=0 OR IN5=0 OR IN4=0))THEN
GOSUB top
PAUSE 250
ELSE
GOSUB btm
ENDIF
GOTO main
'====================================END MAIN==============================================='
top:
IF IN0=0 THEN 'num 0
SEROUT TX, Lcdbaud,["0"]
PAUSE 180
ENDIF
DO
IF IN0=0 THEN
PAUSE 180
ELSE
EXIT
ENDIF
LOOP
IF IN1=0 THEN 'num 1
SEROUT TX, Lcdbaud,["1"]
PAUSE 180
DO
IF IN1=0 THEN
PAUSE 180
ELSE
EXIT
ENDIF
LOOP
ENDIF
IF IN2=0 THEN 'num 2
SEROUT TX, Lcdbaud,["2"]
PAUSE 180
DO
IF IN2=0 THEN
PAUSE 180
ELSE
EXIT
ENDIF
LOOP
ENDIF
IF IN3=0 THEN 'num 3
SEROUT TX, Lcdbaud,["3"]
PAUSE 180
DO
IF IN3=0 THEN
PAUSE 180
ELSE
EXIT
ENDIF
LOOP
ENDIF
IF IN4=0 THEN 'num 4
SEROUT TX, Lcdbaud,["4"]
PAUSE 180
DO
IF IN4=0 THEN
PAUSE 180
ELSE
EXIT
ENDIF
LOOP
ENDIF
IF IN5=0 THEN 'num 5
SEROUT TX, Lcdbaud,["5"]
PAUSE 180
DO
IF IN5=0 THEN
PAUSE 180
ELSE
EXIT
ENDIF
LOOP
ENDIF
RETURN
'=======================================END TOP=============================================='
btm:
IF IN6=0 THEN 'num 6
POLLIN 7, 1
POLLMODE 2
PAUSE 300
counter=0
DO
counter=counter+1
IF IN7=1 THEN
PAUSE 50
ELSE
EXIT
ENDIF
LOOP UNTIL (counter=6)
IF IN7=1 AND IN0=1 THEN
SEROUT TX, Lcdbaud,["6"]
DO
IF IN6=0 THEN
PAUSE 50
ELSE
EXIT
ENDIF
LOOP
ENDIF
ENDIF
IF IN7=0 THEN 'num 7
POLLIN 6, 1
POLLIN 8, 1
POLLMODE 2
PAUSE 300
counter=0
DO
counter=counter+1
IF IN6=1 AND IN8=1 THEN
PAUSE 50
ELSE
EXIT
ENDIF
LOOP UNTIL (counter=6)
IF IN6=1 AND IN8=1 AND IN1=1 THEN
SEROUT TX, Lcdbaud,["7"]
DO
IF IN7=0 THEN
PAUSE 50
ELSE
EXIT
ENDIF
LOOP
ENDIF
ENDIF
IF IN8=0 THEN 'num 8
POLLIN 7, 1
POLLIN 9, 1
POLLMODE 2
PAUSE 300
counter=0
DO
counter=counter+1
IF IN7=1 AND IN9=1 THEN
PAUSE 50
ELSE
EXIT
ENDIF
LOOP UNTIL (counter=6)
IF IN7=1 AND IN9=1 AND IN2=1 THEN
SEROUT TX, Lcdbaud,["8"]
DO
IF IN8=0 THEN
PAUSE 50
ELSE
EXIT
ENDIF
LOOP
ENDIF
ENDIF
IF IN9=0 THEN 'num 9
POLLIN 9, 1
POLLIN 10, 1
POLLMODE 2
PAUSE 300
counter=0
DO
counter=counter+1
IF IN8=1 AND IN10=1 THEN
PAUSE 50
ELSE
EXIT
ENDIF
LOOP UNTIL (counter=6)
IF IN8=1 AND IN10=1 AND IN3=1 THEN
SEROUT TX, Lcdbaud,["9"]
DO
IF IN9=0 THEN
PAUSE 50
ELSE
EXIT
ENDIF
LOOP
ENDIF
ENDIF
IF IN10=0 THEN 'Enter
POLLIN 9, 1
POLLIN 11, 1
POLLMODE 2
PAUSE 300
counter=0
DO
counter=counter+1
IF IN9=1 AND IN11=1 THEN
PAUSE 50
ELSE
EXIT
ENDIF
LOOP UNTIL (counter=6)
IF IN9=1 AND IN11=1 AND IN4=1 THEN
SEROUT TX, Lcdbaud,[ CR, "Thanks for banking"]
DO
IF IN10=0 THEN
PAUSE 50
ELSE
EXIT
ENDIF
LOOP
ENDIF
ENDIF
IF IN11=0 THEN 'Clear
POLLIN 10, 1
POLLIN 12, 1
POLLMODE 2
PAUSE 300
counter=0
DO
counter=counter+1
IF IN10=1 AND IN12=1 THEN
PAUSE 50
ELSE
EXIT
ENDIF
LOOP UNTIL (counter=6)
IF IN10=1 AND IN12=1 AND IN5=1 THEN
SEROUT TX, Lcdbaud,[LcdCls,"Key in PIN",CR]
DO
IF IN11=0 THEN
PAUSE 50
ELSE
EXIT
ENDIF
LOOP
ENDIF
ENDIF
IF IN12=0 THEN 'Cancel
POLLIN 11, 1
POLLIN 5, 1
POLLMODE 2
PAUSE 300
counter=0
DO
counter=counter+1
IF IN12 THEN
PAUSE 50
ELSE
EXIT
ENDIF
LOOP UNTIL (counter=6)
IF IN11=1 AND IN5=1 THEN
SEROUT TX, LcdBaud, [8, " ", 8]
DO
IF IN12=0 THEN
PAUSE 50
ELSE
EXIT
ENDIF
LOOP
ENDIF
ENDIF
RETURN
' {$PBASIC 2.5}
counter VAR Nib
TX PIN 15
Spkr PIN 15
#SELECT $stamp
#CASE BS2PE
T2400 CON 396
T9600 CON 84
T19K2 CON 32
#ENDSELECT
LcdBaud CON T19K2
LcdCls CON $0C ' clear LCD (use PAUSE 5 after)
LcdBLon CON $11 ' backlight on
SEROUT TX, Lcdbaud, ["Key in PIN",CR, LcdBLon]
main:
GOSUB top
IF (IN6=0 AND (IN7=0 OR IN0=0 OR IN1=0)) THEN
GOSUB top
PAUSE 250
ELSEIF (IN7=0 AND (IN8=0 OR IN1=0 OR IN0=0 OR IN2=0))THEN
GOSUB top
PAUSE 250
ELSEIF (IN8=0 AND (IN9=0 OR IN2=0 OR IN1=0 OR IN3=0))THEN
GOSUB top
PAUSE 250
ELSEIF (IN9=0 AND (IN10=0 OR IN3=0 OR IN2=0 OR IN4=0))THEN
GOSUB top
PAUSE 250
ELSEIF (IN10=0 AND (IN11=0 OR IN4=0 OR IN3=0 OR IN5=0))THEN
GOSUB top
PAUSE 250
ELSEIF (IN11=0 AND (IN12=0 OR IN5=0 OR IN4=0))THEN
GOSUB top
PAUSE 250
ELSE
GOSUB btm
ENDIF
GOTO main
'====================================END MAIN==============================================='
top:
IF IN0=0 THEN 'num 0
SEROUT TX, Lcdbaud,["0"]
PAUSE 180
ENDIF
DO
IF IN0=0 THEN
PAUSE 180
ELSE
EXIT
ENDIF
LOOP
IF IN1=0 THEN 'num 1
SEROUT TX, Lcdbaud,["1"]
PAUSE 180
DO
IF IN1=0 THEN
PAUSE 180
ELSE
EXIT
ENDIF
LOOP
ENDIF
IF IN2=0 THEN 'num 2
SEROUT TX, Lcdbaud,["2"]
PAUSE 180
DO
IF IN2=0 THEN
PAUSE 180
ELSE
EXIT
ENDIF
LOOP
ENDIF
IF IN3=0 THEN 'num 3
SEROUT TX, Lcdbaud,["3"]
PAUSE 180
DO
IF IN3=0 THEN
PAUSE 180
ELSE
EXIT
ENDIF
LOOP
ENDIF
IF IN4=0 THEN 'num 4
SEROUT TX, Lcdbaud,["4"]
PAUSE 180
DO
IF IN4=0 THEN
PAUSE 180
ELSE
EXIT
ENDIF
LOOP
ENDIF
IF IN5=0 THEN 'num 5
SEROUT TX, Lcdbaud,["5"]
PAUSE 180
DO
IF IN5=0 THEN
PAUSE 180
ELSE
EXIT
ENDIF
LOOP
ENDIF
RETURN
'=======================================END TOP=============================================='
btm:
IF IN6=0 THEN 'num 6
POLLIN 7, 1
POLLMODE 2
PAUSE 300
counter=0
DO
counter=counter+1
IF IN7=1 THEN
PAUSE 50
ELSE
EXIT
ENDIF
LOOP UNTIL (counter=6)
IF IN7=1 AND IN0=1 THEN
SEROUT TX, Lcdbaud,["6"]
DO
IF IN6=0 THEN
PAUSE 50
ELSE
EXIT
ENDIF
LOOP
ENDIF
ENDIF
IF IN7=0 THEN 'num 7
POLLIN 6, 1
POLLIN 8, 1
POLLMODE 2
PAUSE 300
counter=0
DO
counter=counter+1
IF IN6=1 AND IN8=1 THEN
PAUSE 50
ELSE
EXIT
ENDIF
LOOP UNTIL (counter=6)
IF IN6=1 AND IN8=1 AND IN1=1 THEN
SEROUT TX, Lcdbaud,["7"]
DO
IF IN7=0 THEN
PAUSE 50
ELSE
EXIT
ENDIF
LOOP
ENDIF
ENDIF
IF IN8=0 THEN 'num 8
POLLIN 7, 1
POLLIN 9, 1
POLLMODE 2
PAUSE 300
counter=0
DO
counter=counter+1
IF IN7=1 AND IN9=1 THEN
PAUSE 50
ELSE
EXIT
ENDIF
LOOP UNTIL (counter=6)
IF IN7=1 AND IN9=1 AND IN2=1 THEN
SEROUT TX, Lcdbaud,["8"]
DO
IF IN8=0 THEN
PAUSE 50
ELSE
EXIT
ENDIF
LOOP
ENDIF
ENDIF
IF IN9=0 THEN 'num 9
POLLIN 9, 1
POLLIN 10, 1
POLLMODE 2
PAUSE 300
counter=0
DO
counter=counter+1
IF IN8=1 AND IN10=1 THEN
PAUSE 50
ELSE
EXIT
ENDIF
LOOP UNTIL (counter=6)
IF IN8=1 AND IN10=1 AND IN3=1 THEN
SEROUT TX, Lcdbaud,["9"]
DO
IF IN9=0 THEN
PAUSE 50
ELSE
EXIT
ENDIF
LOOP
ENDIF
ENDIF
IF IN10=0 THEN 'Enter
POLLIN 9, 1
POLLIN 11, 1
POLLMODE 2
PAUSE 300
counter=0
DO
counter=counter+1
IF IN9=1 AND IN11=1 THEN
PAUSE 50
ELSE
EXIT
ENDIF
LOOP UNTIL (counter=6)
IF IN9=1 AND IN11=1 AND IN4=1 THEN
SEROUT TX, Lcdbaud,[ CR, "Thanks for banking"]
DO
IF IN10=0 THEN
PAUSE 50
ELSE
EXIT
ENDIF
LOOP
ENDIF
ENDIF
IF IN11=0 THEN 'Clear
POLLIN 10, 1
POLLIN 12, 1
POLLMODE 2
PAUSE 300
counter=0
DO
counter=counter+1
IF IN10=1 AND IN12=1 THEN
PAUSE 50
ELSE
EXIT
ENDIF
LOOP UNTIL (counter=6)
IF IN10=1 AND IN12=1 AND IN5=1 THEN
SEROUT TX, Lcdbaud,[LcdCls,"Key in PIN",CR]
DO
IF IN11=0 THEN
PAUSE 50
ELSE
EXIT
ENDIF
LOOP
ENDIF
ENDIF
IF IN12=0 THEN 'Cancel
POLLIN 11, 1
POLLIN 5, 1
POLLMODE 2
PAUSE 300
counter=0
DO
counter=counter+1
IF IN12 THEN
PAUSE 50
ELSE
EXIT
ENDIF
LOOP UNTIL (counter=6)
IF IN11=1 AND IN5=1 THEN
SEROUT TX, LcdBaud, [8, " ", 8]
DO
IF IN12=0 THEN
PAUSE 50
ELSE
EXIT
ENDIF
LOOP
ENDIF
ENDIF
RETURN
Comments
"Key in PIN" ???
This wouldn't be one of those fake ATMs would it?
It might help if your code had some comment lines, for starters.
What are these? Do you have a link?
And how are the buttons wired. A schematic would help.
There can be lots of issues when reading buttons.
I have a project which uses a 4x4 keypad. I ended up having some trouble reading the keypresses. I got some help here.
If you describe your setup well, others may be able to help you find a solution to any problems you're having. Notice, I included a schematic and a picture when I asked for help (hint, hint).
Why so many pauses?
Is this what you are using for a keypad? If so, it seems to me this could have troubles with reading other fingers, knuckles, thumbs, shirt sleeves, etc. while the person is pressing the desired buttons. The data sheet states it can read up to 1 inch from the sensor, and one of the graphs shows it could read maybe 2 inches if you do not set your thresholds with care.
http://www.mouser.com/ds/2/414/OPB732-10181.pdf
This description confuses me.
I think you need to describe your problem in more detail. And you need to describe in more detail what you want your code to do and not do.
If two sensors are being triggered, how is the program supposed to know which one of the sensors is the correct one to display? If I were looking at the keyboard with one of your fingers pressing on numbers 6 and 7, how could I tell you meant to display a 6 and not a 7?
Maybe what you need is a way to back up your cursor position, blank out the present display, back up the cursor position again and overwrite the previous number displayed with the new number? Is that what you're talking about?
I'm also wondering how you are avoiding double entries with the sensors you are using. It seems you will get an output as the finger approaches the sensor, and when it is touched the output goes away. When the finger is withdrawn you will get another output from the same sensor. If you are relying on pause commands then how will you account for a caffeine junkie who will touch the keys very quickly while ol' grandma Moses will very slowly push on each one.
They're PROXIMITY sensors, and are designed to detect something close by that's moving in a predictable way, not something that may actually touch them.
Because there's a gap between transmitter an receiver, it will stop detecting anything that comes too close.
And if you have several sensors grouped together like a keypad, you risk 'false positives' with the IR beam from one transmitter being reflected to the receiver of another sensor.
The ONLY WAY to make this work somewhat reliably at all is to set up 'walls' 1" high between the sensors so that the signal can't bounce off to the wrong place.