RF Multiple Button Help!
Dan Taylor
Posts: 207
Hi:
Could somebody please show me an example code for sending the output of more than one, preferably 4, switches from a transmitter to a receiver through RF. Each switch when pressed would then lead to a different subroutine. I am kinda new to the RF Transmitter and Receiver sold by Parallax so I could use some help.
Thanks for any help in advance!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Dan Taylor
Could somebody please show me an example code for sending the output of more than one, preferably 4, switches from a transmitter to a receiver through RF. Each switch when pressed would then lead to a different subroutine. I am kinda new to the RF Transmitter and Receiver sold by Parallax so I could use some help.
Thanks for any help in advance!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Dan Taylor
Comments
'Sending side
data VAR nib
data = INA
Send data
'Receiving Side
Receive Data
IF data%2 == 1 'pressed
'Do something
IF (data>>1) == 1 'pressed
'Do something
'Repeat 2 more times
I would recommend revisiting the product page for the Parallax 433 MHz RF Transceiver Package and review the sample code under the "Downloads" heading. This will get you familiar with how these modules communicate.
Basically, you can run a DO...LOOP in the transmitter program to recursively check the state of each switch and then send that state (0 or 1) to the receiver module. The program for the receiver module will then check the received value from each switch and execute a subroutine based on the switches' state.
Sample Transmitter:
DO
SEROUT TxPin, BaudRate, [noparse][[/noparse]"UUUU", SW1, SW2, SW3, SW4]
LOOP
NOTE: SW1, SW2, SW3, SW4 should be tied to IN# to read the switches' state.
Sample Receiver:
SERIN RxPin, BaudRate, [noparse][[/noparse]WAIT("UUUU"), SW1, SW2, SW3, SW4]
IF (SW1 = 1) THEN
GOSUB First_Subroutine
ELSEIF (SW2 = 1) THEN ...
Happy Developing!
Jessica
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jessica Uelmen
Education Department
Parallax, Inc.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Dan Taylor
' RF_Multi_Bot_Receiver.BS2
SW0···· VAR·· Nib
SW1···· VAR·· Nib
SW2···· VAR·· Nib
SW3···· VAR·· Nib
SW4···· VAR·· Nib
Main:
·· SERIN Receiver, 16468, [noparse][[/noparse]WAIT("UUUU"), SW0, SW1, SW2, SW3, SW4]
·····IF (SW1 = 0) THEN
···· GOSUB Mode_1
···ELSEIF (SW2 = 0) THEN
···· GOSUB Mode_2
···ELSEIF (SW3 = 0) THEN
···· GOSUB Mode_3
·· ELSEIF (SW4 = 0) THEN
···· GOSUB Mode_4
·· ENDIF
·· GOTO Main
And Transmitteer Code...
' RF_Multi_Bot_Transmitter.bs2
Main:
··· SEROUT Transmitter, 16468, [noparse][[/noparse]"UUUU", IN0, IN1, IN2, IN3, IN4]
··· IF (IN1 = 0) THEN
······GOSUB Mode_1
··· ELSEIF (IN2 = 0) THEN
·······GOSUB Mode_2
··· ELSEIF (IN3 = 0) THEN
·······GOSUB Mode_3
··· ELSEIF (IN4 = 0) THEN
·······GOSUB Mode_4
··· ENDIF
· GOTO Main
I am trying to make it so that when SW1 is pressed it will go to the mini project, "Tilt Radio Controller for your Boe-Bot" (http://forums.parallax.com/showthread.php?p=524063). The code for that is under the Subroutine Mode_1. But the receiver never receives the signal for some reason and will never goto Mode_1. Please Help!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Dan Taylor
For help using the SEROUT, please see page 415 in the BASIC Stamp Syntax and Reference Manual. Essentially, your transmitter program can only send variables, constants, expressions, or formatters that tells SEROUT how to format outgoing data. Instead of using the command:
SEROUT Transmitter, 16468, [noparse][[/noparse]"UUUU", IN0, IN1, IN2, IN3, IN4]
Declare them as variables first:
SW0 = IN0
SW1 = IN1
...
SEROUT Transmitter, 16468, [noparse][[/noparse]"UUUU", SW1, SW2 ... etc.]
Also be sure Transmitter is declared as "Transmitter PIN ##" for whatever pin the RF Transmitter module is connected to. Putting the Main routine in your transmitter code in a DO...LOOP will also be beneficial, as right now you are only transmitting the states of the pins once.
Happy Developing!
Jessica
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jessica Uelmen
Education Department
Parallax, Inc.
' RF_Multi_Bot_Transmitter.bs2
Speaker·········· PIN· 4······ ' Piezo Speaker PIN
Transmitter····· PIN· 10
SW0······· VAR·· Nib
SW1······· VAR·· Nib
SW2······· VAR·· Nib
SW3······· VAR·· Nib
SW4······· VAR·· Nib
SW0 = IN0
SW1 = IN1
SW2 = IN2
SW3 = IN3
SW4 = IN4
FREQOUT Speaker, 2000, 3000······ ' Emit Sound from speaker to anounce beginning of program
PULSOUT Transmitter, 1200
Main_Routine:
· DEBUG "Main Routine Before Main...", CR
· DO
··· SW0 = IN0
··· SW1 = IN1
··· SW2 = IN2
··· SW3 = IN3
··· SW4 = IN4
····IF (IN1 = 0) THEN
····· GOSUB Mode_1
··· ELSEIF (IN2 = 0) THEN
······ GOSUB Mode_2
··· ELSEIF (IN3 = 0) THEN
······ GOSUB Mode_3
··· ELSEIF (IN4 = 0) THEN
······ GOSUB Mode_4
··· ENDIF
· LOOP
And the Receiver code...
' RF_Multi_Bot_Receiver.BS2
Speaker········· PIN· 4······ ' Piezo Speaker PIN
Receiver········ PIN· 11····· ' Receiver DATA PIN
SW0···· VAR·· Nib
SW1···· VAR·· Nib
SW2···· VAR·· Nib
SW3···· VAR·· Nib
SW4···· VAR·· Nib
FREQOUT speaker, 2000, 3000······ ' Emit Sound from speaker to anounce beginning of program
Main_Routine:
· DEBUG "Running Main Routine...", CR
· DO
··· SERIN Receiver, 16468, [noparse][[/noparse]WAIT("UUUU"), SW0, SW1, SW2, SW3, SW4]
··· IF (SW1 = 0) THEN: GOSUB Mode_1
··· ELSEIF (SW2 = 0) THEN:GOSUB Mode_2
··· ELSEIF (SW3 = 0) THEN:GOSUB Mode_3
··· ELSEIF (SW4 = 0) THEN:GOSUB Mode_4
··· ENDIF
· LOOP
So is there any problems with the receiver code? Thanks!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Dan Taylor
The Parallax Project of the Week this week is a Wireless Musical Keyboard. It contains a brief explanation on how RF communication works, as well as sample code for pushing pushbuttons on one board, which plays a tone on another. It's coded a bit simpler than the way I steered you here, and it should help you debug your problem.
Happy Developing!
Jessica
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jessica Uelmen
Education Department
Parallax, Inc.
On the receiver side, try putting a debug statement before the SERIN and a statement right after the SERIN. This will tell you if the code gets hung up on the SERIN statement, or if none of the if statements ever evaluate as true.
Here is the Transmitter code...
' RF_Multi_Bot_Transmitter.bs2
Speaker PIN 4 ' Piezo Speaker PIN
Transmitter PIN 10
SW0 VAR Nib
SW1 VAR Nib
SW2 VAR Nib
SW3 VAR Nib
SW4 VAR Nib
SW0 = IN0
SW1 = IN1
SW2 = IN2
SW3 = IN3
SW4 = IN4
FREQOUT Speaker, 2000, 3000 ' Emit Sound from speaker to anounce beginning of program
PULSOUT Transmitter, 1200
Main_Routine:
DEBUG "Main Routine Before Main...", CR
DO
SW0 = IN0
SW1 = IN1
SW2 = IN2
SW3 = IN3
SW4 = IN4
SEROUT Transmitter, 16468, [noparse][[/noparse]"UUUU", SW0, SW1, SW2, SW3]
IF (IN1 = 0) THEN
GOSUB Mode_1
ELSEIF (IN2 = 0) THEN
GOSUB Mode_2
ELSEIF (IN3 = 0) THEN
GOSUB Mode_3
ELSEIF (IN4 = 0) THEN
GOSUB Mode_4
ENDIF
LOOP
OK Thanks SRLM I'll try that. I didn't even think of that.
Thanks Jessica! I that program is very helpful! I think I have it figured out now.
Thank you all for your help!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Dan Taylor
Add a Pulsin command to the receiver code that corresponds to the Pulsout signal that you are transmitting and you should start receiving data.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Craig Eid
www.TriadRD.com
PULSIN Receiver, 1, number
Why would you need the variable·number? Wouldn't it be a waste of room?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Dan Taylor
Post Edited (Dan Taylor) : 2/15/2009 11:13:28 PM GMT
You should not need a PULSIN command on the receiver side. By calling the PULSOUT command in the transmitter program, you're simply allowing the receiver time to synchronize with the transmitter. PULSIN is used to measure the width of a pulse and is not necessary to synch the two RF modules. (See the Parallax 433 MHz RF Transceiver Package product page for documentation and sample code demonstrating this concept.)
If you're still having problems communicating, try transmitting the state of one pushbutton wirelessly. You can setup the Debug Terminal to display what's received using the command DEBUG ? SW0 to verify you're receiving the right information. Then add the next pushbutton and if that works, add the next, etc. This way you can easily pinpoint the error in your code.
You can also hook up an LED to your circuit if you want to be sure you're receiving data without displaying it in the Debug Terminal, using the following code:
LOW LedPin
SEROUT RxPin, Baud, [noparse][[/noparse]Data]
HIGH LedPin
This way, the LED will blink when data is received. (Note: Be sure to use a 470ohm resistor connected in series to limit the current so you don't damage the I/O pin.)
Hope this helps.
Jessica
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jessica Uelmen
Education Department
Parallax, Inc.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Dan Taylor