Shop OBEX P1 Docs P2 Docs Learn Events
Simple Prop to Prop Communication with Xbee — Parallax Forums

Simple Prop to Prop Communication with Xbee

BotulismBotulism Posts: 18
edited 2020-08-31 20:43 in Propeller 1
I have two identical devices consisting of 1 Prop Mini, red/blue LEDs, 1 button, and one standard/unchanged Xbee. This is baby steps for a larger project. One Prop starts blue, the other red. If the red Prop button is pressed, the blue Prop blinks Red and vice versa. I thought I would start be sending a simple number between props like the number 1. Turns out there is more to communicating than I thought. I can get one Prop to talk to my PC through the serial interface of the Xbee XCTU software, but I am clearly missing something when it comes to the second Prop receiving the information. (I have third Xbee connected to my PC to make sure data is being sent). I am aware of this document, but I am not finding anything about Prop to Prop communication in it. https://www.parallax.com/sites/default/files/downloads/122-32450-XBeeTutorial-v1.0.1.pdf
CON
  _clkmode = xtal1 + pll2x
  _xinfreq = 5_000_000


OBJ

  XB : "XBee_Object"                                    'XB Communication Port

                       
VAR
   byte LocalButton, RemoteButton                               
   byte BlueLED, RedLED, XB_Rx_Pin, XB_Tx_Pin           
   long Stack[40]
   
PUB Main      
  BlueLED := 16                                         'LED Pins
  RedLED := 17
  dira[BlueLED]~~                                       'Sets LED pin as output line with ~~
  outa[BlueLED]~                                        'Sets LED pin LOW or OFF with ~
  dira[RedLED]~~                                        'Sets LED pin as output line with ~~
  outa[RedLED]~                                         'Sets LED pin OFF with ~

                                            
  XB_Rx_Pin := 0                                        'Xbee pins
  XB_Tx_Pin := 1
     
  LocalButton := 15                                     'Local Button Pin
  dira[LocalButton] := 0                                'Set as INPUT
  
  RemoteButton := 0                                     'RemoteButton value

  XB.start(XB_Rx_Pin, XB_Tx_Pin, 0, 9600)               'Initialize the XBee
  waitcnt(clkfreq/2 + cnt)                              'Pause for 1/2 sec for XB to initialize
 
  cognew (XB_Rx, @Stack)                                'Cog for Rx

  outa[RedLED]~~                                        'Start with Red LEDs on 

  repeat                                                'Look for local button press, transmit a 1, and blink red LEDs
    if ina[LocalButton] == 1                            
      XB.Dec(1)
      XB.CR
      BlinkRed      

PUB XB_Rx 
  repeat                                                'Look for a remote button press and blink ble LEDs
    RemoteButton := XB.RxDec
    if RemoteButton == 1
      BlinkBlue
      RemoteButton := 0
      
PUB BlinkRed                                            'Blink Red LEDs then keep red LEDs on
  outa[BlueLED]~ 
  repeat 30
    !outa[RedLED]
    waitcnt(clkfreq/10 + cnt)
  outa[RedLED]~~
    
PUB BlinkBlue                                          'Blink Blue LEDs then keep red LEDs on
  outa[RedLED]~ 
  repeat 30
    !outa[BlueLED]
    waitcnt(clkfreq/10 + cnt)
  outa[RedLED]~~

Comments

  • You need to set an IO pin as an output from the same cog which turns it on and off.

    It's generally a good idea to only have one cog interact with individual IO pins. Whichever cog sets the red LED pin as an output should also be the cog to set the pin high or low. The same is true for the green LED.

    Are you running the Propeller at 10 MHz? You likely know it can run 8 times faster.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2020-08-31 23:34
    Here's one possible solution to the cog problem. Use state variable to indicate which LED should be toggled.

    I added a couple variables to the VAR section:
    VAR
       byte LocalButton, RemoteButton                               
       byte BlueLED, RedLED, XB_Rx_Pin, XB_Tx_Pin           
       long Stack[40]
       long RedState, BlueState
    

    Here's the other code I changed.
      repeat                                                'Look for local button press, transmit a 1, and blink red LEDs
        if ina[LocalButton] == 1                            
          XB.Dec(1)
          XB.CR    
          RedState := true
        if RedState
          BlinkRed
          RedState := false
        if BlueState
          BlinkBlue
          BlueState := false
              
    PUB XB_Rx 
      repeat                                                'Look for a remote button press and blink ble LEDs
        RemoteButton := XB.RxDec
        if RemoteButton == 1
          BlueState := true
          RemoteButton := 0
    

    I have not tested this.

    Edit: If you're going to use "true" as a possible variable value, make sure you use a long. "true" equals negative one. Only longs can equal negative one.

    The code blocks below will produce the same comparison. RedState doesn't have to be "true" or -1 to satisfy the condition. RedState just needs to be not zero (aka false).
        if RedState
    
        if RedState <> 0
    

    The "true" and "false" conditions confused me when I first started programming the Propeller. Hopefully these values are not confusing to you.
  • Your example works great! Every other time I've done something like this one side was either the Tx or the Rx and thus no problems. Thank you!
Sign In or Register to comment.