Shop OBEX P1 Docs P2 Docs Learn Events
Trying to get started — Parallax Forums

Trying to get started

T ChapT Chap Posts: 4,217
edited 2006-05-24 17:05 in BASIC Stamp
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

Comments

  • bushibushi Posts: 12
    edited 2006-05-23 20:32
    Well I think your problem is the (0 = 1) statement. In this case, it is a always false. You may want to try:

    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.
  • T ChapT Chap Posts: 4,217
    edited 2006-05-24 01:15
    Well I really wanted to not use toggle, since I will need to add other commands to these with the same if statements as below. Any ideas why this just turns on and not back off on the second press? It seems using Led1 = 0 works fine with in3 = 1, but using Led1 = 1 does not work.


    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
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2006-05-24 02:27
    It seems that if you activate the switch (in3=1), and hold it there, the program will put led1 high for 150 milliseconds and then low for 100 milliseconds, in a loop. If you release the button (in3=0), then the led will stick with whatever state it had at the last IF statement.

    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?

    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
        led1 = 1
         ' stuff for led1=0
         CASE 1
        led1 = 0
          ' stuff for led1=1
         ENDSELECT
      ENDIF
    LOOP
    



    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
  • T ChapT Chap Posts: 4,217
    edited 2006-05-24 03:01
    Thanks for the help, I think that is what I want to do: Press a button once, led changes state to high, press button again, led changes state to low, just like a toggle.

    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?
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2006-05-24 03:20
    Oh, it should say
    HIGH led1 ' instead of led1=1
    and
    LOW led1 ' instead of led1=0

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • T ChapT Chap Posts: 4,217
    edited 2006-05-24 03:28
    Well the concept looks great, I appreaciate you getting me on the right track.

    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 AllenTracy Allen Posts: 6,658
    edited 2006-05-24 16:48
    My bad, again--the problem with off-the-cuff programing is that you don't get a reality check. YOU are the reality check! The value led1 has to be declared as a PIN, not as a CONstant. That is because it is both an output, as in "HIGH led1", and an input, as "SELECT led1"

    ' {$PBASIC 2.5}
    
    LED1 PIN 0     ' <---- note that this is now PIN instead of CON
    btn3 VAR BIT  ' present state of in3
    btm3 VAR BIT ' 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
  • T ChapT Chap Posts: 4,217
    edited 2006-05-24 17:05
    EXCELLENT! That is the solution! thanks so much. You know how it is the first time you try to make a micro do something, I added this just for a test as well with a secons switch:

    IF IN4 = 1 THEN

    LOW led1

    ENDIF

    Now to move on to figure out wha you did to understand it completely.

    Thanks again
Sign In or Register to comment.