Stap 3: Arduino met Bluetooth
De volgende code op de Arduino laden om te zetten van je Arduino als de bluetooth-slave:/* Bluetooth Setup on Arduino by Nicole Grimwood Based upon: Seeed Wiki Bluetooth slave code http://www.seeedstudio.com/wiki/index.php?title=Bluetooth_Shield This code is in the public domain. */ #include <SoftwareSerial.h> // Software Serial Port #define RxD 6 // This is the pin that the Bluetooth (BT_TX) will transmit to the Arduino (RxD) #define TxD 7 // This is the pin that the Bluetooth (BT_RX) will receive from the Arduino (TxD) #define DEBUG_ENABLED 1 SoftwareSerial blueToothSerial(RxD,TxD); #define DATA_1 (PORTC |= 0X01) // DATA 1 #define DATA_0 (PORTC &= 0XFE) // DATA 0 #define STRIP_PINOUT (DDRC=0xFF) void setup() { pinMode(RxD, INPUT); // Setup the Arduino to receive INPUT from the bluetooth shield on Digital Pin 6 pinMode(TxD, OUTPUT); // Setup the Arduino to send data (OUTPUT) to the bluetooth shield on Digital Pin 7 setupBlueToothConnection(); // Used to initialize the Bluetooth shield } void loop() { char recvChar; while(1){ if(blueToothSerial.available()){ // check if there's any data sent from the remote bluetooth recvChar = blueToothSerial.read(); Serial.print(recvChar); // Print the character received to the Serial Monitor } } } // The following code is necessary to setup the bluetooth shield void setupBlueToothConnection(){ blueToothSerial.begin(38400); //Set BluetoothBee BaudRate to default baud rate 38400 blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode blueToothSerial.print("\r\n+STNA=SeeedBTSlave\r\n"); //set the bluetooth name as "SeeedBTSlave" blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here delay(2000); // This delay is required. blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable Serial.println("The slave bluetooth is inquirable!"); delay(2000); // This delay is required. blueToothSerial.flush(); }
Als de bluetooth goed werkt, knippert een rode en een groene LED na elkaar in de juiste volgorde op het schild. Als dit gebeurt een paar seconden nadat u de code op de Arduino, probeer macht fietsen de Arduino en herladen van de code laadt niet gestart.