Shop OBEX P1 Docs P2 Docs Learn Events
trouble with xbee code — Parallax Forums

trouble with xbee code

gennarobasso81gennarobasso81 Posts: 84
edited 2012-10-02 20:50 in Accessories
hello all,

I am having trouble with some code for an xbee. overview of project is i have two bs2's connected to xbees, I have one bs2/xbee with two pushbuttons the other with two different colored led's. I am trying to get it so one pushbutton will control one color and the other pushbotton the other color.

problem i am having:
regardless of what button is pushed the lights go on in a pattern. first the green, then the red. If i hit Pushbutton one 5 time it will go green, red, green......and so on. it i hit any combination of the two buttons it still repeats the pattern of green, red, green, red, green,...and so on

IMG_2422.jpg
IMG_2423.jpg
IMG_2425.jpg
IMG_2424.jpg



transmitting code
' {$STAMP BS2}
' {$PBASIC 2.5}
 

'----------------[Initialize]---------------------------
PAUSE 5
SEROUT 2, 84, [22,12]

SEROUT 2, 84,50, [22,17, "Hello", 13, "Geno"]
PAUSE 1000
SEROUT 2, 84, [22,12]
PAUSE 5
'---------------[Variables]-----------------------------
LR            VAR     Word
UD            VAR     Word
btn1wrk       VAR     Byte
btn2wrk       VAR     Byte
Led           VAR     Bit
Led1          VAR     Bit

'---------------[Constants]-----------------------------
BUTTON btn1, 1, 0, 0, btn1Wrk,0, No_Press
BUTTON btn2, 1, 0, 0, btn2Wrk,0, No_Press
Btn1          PIN     6
Btn2          PIN     7
Tx            PIN     14 ' XBee DIN
Rx            PIN     15 ' XBee DOUT
stickUD       CON     25
stickLD       CON     25
 

'-------------------------[Main]---------------------------
 
Main:

HIGH 13
PAUSE 2
RCTIME 13, 1, UD
HIGH 12
PAUSE 2
RCTIME 12, 1, LR
DEBUG HOME, "UD =", DEC UD, CLREOL, CR,
"LR = ", DEC LR, CLREOL
IF UD > stickUD + 2 THEN GOSUB Forward
IF UD < stickUD - 2 THEN GOSUB Backwards
IF UD = stickUD     THEN GOSUB Clear
IF LR > stickLD + 2 THEN GOSUB Right
IF LR < stickLD - 2 THEN GOSUB Left
IF LR = stickLD     THEN GOSUB Clear
IF btn1 =1 THEN
DEBUG CR, "Button 1 pushed"
GOSUB Left_Turn
DEBUG CLS
ENDIF
IF btn2 = 1 THEN
DEBUG CR, "Button 2 pushed"
GOSUB Right_Turn
DEBUG CLS
ENDIF

No_Press:
 GOTO main

GOTO main

'-------------------------[Sub Routine]-----------------------
Forward:
SEROUT 2,84, 20, [17, 128, "Forward"]
PAUSE 200
RETURN
Backwards:
SEROUT 2, 84, 20, [17, 128, "Backwards"]
PAUSE 200
RETURN
Right:
SEROUT 2, 84, 20, [17, 148, "Right"]
PAUSE 200
RETURN
Left:
SEROUT 2, 84, 20, [17,148, "left"]
PAUSE 200
RETURN
Clear:
SEROUT 2, 84, [12,17]
PAUSE 200
RETURN

Left_Turn:
IF  Btn1 = 1 THEN     SEROUT 2, 84, ["Left", 13, "Turn"]
                      SEROUT Tx, 84,[ DEC Led,CR]
                      PAUSE 500
IF  Btn1 = 0 THEN     SEROUT 2, 84, [12]
                      PAUSE 5
                      RETURN
 

Right_Turn:
IF btn2 = 1 THEN      SEROUT 2, 84, ["Right", 13, "Turn"]
                      SEROUT Tx, 84, [DEC Led1,CR]
                      PAUSE 500
IF btn2 = 0 THEN      SEROUT 2, 84, [12]
                      PAUSE 5
                      RETURN


