global variables aren't acting very global?
stampinator
Posts: 18
Hi everyone,
I am working on a bs1 program, and am a little confused by the behavior I am getting...
if I do (simplified version of my code):
Basically what I am experiencing is, state is always different than lastState, even though they should be the same. So, for example, if state is $03, and the switch_triggered method gets called, then I expect lastState to get set to $03, so when the check method gets called, nothing should happen until the state changes again-- to say $7. However, what is actually happening is, as soon as switch_triggered gets called, the fire method gets called even though the state has not changed... I am totally confused by how this is possible, and what is going on. If anyone has any insight or suggestions I'd greatly appreciate it.
I am working on a bs1 program, and am a little confused by the behavior I am getting...
if I do (simplified version of my code):
symbol current = B0 symbol state = B1 symbol lastState = B2 symbol count = B3 symbol switch = B4 main: current = PINS state = current & $F IF count > 0 THEN check switch = current & $30 if switch = 0 THEN switch_triggered GOTO main check: IF state = lastState THEN main count = count - 1 GOTO fire switch_triggered: count = count + 1 lastState = state GOTO main fire: LET PIN7 = 1 PAUSE 100 LET PIN7 = 0 lastState = 0 GOTO main
Basically what I am experiencing is, state is always different than lastState, even though they should be the same. So, for example, if state is $03, and the switch_triggered method gets called, then I expect lastState to get set to $03, so when the check method gets called, nothing should happen until the state changes again-- to say $7. However, what is actually happening is, as soon as switch_triggered gets called, the fire method gets called even though the state has not changed... I am totally confused by how this is possible, and what is going on. If anyone has any insight or suggestions I'd greatly appreciate it.
Comments
foo = 123
bar = foo
It said that bar is a reference to foo. So I found that if I actually did lastState = PINS & $F, instead of lastState = state, then it worked.
I think Sapphire is correct. Pay attention to how the variables are initialized. The default is zero.