help with stamp code for bluetooth
vw_gti
Posts: 10
i need it so when a bluetooth device is conected i need to have a output for a second thend when it disconects it would use output 2 for a secound
if anyone can help
thanks
if anyone can help
thanks
Comments
The EB500 and Easy-Bluetooth adapters have a mode where they will automatically connect to a paired Bluetooth device and look like an ordinary full duplex serial connection to the Stamp and PC. There's normally no indication to the Stamp that a connection has been made other than that the Easy-Bluetooth will begin to deliver serial characters to the Stamp from the PC (and vice versa). There are commands that the Stamp can send to the Easy-Bluetooth adapter that are in the documentation that will return connection status, but that's more complicated and not something to do as a "first project".
http://forums.parallax.com/showthread.php?p=806358
You'll have to learn how to program this yourself. It would be cheating for one of us to write a program for you, but people will be willing to give you advice and to help you to understand things.
Download the documentation for the EB500. Go to Parallax's webstore and search for 'EB500'. On the webpage for the product, you'll see a list of links for documention and sample programs. Even though the Easy-Bluetooth is different from the EB500, the same ideas apply and I think the EB500 documentation is easier to understand.
It would be helpful if you would describe what you have available in terms of equipment.
Page 10 talks about the pins on the EB500 and what they're for. In particular, pin 8 (Stamp I/O pin 5) tells you whether or not there's a connection.
' {$STAMP BS2}
' {$PBASIC 2.5}
{$STAMP BS2}
'
[noparse][[/noparse] I/O Definitions ]
lock PIN 8 ' Lock output
unlock PIN 9 ' unlock output
'
[noparse][[/noparse] Constants ]
Pdelay CON 500 ' delay 500 milliseconds
'Wait for the eb500 radio to be ready
PAUSE 1000
'Connect to the remote device
SEROUT 1,84,[noparse][[/noparse]“CON 00:24:36:6C:F4:E3”,CR]
SERIN 0,84,[noparse][[/noparse]WAIT(“ACK”,CR)]
'Wait for the connection to be established and switch to data mode
WaitForConnection:
IF IN5 = 0 THEN WaitForConnection
IF IN5 = 1 THEN HIGH unlock
PAUSE Pdelay
LOW unlock
THEN IF IN5 =0 THEN HIGH lock
PAUSE pdelay
LOW unlock
GOTO Main
When you're done, post it again.
' {$STAMP BS2}
' {$PBASIC 2.5}
'
[noparse][[/noparse] I/O Definitions ]
lock PIN 12 ' Lock output
unlock PIN 15 ' unlock output
RX CON 2 'Receive Pin
TX CON 0 'Transmit Pin
Baud CON 84 '9600 Baud
'
[noparse][[/noparse] Constants ]
Pdelay CON 500 ' delay 500 milliseconds
main:
'Wait for the eb500 radio to be ready
PAUSE 1000
'Connect to the remote device
SEROUT 1,84,[noparse][[/noparse]"CON 00:17:A0:01:57:F7",CR]
SERIN 0,84,[noparse][[/noparse]WAIT("ACK",CR)]
WaitForConnection:
IF IN5 = 0 THEN WaitForConnection
IF IN5 = 1 THEN HIGH unlock
PAUSE Pdelay
LOW unlock
IF IN5 =0 THEN HIGH lock
PAUSE pdelay
LOW unlock
GOTO Main
Some comments ...
IF IN5 = 1 THEN HIGH unlock
You don't need the IF IN5 = 1 THEN at this point in your program since IN5 has to be 1. The previous IF statement guarantees it
After the 1st LOW statement, you need to wait for the connection to go away. Do the same thing that you did starting at WaitForConnection, just do the opposite ... WaitForNoConnection.
Notice that your last LOW is for unlock, not lock.
The GOTO Main doesn't have to go back to the beginning of the program. The SEROUT and SERIN only has to be done once at the beginning.
This is what is known as a State Machine. The program spends part of its time in one section, then, when something happens, it switches to another section of the program where it sits until something else happens. Then it goes back to the first section. Each section provides a "state" and the "something happens" is the transition condition from one state to another. You can have very complex State Machines with lots of states and all sorts of transitions from one state to another. Do a web search for "Wiki State Machine" for an explanation and examples.