Shop OBEX P1 Docs P2 Docs Learn Events
Turning on LEDs remotely with xbee — Parallax Forums

Turning on LEDs remotely with xbee

DavidNymanDavidNyman Posts: 52
edited 2014-09-18 18:27 in Accessories
I've got a project where I need to turn on or off three LEDs in another room. I've downloaded the circuits from Parallax Simple_Byte_Rx.BS2 and Simple_Byte_Tx.bS2 and got them up and running with two Xbee S1 radios. I can turn on the LED just as the program is designed to do. Here's my problem:
I'm not versed well enough with xbees yet to have much luck modifying the code. At first it looked like simple cut and paste to add a few LEDs, but I learned very quickly it wasn't that easy.
On the base (Tx), I have three normally open momentary pushbuttons tied to PIN 0, PIN 1, PIN 2. The inputs are pulled low with a 2.2k resistor to ground and the switches are tied to +5 effectively bring the input high when the button is pressed, otherwise the circuit is wired just as the program shows.
The remote (Rx), has three LEDs, Red, Yellow and Green tied to PIN 0, PIN 1 and PIN 2 through 220 ohm resistors. The rest of the circuit is wired just as the program for the receiver shows.
On the receiver, the buzzer isn't needed and I haven't included it on my breadboard mockups.

Can anyone help me add control over two more LEDs?

Here's the initial programs as downloaded from Parallax:


' ***********************************************
' Simple_Byte_Tx.bs2
' Sends the state of pushbutton every 250mSec
' ***********************************************

' {$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 CON 15 ' XBee DOUT
Tx CON 14 ' XBee DIN

PB PIN 0 ' Pushbutton

State VAR Bit

' ***************** Main Loop
DO
State = PB ' Read pushbutton
SEROUT Tx, Baud,[State] ' Send pushbutton value as byte
PAUSE 250 ' short delay
LOOP


' ***************************************************
' Simple_Byte_Rx.bs2
' Receives byte value (0/1) to control LED and buzzer
' ***************************************************

' {$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 CON 15 ' XBee DOUT
Tx CON 14 ' XBee DIN

State VAR Bit

Led PIN 0
Buzzer PIN 5

' ***************** Main Loop
DO
SERIN Rx, Baud, [State] ' Wait for byte and accept
IF State = 1 THEN ' Based on value,
HIGH LED ' if 1, Turn on LED
FREQOUT Buzzer,200,3000 ' Sound buzzer
ELSE
LOW LED ' if 0, turn off LED
ENDIF
LOOP



+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
' ***********************************************
' Simple_Byte_Tx.bs2
' Sends the state of pushbutton every 250mSec
' ***********************************************

' {$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 CON 15 ' XBee DOUT
Tx CON 14 ' XBee DIN

PB PIN 0 ' Pushbutton

State VAR Bit

' ***************** Main Loop
DO
State = PB ' Read pushbutton
SEROUT Tx, Baud,[State] ' Send pushbutton value as byte
PAUSE 250 ' short delay
LOOP

