Help With Code for alarm
Hey I was trying to make a alarm for when a chair is removed from a desk, here is my code, my trouble is the getting alarm the alarm to go off or not.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I don't have a basic stamp, I AM The basic stamp 2
' {$STAMP BS2}
' {$PBASIC 2.5}
lcd PIN 15
ping PIN 14
baud CON 32
pb PIN 2
light PIN 0
bicolor PIN 1 'if you do not have a bi color then set this to 8
VARs:
LcdBkSpc CON $08 ' move cursor left
LcdRt CON $09 ' move cursor right
LcdLF CON $0A ' move cursor down 1 line
LcdCls CON $0C ' clear LCD (use PAUSE 5 after)
LcdCR CON $0D ' move pos 0 of next line
LcdBLon CON $11 ' backlight on
LcdBLoff CON $12 ' backlight off
LcdOff CON $15 ' LCD off
LcdOn1 CON $16 ' LCD on; cursor off, blink off
LcdOn2 CON $17 ' LCD on; cursor off, blink on
LcdOn3 CON $18 ' LCD on; cursor on, blink off
LcdOn4 CON $19 ' LCD on; cursor on, blink on
LcdLine1 CON $80 ' move to line 1, column 0
LcdLine2 CON $94 ' move to line 2, column 0
Scale CON $200
LcdCC0 CON $F8 ' define custom char 0
LcdCC1 CON $F9 ' define custom char 1
LcdCC2 CON $FA ' define custom char 2
LcdCC3 CON $FB ' define custom char 3
LcdCC4 CON $FC ' define custom char 4
LcdCC5 CON $FD ' define custom char 5
LcdCC6 CON $FE ' define custom char 6
LcdCC7 CON $FF ' define custom char 7
RawToIn CON 889 ' 1 / 73.746 (with **)
RawToCm CON 2257 ' 1 / 29.034 (with **)
chairdist VAR WORD
IsHigh CON 1 ' for PULSOUT
IsLow CON 0
rawDist VAR WORD ' raw measurement
alarm VAR BYTE
rawlow VAR WORD
rawhigh VAR WORD
Inde:
SEROUT lcd, baud, [noparse][[/noparse]lcdoff]
SEROUT lcd, baud, [noparse][[/noparse]lcdon1]
SEROUT lcd, baud, [noparse][[/noparse]lcdcls]
SEROUT lcd, baud, [noparse][[/noparse]lcdblon]
Author:
SEROUT lcd, baud, [noparse][[/noparse]lcdline1,"Author",Lcdline2,"Shane Patterson"]
PAUSE 1000
SEROUT lcd, baud, [noparse][[/noparse]lcdcls, lcdline1, "A.K.A", lcdline2, "Tryx"]
PAUSE 1000
measure:
SEROUT lcd, baud, [noparse][[/noparse]lcdcls,lcdline1, "Click Button", lcdline2, "With Chair"]
DO
LOOP UNTIL pb = 1
HIGH light
LOW bicolor
PAUSE 250
GOSUB pingm
LOW light
rawdist = chairdist
rawdist = 0
SEROUT lcd, baud, [noparse][[/noparse]lcdcls, lcdline1,"Great"]
PAUSE 1600
alarms:
SEROUT lcd, baud, [noparse][[/noparse]lcdcls, lcdline1, "Arming"]
PAUSE 100
SEROUT lcd, baud, [noparse][[/noparse]lcdbloff]
PAUSE 100
SEROUT lcd, baud, [noparse][[/noparse]lcdblon]
PAUSE 100
SEROUT lcd, baud, [noparse][[/noparse]lcdbloff, lcdoff]
alarm = 0
PAUSE 500
alarmloop:
PAUSE 100
GOSUB pingm
rawlow = rawdist - 0
rawhigh = rawdist + 0
IF rawlow < chairdist THEN alarmLOOP
IF rawhigh > chairdist THEN alarmloop
SEROUT lcd, baud, [noparse][[/noparse]lcdon1, lcdblon, lcdline1, lcdcls, " ALARM"]
HIGH light
LOW bicolor
PAUSE 100
SEROUT lcd, baud, [noparse][[/noparse]lcdbloff]
LOW light
PAUSE 100
SEROUT lcd, baud, [noparse][[/noparse]lcdblon]
DO
LOOP UNTIL pb = 1
GOSUB alarms
END
pingm:
rawDist = 0
Ping = IsLow ' make trigger 0-1-0
PULSOUT Ping, 5 ' activate sensor
PULSIN Ping, IsHigh, rawDist ' measure echo pulse
rawDist = rawDist */ Scale ' convert to uS
rawDist = rawDist / 2 ' remove return trip
RETURN
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I don't have a basic stamp, I AM The basic stamp 2

Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Powered by enthusiasm
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
The maximum number of times the gosub can be called without a return is 4 levels. (On the BS2) Sometimes used in recursive code but I think you really want a GOTO here instead.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Think Inside the box first and if that doesn't work..
Re-arrange what's inside the box then...
Think outside the BOX!