receiving code
' ***************************************************
' Simple_Control_Remote.bs2
' Receives decimal value to control buzzer and LED
' ***************************************************
' {$STAMP BS2}
' {$PBASIC 2.5}
#SELECT $STAMP
#CASE BS2, BS2E, BS2PE
T9600 CON 84
#CASE BS2SX, BS2P
T9600 CON 240
#CASE BS2PX
T9600 CON 396
#ENDSELECT
' ***************** Variables, Constants and Pins
Baud CON T9600 ' Set Baud rate
Rx PIN 15 ' XBee DOUT
Tx PIN 14 ' XBee DIN
Led PIN 0
Led1 PIN 1
green VAR Byte
red   VAR Byte
' ***************** Main Loop
DO
SERIN Rx, Baud,  [DEC green]  ' Wait for decimal and accept
HIGH LED ' Turn on LED
PAUSE 1000
LOW LED ' Turn off LED
SERIN Rx, Baud, [DEC red]
HIGH Led1
PAUSE 1000
LOW Led1
LOOP
1024 x 683 - 84K
1024 x 683 - 87K
1024 x 683 - 104K
1024 x 683 - 63K

Comments

  • W9GFOW9GFO Posts: 4,010
    edited 2012-10-01 20:25
    As it is now your receiving code has no way of skipping over either of the LEDs.
  • gennarobasso81gennarobasso81 Posts: 84
    edited 2012-10-01 20:38
    do you know a good way to use an IF / THEN statement with the serin Rx. I played around with a lot of different variations of the code.

    this is one example. It was all i had to show because i didnt save any of the non working codes.
    ' ***************************************************
    ' Simple_Control_Remote.bs2
    ' Receives decimal value to control buzzer and LED
    ' ***************************************************
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    #SELECT $STAMP
    #CASE BS2, BS2E, BS2PE
    T9600 CON 84
    #CASE BS2SX, BS2P
    T9600 CON 240
    #CASE BS2PX
    T9600 CON 396
    #ENDSELECT
    ' ***************** Variables, Constants and Pins
    Baud CON T9600 ' Set Baud rate
    Rx PIN 15 ' XBee DOUT
    Tx PIN 14 ' XBee DIN
    Led PIN 0
    Led1 PIN 1
    ' ***************** Main Loop
    DO
    SERIN Rx, 84, [DEC led]
    SERIN Rx, 84, [DEC led1]
    IF Led = 1 THEN
    HIGH LED ' Turn on LED
    PAUSE 1000
    LOW LED ' Turn off LED
    ENDIF
    IF Led1 = 1 THEN
    HIGH Led1
    PAUSE 1000
    LOW Led1
    ENDIF
    LOOP
    
  • W9GFOW9GFO Posts: 4,010
    edited 2012-10-01 21:01
    Place what you receive from SERIN into a VAR then drive the LEDs based upon the contents of that VAR.
  • gennarobasso81gennarobasso81 Posts: 84
    edited 2012-10-02 20:50
    thank you for ur advice. I did try this before but after revisiting and trying again i got it.

    transmit
    Left_Turn:
    IF  Btn1 = 1 THEN     SEROUT 2, 84, ["Left", 13, "Turn"]
                          SEROUT Tx, 84,[DEC 0,CR]
                          PAUSE 500
    IF  Btn1 = 0 THEN     SEROUT 2, 84, [12]
                          PAUSE 5
                          RETURN
     
    
    Right_Turn:
    IF btn2 = 1 THEN      SEROUT 2, 84, ["Right", 13, "Turn"]
                          SEROUT Tx, 84, [DEC 1,CR]
                          PAUSE 500
    IF btn2 = 0 THEN      SEROUT 2, 84, [12]
                          PAUSE 5
                          RETURN
    


    receive
    Rx PIN 15 ' XBee DOUT
    Tx PIN 14 ' XBee DIN
    green PIN 0
    red   PIN 1
    Led   VAR Bit
    Led1  VAR Bit
    ' ***************** Main Loop
    DO
    SERIN Rx, 84, [DEC Led]
    SERIN Rx, 84, [DEC Led1]
    IF Led = 0 THEN
    HIGH green ' Turn on LED
    PAUSE 1000
    LOW green ' Turn off LED
    ENDIF
    IF Led1 = 1 THEN
    HIGH red
    PAUSE 1000
    LOW red
    ENDIF
    LOOP
    
    
Sign In or Register to comment.