Trying to get started
T Chap
Posts: 4,223
I just got the P40 and demo board. I want to build a latch that will toggle on the same button.
This will turn on but not back off. Can someone tell me what is wrong? LED is on P0, switch is on P3.
Thanks
DO
DEBUG ? IN3
IF (IN3 = 1) AND (0 = 0) THEN
HIGH 0
PAUSE 500
ELSEIF (IN3 = 1) AND (0 = 1) THEN
LOW 0
PAUSE 250
ENDIF
LOOP
This will turn on but not back off. Can someone tell me what is wrong? LED is on P0, switch is on P3.
Thanks
DO
DEBUG ? IN3
IF (IN3 = 1) AND (0 = 0) THEN
HIGH 0
PAUSE 500
ELSEIF (IN3 = 1) AND (0 = 1) THEN
LOW 0
PAUSE 250
ENDIF
LOOP
Comments
PIN0 = 0
DO
DEBUG ? IN3
IF IN3 = 1 THEN
TOGGLE 0
ENDIF
PAUSE 500
LOOP
There might be a better way to do it, but this is pretty straight forward.
LED1 CON 0
DO
IF (IN3 = 1) AND (led1 = 0) THEN
PAUSE 100
HIGH led1
PAUSE 50
ENDIF
IF (IN3 = 1) AND (led1 = 1) THEN
PAUSE 100
LOW led1
ENDIF
LOOP
Maybe instead what you want is to detect the _transition_ of in3 from 0 to 1. Then you have to release it and push it again before led1 will change state. Is that right?
This uses the Stampese operators ^, exclusive or and &, AND.
(btn3 ^ btm3 & btn3 ) is 1 only if there is a change from 0 to 1 at in3
The statements execute only once each time the button is pressed. That might not be what you want?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
When inputting the code, I get an error at led1 = 1 saying "expected a label, variable or instruction", so I couldn't test it. I assume that code works on a BS2P40?
HIGH led1 ' instead of led1=1
and
LOW led1 ' instead of led1=0
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
If I copied it corectly, there must be something else missing, it will load but just turns on the led, never turns it off again though.
Here is what I have:
LED1 CON 0
btn3 VAR BIT
btm3 VAR BIT '·extra·state·variable,·to·hold·prior·state·of·in3
btx3 VAR BIT '·1·only·when·in3·changes·0-->1
btm3 = IN3
DO
btn3 = IN3 '·read·state·now
btx3 = btn3 ^ btm3 & btn3 '·detect·0-->1
btm3 = btn3 '·save·previous·state
IF btx3 = 1 THEN ' transition·0-->1?
SELECT led1 '·actions·based·on·state·of·led
CASE 0
HIGH led1
'·stuff·for·led 1=0
CASE 1
LOW led1
'·stuff·for·led1=1
ENDSELECT
ENDIF
LOOP
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
IF IN4 = 1 THEN
LOW led1
ENDIF
Now to move on to figure out wha you did to understand it completely.
Thanks again