Shop OBEX P1 Docs P2 Docs Learn Events
IF THEN Help — Parallax Forums

IF THEN Help

BassistBassist Posts: 10
edited 2010-06-02 16:44 in BASIC Stamp
I need to send "sre*" to a 232 device. I am able to do that but I need to send the string when pin0 = 1. I have not been able to figure out the IF THEN· statement. Any help will be appreciated. Thanks in advance.

Comments

  • FranklinFranklin Posts: 4,747
    edited 2010-05-31 02:15
    Show us your code!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • BassistBassist Posts: 10
    edited 2010-05-31 02:58
    Here you go.· If there is a better way to do this please let me know.

    Record:
    · IF (IN0 = 1) THEN Main




    ·Main:
    ·· DO
    · SEROUT 16, 16780, [noparse][[/noparse]"sre*",CR]

    · PAUSE 2500
    · LOOP


    · END
  • FranklinFranklin Posts: 4,747
    edited 2010-05-31 03:34
    So, does that code work for you? If not you need to think about what the commands do and why it is not working. (HINT) What happens if IN0 is not 1

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • ercoerco Posts: 20,256
    edited 2010-05-31 03:35
    No parens.

    IF IN0=1 THEN Main

    unless you are using a BS1, in which case

    IF PIN0=1 THEN Main

    The PBasic editor turns colors to help ID syntax/typing errors. Make sure you used "zero" and not "oh".

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"If you build it, they will come."
  • BassistBassist Posts: 10
    edited 2010-05-31 03:54
    I took the parens out and it still works the same. When I look at it with Hyperterminal it displays sre* after 2.5 seconds whether the If statement is in or not....
  • ercoerco Posts: 20,256
    edited 2010-05-31 04:40
    You do have an external connection to pin0 that goes from zero volts to +5v volts? Guaranteed? Make direct connections and retry.

    Attach your entire program.

    And per Stephen, you must have something like:

    Record:
    IF (IN0 = 1) THEN Main
    goto record' loop back if IN0=0

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"If you build it, they will come."
  • BassistBassist Posts: 10
    edited 2010-05-31 05:18
    Thanks Erco, that did the trick. Today is my first day so I have alot to learn. I have another question. I think I read that if you are using the programming·port to send data out·that you need both the transmit and receive wires connected to get the proper voltage levels. The application I hope to use this for requires the receive wire be connected to another device, so I will only have the transmit wire available. Will·this work? Do I need to use a MAXIM 232 and a output pin?· Thanks again to both of you, I appreciate the help!



    ' TEST.bs2
    ' This program transmits the string "sre*" followed by a
    ' carriage-return at 2400 baud, inverted, N81 format.

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    ·INPUT 0

    ·Record:
    · IF IN0 = 1 THEN Main
    · GOTO Record

    ·Main:
    · DO
    · SEROUT 16, 16780, [noparse][[/noparse]"sre*",CR]
    · PAUSE 2500
    · LOOP

    · END
  • BassistBassist Posts: 10
    edited 2010-05-31 06:01
    I was mixed up on the receive and transmit wires on the last post. I will only have the receive wire available, not the transmit. TNX
  • BassistBassist Posts: 10
    edited 2010-06-01 17:40
    I found out I need CR/LF for this to work. I do not see LF referenced anywhere... HELP
  • BassistBassist Posts: 10
    edited 2010-06-01 17:46
    Okay so I'm stupid...
  • BassistBassist Posts: 10
    edited 2010-06-01 17:57
    when I add LF here:

    Main:
    DO
    SEROUT 16, 16780, [noparse][[/noparse]"sre*",CR,LF]
    PAUSE 2500
    LOOP

    END

    It spits out the string repeatedly no matter the state of input 0. I think I need to send the hex representation of sre*CRLF as the string. How do I do this? Thanks, Tab
  • FranklinFranklin Posts: 4,747
    edited 2010-06-02 03:01
    Why would you think it would act otherwise, Look at your code:

    start loop
    serout
    wait a while
    repeat loop
    there is no checking of anything in the loop

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • BassistBassist Posts: 10
    edited 2010-06-02 03:28
    I removed the DO and LOOP. It works better but is still not doing what I want.
  • Spiral_72Spiral_72 Posts: 791
    edited 2010-06-02 16:33
    Computers do EXACTLY what you tell them, nothing more, nothing less. It's a different way of thinking, kind of like telling a child what to do.


    An If-Then statement works like this:

    If IN0 = 1 then do something........if not, then go to the next instruction.

    So, if you have:

    IF IN0 = 1 then goto main

    Main:
    send some serial data

    END



    It will execute the Main routine regardless of pin 0's state because if IN0 = 1, it goes to the routine Main. If IN0 = 0 it falls through the condition (because it's not true) and executes the next command, which happens to be THE MAIN ROUTINE!!!

    By doing the

    Record:
    IF IN0=1 goto Main
    goto Record

    You'll check pin 0 over and over forever..... or until pin 0 = 1...... the little "GOTO Record" is the next instruction past the If-Then condition, so when the If-Then is false, it executes the next command which starts the check again. When pin 0 = 1, it leaves the little check routine and executes new code (your Main routine)


    The END statement stops everything. Period. It don't do no mo' until you power off or hit the reset button..... So depending on what you want you code to do, you might want to put a "Goto Record" after your Main routine, but before the END command.... so if pin 0 = 1, it sends your serial data, then starts the whole program again.


    I hope all that makes sense and is helpful..... Now I go back to work [noparse]:)[/noparse]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "puff"...... Smile, there went another one.
  • BassistBassist Posts: 10
    edited 2010-06-02 16:44
    Thanks Spiral_72. That helps!
Sign In or Register to comment.