firedancer

Arduino Bluetooth LED blink

Jun 27th, 2013
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.08 KB | None | 0 0
  1. /*
  2. Personal note:
  3. This is a modified version of the BluetoothShield slave demo from SeeedStudio. I've made changes to enable the LED blink.
  4. */
  5.  
  6. /*
  7. BluetoothShield Demo Code Slave.pde. This sketch could be used with
  8. Master.pde to establish connection between two Arduino. It can also
  9. be used for one slave bluetooth connected by the device(PC/Smart Phone)
  10. with bluetooth function.
  11. 2011 Copyright (c) Seeed Technology Inc.  All right reserved.
  12.  
  13. Author: Steve Chang
  14.  
  15. This demo code is free software; you can redistribute it and/or
  16. modify it under the terms of the GNU Lesser General Public
  17. License as published by the Free Software Foundation; either
  18. version 2.1 of the License, or (at your option) any later version.
  19.  
  20. This library is distributed in the hope that it will be useful,
  21. but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  23. Lesser General Public License for more details.
  24.  
  25. You should have received a copy of the GNU Lesser General Public
  26. License along with this library; if not, write to the Free Software
  27. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  28.  
  29. For more details about the product please check http://www.seeedstudio.com/depot/
  30.  
  31. */
  32.  
  33.  
  34. /* Upload this sketch into Seeeduino and press reset*/
  35.  
  36. #include <SoftwareSerial.h>   //Software Serial Port
  37. #define RxD 6
  38. #define TxD 7
  39. #define Led 13
  40.  
  41. #define DEBUG_ENABLED  1
  42.  
  43. SoftwareSerial blueToothSerial(RxD,TxD);
  44.  
  45. void setup()
  46. {
  47.   Serial.begin(9600);
  48.   pinMode(RxD, INPUT);
  49.   pinMode(TxD, OUTPUT);
  50.   pinMode(Led, OUTPUT);
  51.   setupBlueToothConnection();
  52. }
  53.  
  54. void loop()
  55. {
  56.   char recvChar;
  57.   while(1){
  58.     if(blueToothSerial.available()){//check if there's any data sent from the remote bluetooth shield
  59.       int i = blueToothSerial.parseInt();
  60.       if (i == 1) {
  61.         Serial.println("Turn LED on");
  62.         digitalWrite(Led, HIGH);
  63.       } else {
  64.         Serial.println("Turn LED off");
  65.         digitalWrite(Led, LOW);
  66.       }
  67.       Serial.print(i);
  68.  //     recvChar = blueToothSerial.read();
  69.    //   Serial.print(recvChar);
  70.     }
  71.     if(Serial.available()){//check if there's any data sent from the local serial terminal, you can add the other applications here
  72.       recvChar  = Serial.read();
  73.       blueToothSerial.print(recvChar);
  74.     }
  75.   }
  76. }
  77.  
  78. void setupBlueToothConnection()
  79. {
  80.   blueToothSerial.begin(38400); //Set BluetoothBee BaudRate to default baud rate 38400
  81.   blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
  82.   blueToothSerial.print("\r\n+STNA=SeeedBTSlave\r\n"); //set the bluetooth name as "SeeedBTSlave"
  83.   blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
  84.   blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here
  85.   delay(2000); // This delay is required.
  86.   blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable
  87.   Serial.println("The slave bluetooth is inquirable!");
  88.   delay(2000); // This delay is required.
  89.   blueToothSerial.flush();
  90. }
Advertisement
Add Comment
Please, Sign In to add comment