Comments

  • PJAllenPJAllen Banned Posts: 5,065
    edited 2014-09-13 16:46
    Hi, David.
    I've seen this Subject languish for several hours with nary a response.
    If nobody gets around to it in the next few days then I'll jump in.
  • DavidNymanDavidNyman Posts: 52
    edited 2014-09-13 17:33
    I'm getting closer. I realized that I would need some data flow control to pull this off. I started looking at "Manual_Polling_Base.BS2 and Polling_Remote.BS2 and got some inspiration. With a few cut and pastes, I came up with what is printed below. The circuit is up and working great between the remote and base as long as I run the Base terminal program to control my LEDs on the Remote. I need to remove the terminal usage and change to 3 buttons wired to PIN 0, PIN 1 & PIN 2.
    Here's where I'm at currently:

    ' ******************************************************
    ' Manual_Polling_Base.bs2
    ' This program:
    ' - Configures XBee for fast AT Command Mode
    ' - Using DEBUG Window, User can control Remote
    ' - R-RedLED, Y-YellowLED and G-GreenLED.
    ' - Address changed to a constant.
    ' - Accepts an acknowledgement value
    ' - Requires 802.15.4 XBee (Series 1)
    ' ******************************************************
    ' {$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
    Green_button PIN 0
    Red_button PIN 1
    Yellow_button PIN 2
    Baud CON T9600 ' Set Baud rate
    Rx PIN 15 ' XBee DOUT
    Tx PIN 14 ' XBee DIN
    DataOut VAR Word ' Frequency to send
    DL_Addr CON $2 ' Destination address for data
    DataIn VAR Byte ' General variable for data
    Light VAR Word ' Returned light level
    ' ***************** Configure XBee in AT Command Mode
    PAUSE 500
    DEBUG CLS,"Configuring XBee..."
    PAUSE 3000 ' Guard time
    SEROUT Tx,Baud,["+++"] ' Command Mode Sequence
    PAUSE 2000 ' Guard time
    SEROUT Tx,Baud,["ATGT 3,MY 0",CR] ' Set low guard time and Base address
    SEROUT TX,Baud,["ATCN",CR] ' Exit Command Mode
    ' ***************** Main Loop
    DO

    GOSUB Config_XBee ' Set DL address of XBee

    DEBUG CR,"Choose Action:",CR,
    "R - Red LED",CR,
    "G - Green LED",CR,
    "Y - Yellow LED",CR,
    "? "
    DEBUGIN DataIn ' Accept choice


    SELECT DataIn ' LED control, get state and send
    CASE "G","l"
    DEBUG CR,"Enter LED State (0/1):"
    DEBUGIN DEC DataOut ' Accept user data
    DEBUG "Sending Data!",CR
    SEROUT Tx,Baud,["G",CR,CR] ' Send G
    SEROUT Tx,Baud,[DEC DataOut,CR,CR] ' Send LED state
    GOSUB CheckAck ' Get Acknowledgement
    GOTO Done

    ' LED control, get state and send
    CASE "R","l"
    DEBUG CR,"Enter LED State (0/1):"
    DEBUGIN DEC DataOut ' Accept user data
    DEBUG "Sending Data!",CR
    SEROUT Tx,Baud,["R",CR,CR] ' Send L
    SEROUT Tx,Baud,[DEC DataOut,CR,CR] ' Send LED state
    GOSUB CheckAck ' Get Acknowledgement
    GOTO Done

    ' LED control, get state and send
    CASE "Y","l"
    DEBUG CR,"Enter LED State (0/1):"
    DEBUGIN DEC DataOut ' Accept user data
    DEBUG "Sending Data!",CR
    SEROUT Tx,Baud,["Y",CR,CR] ' Send L
    SEROUT Tx,Baud,[DEC DataOut,CR,CR] ' Send LED state
    GOSUB CheckAck ' Get Acknowledgement
    GOTO Done


    ENDSELECT
    Timeout:
    DEBUG "No data received",CR
    Done:
    PAUSE 2000
    LOOP

    Config_XBee:
    ' Configure XBee for destination node address
    PAUSE 10 ' Short guard time
    SEROUT Tx,Baud,["+++"] ' Command Mode sequence
    PAUSE 10 ' Short guard time
    SEROUT TX,Baud,["ATDL ", HEX DL_Addr,CR] ' Set Destination Node Address
    SEROUT Tx,Baud,["ATCN",CR] ' Exit Command Mode
    RETURN

    CheckAck:
    SERIN Rx,Baud,1000,CheckTimeout,[DEC dataIn] ' Accept incoming byte
    IF dataIn = 1 THEN ' If 1, then ack'd
    DEBUG BELL,"OK - Ack Received!",CR
    ELSE ' If received, but not "1", problem
    DEBUG "Bad Ack!",CR
    ENDIF
    RETURN

    CheckTimeout:
    DEBUG "No ack received!",CR ' If nothing recieved
    RETURN

    ' ******************************************************
    ' Polling_Remote.bs2
    ' This program accepts a character and values:
    ' - G & 0 or 1 to control state of LED
    ' - R & 0 or 1 to control state of LED
    ' - Y & 0 or 1 to control state of LED
    '
    ' Return acknowledgements or value to Base
    '
    ' Requires 802.15.4 XBee (Series 1)
    ' ******************************************************
    ' {$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
    '****************** Variable, Constants and Pins
    Baud CON T9600
    GLED PIN 7
    YLed PIN 6
    RLED PIN 5
    'Servo PIN 12
    Rx PIN 15 ' XBee DOUT
    Tx PIN 14 ' XBee DIN
    RTS PIN 11 ' XBee RTS
    Freq VAR Word ' Received frequency for buzzer
    GState VAR Bit ' Received state of green LED
    RState VAR Bit ' Received state FOR red LED
    YState VAR Bit ' Received state for yellow LED
    DataIn VAR Byte ' General byte data
    Light VAR Word ' Measured light level
    Position VAR Word ' Received servo position
    My_Addr CON $2 ' Set address of node, modify as desired, $1-$FFFE
    ' ***************** Configure XBee to use RTS and set Address
    Position = 750
    PAUSE 500
    DEBUG CLS,"Configuring XBee...",CR
    PAUSE 3000 ' Guard time
    SEROUT Tx,Baud,["+++"] ' Command Mode Sequence
    PAUSE 2000 ' Guard time
    SEROUT Tx,Baud,["ATD6 1",CR] ' Enable RTS
    SEROUT Tx,Baud,["ATMY ", HEX My_Addr,CR] ' Set node address
    SEROUT Tx,Baud,["ATDL 0,CN",CR] ' Set destination address of Base
    ' & Exit Command Mode
    ' ***************** Main Loop
    DO
    GOSUB AcceptData
    GOSUB Control
    LOOP
    AcceptData:
    SERIN Rx\RTS,Baud,10,Timeout,[DataIn] ' Accept byte
    SELECT DataIn
    CASE "G" ' G to control Green LED
    SERIN Rx\RTS,Baud,1000,Timeout,[DEC GState] ' Accept LED state
    PAUSE 200 ' Give Base time to set up
    SEROUT Tx,Baud,[CR,DEC 1,CR] ' Return acknowledgment
    CASE "R" ' R to control Red LED
    SERIN Rx\RTS,Baud,1000,Timeout,[DEC RState] ' Accept buzzer frequency
    PAUSE 200 ' Give Base time to set up
    SEROUT Tx,Baud,[CR,DEC 1,CR] ' Return acknowledgment
    CASE "Y" ' Y to control Yellow LED
    SERIN Rx\RTS,Baud,1000,Timeout,[DEC YState]' Accept position
    PAUSE 200
    SEROUT Tx,Baud,[CR,DEC 1,CR] ' Return acknowledgment
    ENDSELECT
    Timeout:
    RETURN

    Control:
    IF GState = 1 THEN ' Control LED based on state
    HIGH GLED
    ELSE
    LOW GLED
    ENDIF
    IF YState = 1 THEN ' Control Buzzer based on Freq
    HIGH YLed
    ELSE
    LOW YLed
    PAUSE 100
    ENDIF
    IF RState = 1 THEN ' Control Buzzer based on Freq
    HIGH RLed
    ELSE
    LOW RLed
    PAUSE 100
    ENDIF

    RETURN
  • GenetixGenetix Posts: 1,754
    edited 2014-09-13 23:38
    Hello David,
    In PBASIC there are several variables that control the input and output of the BS2. IN0, IN1, and IN2 tell you what the input states of Pins 0, 1, and 2 are. INA tells that states of Pin 0-3, so you only need the line State = INA. There are similar variables for output as well. The BS2 pins start out as inputs so you would need change pins to outputs and this is done using the DIR variable. DIRA = %0111 will change Pin 0, 1, and 2 to output. Then you could add the line OUTA = State.

    Look up DIR, IN, and OUT in the Basic Stamp Manual.
    http://www.parallax.com/downloads/basic-stamp-manual

    That should get you started. You should write a simple test program though to test each of your switches and LEDs so you know they work before you start modifying programs.
    DEBUG ? IN0 ' Print the value of Pin 0 on the screen

    High 0 ' Turn on LED
    PAUSE 500 ' Wait for 1/2 second
    Low 0 ' Turn off LED

    Good Luck!
  • DavidNymanDavidNyman Posts: 52
    edited 2014-09-14 08:43
    Well, after a long night, Here's the finished code. It's working great. I can set the remote unit in the kitchen and run it from the garage.
    Time for the happy dance.
    Thanks very much for the help.
    Here's how it turned out:

    ' ******************************************************
    ' Desktop transmitter.bs2
    ' 14 Sept 2014
    ' This program:
    ' - Configures XBee for fast AT Command Mode
    ' - Using DEBUG Window, User can control Remote
    ' - R-RedLED, Y-YellowLED and G-GreenLED.
    ' - Address changed to a constant.
    ' - Accepts an acknowledgement value
    ' - Requires 802.15.4 XBee (Series 1)
    ' ******************************************************
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    ' ***************** Variables, Constants and Pins
    T9600 CON 84
    Green_button PIN 0
    Red_button PIN 1
    Yellow_button PIN 2
    Baud CON T9600 ' Set Baud rate
    Rx PIN 15 ' XBee DOUT
    Tx PIN 14 ' XBee DIN
    DataOut VAR Word ' Frequency to send
    DL_Addr CON $2 ' Destination address for data
    DataIn VAR Byte ' General variable for data

    ' ***************** Configure XBee in AT Command Mode
    PAUSE 500
    DEBUG CLS,"Configuring XBee..."
    PAUSE 3000 ' Guard time
    SEROUT Tx,Baud,["+++"] ' Command Mode Sequence
    PAUSE 2000 ' Guard time
    SEROUT Tx,Baud,["ATGT 3,MY 0",CR] ' Set low guard time and Base address
    SEROUT TX,Baud,["ATCN",CR] ' Exit Command Mode
    ' ***************** Main Loop***********************************

    GOSUB Config_XBee ' Set DL address of XBee
    DO
    IF IN0 = 1 THEN GOSUB Green_Push_Button ELSE GOSUB Green_Off
    IF IN1 = 1 THEN GOSUB Yellow_Push_Button ELSE GOSUB Yellow_Off
    IF IN2 = 1 THEN GOSUB Red_Push_Button ELSE GOSUB Red_Off
    LOOP
    '*******************Subroutines*********************************
    Green_Off:
    DataOut = 0
    SEROUT Tx,Baud,["G",CR,CR] ' Send G
    SEROUT Tx,Baud,[DEC DataOut,CR,CR] ' Send LED state
    GOSUB CheckAck ' Get Acknowledgement
    PAUSE 200
    RETURN

    Green_Push_Button:
    DataOut = 1
    SEROUT Tx,Baud,["G",CR,CR] ' Send G
    SEROUT Tx,Baud,[DEC DataOut,CR,CR] ' Send LED state
    GOSUB CheckAck ' Get Acknowledgement
    PAUSE 200
    RETURN

    Red_Off:
    DataOut = 0
    SEROUT Tx,Baud,["R",CR,CR] ' Send R
    SEROUT Tx,Baud,[DEC DataOut,CR,CR] ' Send LED state
    GOSUB CheckAck ' Get Acknowledgement
    PAUSE 200
    RETURN

    Red_Push_Button:
    DataOut = 1
    SEROUT Tx,Baud,["R",CR,CR] ' Send R
    SEROUT Tx,Baud,[DEC DataOut,CR,CR] ' Send LED state
    GOSUB CheckAck ' Get Acknowledgement
    RETURN

    Yellow_Off:
    DataOut = 0
    SEROUT Tx,Baud,["Y",CR,CR] ' Send Y
    SEROUT Tx,Baud,[DEC DataOut,CR,CR] ' Send LED state
    GOSUB CheckAck ' Get Acknowledgement
    PAUSE 200
    RETURN

    Yellow_Push_Button:
    DataOut = 1
    SEROUT Tx,Baud,["Y",CR,CR] ' Send Y
    SEROUT Tx,Baud,[DEC DataOut,CR,CR] ' Send LED state
    GOSUB CheckAck ' Get Acknowledgement
    RETURN

    Timeout:
    DEBUG "No data received",CR

    Config_XBee:
    ' Configure XBee for destination node address
    PAUSE 10 ' Short guard time
    SEROUT Tx,Baud,["+++"] ' Command Mode sequence
    PAUSE 10 ' Short guard time
    SEROUT TX,Baud,["ATDL ", HEX DL_Addr,CR] ' Set Destination Node Address
    SEROUT Tx,Baud,["ATCN",CR] ' Exit Command Mode
    RETURN

    CheckAck:
    SERIN Rx,Baud,1000,CheckTimeout,[DEC dataIn] ' Accept incoming byte
    IF dataIn = 1 THEN ' If 1, then ack'd
    DEBUG BELL,"OK - Ack Received!",CR
    ELSE ' If received, but not "1", problem
    DEBUG "Bad Ack!",CR
    ENDIF
    RETURN

    CheckTimeout:
    DEBUG "No ack received!",CR ' If nothing recieved
    RETURN





    ' ******************************************************
    ' Wall Light.bs2
    ' 14 Sept 2014
    ' This program accepts a character and values:
    ' - G & 0 or 1 to control state of LED
    ' - R & 0 or 1 to control state of LED
    ' - Y & 0 or 1 to control state of LED
    ' Return acknowledgements or value to Base
    ' Requires 802.15.4 XBee (Series 1)
    ' ******************************************************
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    '****************** Variable, Constants and Pins
    T9600 CON 84
    Baud CON T9600
    GLED PIN 7
    YLed PIN 6
    RLED PIN 5
    Rx PIN 15 ' XBee DOUT
    Tx PIN 14 ' XBee DIN
    RTS PIN 11 ' XBee RTS
    GState VAR Bit ' Received state of green LED
    RState VAR Bit ' Received state FOR red LED
    YState VAR Bit ' Received state for yellow LED
    DataIn VAR Byte ' General byte data
    My_Addr CON $2 ' Set address of node, modify as desired, $1-$FFFE
    ' ***************** Configure XBee to use RTS and set Address
    DEBUG CLS,"Configuring XBee...",CR
    PAUSE 3000 ' Guard time
    SEROUT Tx,Baud,["+++"] ' Command Mode Sequence
    PAUSE 2000 ' Guard time
    SEROUT Tx,Baud,["ATD6 1",CR] ' Enable RTS
    SEROUT Tx,Baud,["ATMY ", HEX My_Addr,CR] ' Set node address
    SEROUT Tx,Baud,["ATDL 0,CN",CR] ' Set destination address of Base
    ' & Exit Command Mode
    ' ***************** Main Loop
    DO
    GOSUB AcceptData
    GOSUB Control
    LOOP

    AcceptData:
    SERIN Rx\RTS,Baud,10,Timeout,[DataIn] ' Accept byte
    SELECT DataIn
    CASE "G" ' G to control Green LED
    SERIN Rx\RTS,Baud,1000,Timeout,[DEC GState] ' Accept Green LED state
    PAUSE 200 ' Give Base time to set up
    SEROUT Tx,Baud,[CR,DEC 1,CR] ' Return acknowledgment
    CASE "R" ' R to control Red LED state
    SERIN Rx\RTS,Baud,1000,Timeout,[DEC RState] ' Accept Red LED state
    PAUSE 200 ' Give Base time to set up
    SEROUT Tx,Baud,[CR,DEC 1,CR] ' Return acknowledgment
    CASE "Y" ' Y to control Yellow LED
    SERIN Rx\RTS,Baud,1000,Timeout,[DEC YState] ' Accept Yellow LED state
    PAUSE 200
    SEROUT Tx,Baud,[CR,DEC 1,CR] ' Return acknowledgment
    ENDSELECT
    Timeout:
    RETURN

    Control:
    IF GState = 1 THEN ' Control Green LED based on state
    HIGH GLED
    LOW YLed
    LOW RLed
    ENDIF

    IF YState = 1 THEN ' Control Yellow LED based on state
    HIGH YLed
    LOW GLed
    LOW RLed
    ENDIF

    IF RState = 1 THEN ' Control Red LED based on state
    HIGH RLed
    LOW GLed
    LOW YLed
    ENDIF

    'DEBUG CLS
    'DEBUG "GState = ", DEC GState,CR
    'DEBUG "YState = ", DEC YState,CR
    'DEBUG "RState = ", DEC RState

    RETURN
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2014-09-14 10:26
    DavidNyman wrote: »
    Thanks very much for the help.

    What help?
    You did it all, dude.

    I guess that you noticed that you needed to come up with a value for "DataOut" when the input was not 1.

    (That could be boiled way way down, but as long as you're dancing it's good.)
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-09-15 08:41
    I apologize David, I did not see your thread until this morning and by then you had it solved. It's great to see that you kept going and solved your issue. If you need help again, please don't hesitate to post. I do usually check the forums on the weekends, but this weekend I was unable to.
  • DavidNymanDavidNyman Posts: 52
    edited 2014-09-18 18:27
    Thanks everyone. Somehow posting the problem made me look at it more closely and eventually figure out an answer. A learning experience in itself!
    I've changed the program a little in that now the base unit latches the input from the three buttons. I added three LEDs to the base unit for indication as to which LED is illuminated on the remote. One challenge has been finding yellow LEDs that are somewhat bright. Even the so called super bright yellow don't come close to the red and green ones used in the project for brightness.

    I learned a little too late about a hand held controller that was briefly available on Kickstarter. Now that the finished "favor" project is delivered to the new owner I'd like to eventually turn the circuit and program into something like this:
    http://quantumri.com/Q4.html
    I've emailed this company several times to inquire about purchasing one of these controllers. I never get a response so now it's time to build my own.
Sign In or Register to comment.