Shop OBEX P1 Docs P2 Docs Learn Events
My first Basic Stamp project (SERIN and Code Questions) — Parallax Forums

My first Basic Stamp project (SERIN and Code Questions)

SagebrushSagebrush Posts: 7
edited 2009-12-14 03:44 in Learn with BlocklyProp
Hello, I am trying to create communication between a BS1 (Transmitter) and BS2 (Receiver) to do three simple functions: turn LED 1 on/off, turn LED 2 on/off, or turn both LEDs off from whatever state they are in.

To do this, I have 3 buttons attached to the transmitter to put a voltage across pins 1,2, or 3 on the BS1.

I'm currently using a TWS-434a and RWS-434 to transmit and receive. Unfortunately, I haven't had any success, and I am not sure if it is my wiring, or my code. I have not received any syntax errors, but I wonder if the following code would be good to perform the functions I have in mind? Thanks for your time and suggestions.

Transmitter:
' {$STAMP BS1}
SYMBOL slowdown = B3
SYMBOL code = W4      'The code determines the receiver operation'
SYMBOL synch = W5     'The synch is like the starting key'
SYMBOL junk = B6      'Junk gets the receiver running'
LET junk = 123
LET synch = 63158
DIRS = %00000001
LET code = 000
Start:
PAUSE 30
slowdown = 1
IF PIN1 = 1 THEN Operation1          'Depending on which button is depressed'
IF PIN2 = 1 THEN Operation2          'A different code is sent'
IF PIN3 = 1 THEN Operation3
IF PINS = %00000000 THEN Pauser
IF slowdown = 1 THEN Start
IF code <> 000 THEN Transmit
IF slowdown = 0 THEN CodeReset
PAUSE 70
GOTO Start

Operation1:
LET code = 11013
RETURN

Operation2:
LET code = 21013
RETURN

Operation3:
LET code = 31013
RETURN

Pauser:
LET slowdown = 0
RETURN

CodeReset:
LET code = 000
RETURN

Transmit:
SEROUT 0,N2400,(junk,synch,code)    'This sends the junk, followed by the synch'
RETURN                              'and code'




Receiver:
 ' {$STAMP BS2}
synch CON 63158    'This tells the receiver what to expect'
BAUD CON 16780
code VAR Word
DIRH=%11111111

Start:
SERIN 0,BAUD,[noparse][[/noparse]WAIT(synch),code]
IF code=11013 THEN Light1          'Depending on code values'
IF code=21013 THEN Light2          'One of three operations will run'
IF code=31013 THEN Light3
GOTO Start

Light1:
TOGGLE 14       'Turns the 1st set of lights on or off'
RETURN

Light2:
TOGGLE 15       'Turns the 2nd set of lights on or off'
RETURN

Light3:
LOW 14          'Turns all lights off'
LOW 15
RETURN

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-12-12 18:29
    First of all, you need to understand GOSUB / RETURN. Both programs use a RETURN statement to try to return to the statement after an IF. That won't work. "IF <condition> THEN <label>" does a GOTO to the <label> if the <condition> succeeds. The RETURN will cause the Stamp to restart. What you need is to put a label (<next label>) after the IF statement and use "GOTO <next label>" instead of the RETURN. Like this:
    IF code = 55555 THEN myLabel1
    nextLabel1:
    


    myLabel1:
    LOW 0
    GOTO nextLabel1
    


    You could also skip the use of "<next label>" and just do:
    IF code = 44444 THEN myLabel1
    


    myLabel1:
    LOW 1
    GOTO Start
    
  • SagebrushSagebrush Posts: 7
    edited 2009-12-12 18:35
    Thank you so much for that information, I'll eliminate the use of RETURN from my code.
  • SagebrushSagebrush Posts: 7
    edited 2009-12-14 03:44
    Thanks to the help I've received so far, I've feel I've come along fairly well on my project. However, I'd still like to pick the brain of anyone willing to offer their advice.

    My questions still relate to SERIN/SEROUT Syntax, as well as actual circuit connections. I'm using a couple of 433 MHz transceivers presently.


    • For one, I assume I should use pull down resistors for on the TX-RX pins?
      Furthermore, should I use pull down resistors on my data output/input pins?
      When using SEROUT with a BS1 module, if I want to just store a number into a word variable, like 11013 in my first post, should I place a '#' before the variable name? (As seen in the code to follow)
      To go along with that, when using SERIN, should I place DEC before the variable name on the BS2 Module?

    Here are some pieces of code, so far I haven't gotten a response from the receiver, even when directly connecting the receiver and transmitter data pins.

    BS1 Transmitter Excerpt:
    symbol code = W4
    LET code = 11013
    PULSOUT 0,300 
    SEROUT 0,N2400,(junk,synch,#code,"B")
    
    



    BS2 Receiver Excerpt
    synch CON "A"  
    BAUD CON 16780
    code VAR Word
    SERIN 0,BAUD,[noparse][[/noparse]WAIT(synch),DEC code]
    IF code=11013 THEN Light1
    
    
Sign In or Register to comment.