Hey all,
I'm having an issue with my Bluetooth shield set up, where if I connect everything up to my BoeBot Shield + the Bluetooth Shield, the Arduino crashes/restarts. See below for more details.
Setup (on robot)
- Arduino Uno
- Boe-Bot Sheild (similar to: https://www.parallax.com/product/28832)
- With two 360* full rotation servo motors
- Seeedruino Bluetooth Shield (http://www.seeedstudio.com/wiki/Bluetooth_Shield) --> Set up to run as a master.
- HD44780 Character display (using a 74HC595 shift register to reduce the number of pins used)
- HC-SR04 Distance Sensor
(separate)
- Arduino Uno
- Seeedruino Bluetooth Shield --> Set up to run as a master
Issue / Behaviour:
- Arduino is powered on > the sketch goes into the setup function > the setup() calls a bluetoothsetup() function.
- The bluetooth fails to connect with it's paired shield.
- COMMENT: Everything appears to work up to the delay(), where halfway through the delay(), the device crashes (BEFORE it gets to the loop() function.
When I run the below sample program, everything works as expected. It is just when I try to use the Seeedruino Bluetooth in my custom code that it doesn't work. I bought a HC-06 Bluetooth device, and that works fine with everything connected (HC-06 connects to laptop's bluetooth).
Everything is connected correctly.
Can anyone help point me in the right direction here?
Custom Code - uploaded to robot
/*
* Written By: Cian Byrne 2015
*
* Sensors Used:
* - HC-SR04 Distance Sensor
HD47780 Character LCD Display(via 74HC595N74HC595N
* - Servo Motor
* - BoeBot Sheild
*/
#include <LiquidCrystal595.h> // include the library
#include <SoftwareSerial.h> //Software Serial Port
#include <Servo.h>
/*
* define global pins:
*/
#define trigPin 9
#define echoPin 6
#define servoPin 11
#define RxD 7
#define TxD 6
#define DEBUG_ENABLED 1
/*
* Variables
*/
int pos = 0; //variable to store the servo position
long duration, distance; //variables to store the SR04 values
String slaveAddr = "0,6A,8E,16,C4,FD"; //MAC address of other BT sheild.
int distances[180]; //Array to dump values from distance sensor
byte dataPacketA[180]; //Array to dump values from distance sensor
SoftwareSerial blueToothSerial(RxD,TxD);
Servo ultraServo; // create servo object to control a servo
LiquidCrystal595 lcd(3,4,5); // datapin, latchpin, clockpin
/*
* Set up function
*/
void setup() {
/*
* Serial set up (hardware and bluetooth
*/
Serial.begin(9600);
pinMode(RxD, INPUT);
pinMode(TxD, OUTPUT);
setupBlueToothConnection();
//wait 1s and flush the serial and btooth buffers
delay(1000);
Serial.flush();
blueToothSerial.flush();
/*
* Servo and LCD Display
*/
ultraServo.attach(servoPin); // attaches the servo on pin 9 to the servo object
ultraServo.write(90); //set servo to middle position
lcd.begin(16,2); // 16 characters, 2 rows
lcd.clear();
lcd.setCursor(0,0);
/*
* SR HC-06 - Distance Sensor
*/
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// REMOVED CODE LOGIC
// send data over serial to PC:
blueToothSerial.write(dataPacketA,180);
Serial.write(dataPacketA,180);
}
// CODE REMOVED
/*
* This is for the Seeedruino Bluetooth device
*/
void setupBlueToothConnection()
{
// Set BluetoothBee BaudRate to default baud rate 38400
blueToothSerial.begin(38400);
// set the bluetooth work in master mode
blueToothSerial.print("\r\n+STWMOD=1\r\n");
// set the bluetooth name (irrelevant)
blueToothSerial.print("\r\n+STNA=Master20\r\n");
// Auto-connection is forbidden here
blueToothSerial.print("\r\n+STAUTO=0\r\n");
// This delay is required.
delay(2000);
blueToothSerial.flush();
// This delay is required.
delay(2000);
//form the full connection command
Serial.print("Connecting to slave: ");
Serial.println(slaveAddr);
//send connection command
blueToothSerial.print("\r\n+CONN=" + slaveAddr + "\r\n");
delay(5000);
}
/*
* This is for the HC-06 Bluetooth
*/
void setupBluetoothConn(){
//Set BluetoothBee BaudRate to default baud rate 9600
blueToothSerial.begin(9600);
//delay
delay(2000);
}
Sample Code (that works!) - uploaded to receiving Arduino connected to USB serial port
#include <SoftwareSerial.h> // Software Serial Port
#define RxD 7
#define TxD 6
#define DEBUG_ENABLED 1
// Make sure you modify this address to the one in your Slave Device!!
String slaveAddr = "0,6A,8E,16,C4,FD";
SoftwareSerial blueToothSerial(RxD,TxD);
void setup()
{
Serial.begin(9600);
pinMode(RxD, INPUT);
pinMode(TxD, OUTPUT);
setupBlueToothConnection();
//wait 1s and flush the serial and btooth buffers
delay(1000);
Serial.flush();
blueToothSerial.flush();
}
void loop()
{
char recvChar;
// Infinite loop
while(1)
{
// If there is data pending to be read from the shield
if(blueToothSerial.available())
{
recvChar = blueToothSerial.read();
// Print the data through the Serial channel
Serial.print("Master Received: ");
Serial.print(recvChar);
Serial.print("\n");
}
// If there is data pending to be read from the serial port
if(Serial.available())
{
recvChar = Serial.read();
// Send the data through btooth
blueToothSerial.print(recvChar);
Serial.print("Master Transmitted: ");
Serial.print(recvChar);
Serial.print("\n");
}
}
}
// Function to start the connection. Make sure slaveAddr is set to the right
// value.
void setupBlueToothConnection()
{
// Set BluetoothBee BaudRate to default baud rate 38400
blueToothSerial.begin(38400);
// set the bluetooth work in master mode
blueToothSerial.print("\r\n+STWMOD=1\r\n");
// set the bluetooth name (irrelevant)
blueToothSerial.print("\r\n+STNA=Master20\r\n");
// Auto-connection is forbidden here
blueToothSerial.print("\r\n+STAUTO=0\r\n");
// This delay is required.
delay(2000);
blueToothSerial.flush();
// This delay is required.
delay(2000);
//form the full connection command
Serial.print("Connecting to slave: ");
Serial.println(slaveAddr);
//send connection command
blueToothSerial.print("\r\n+CONN=" + slaveAddr + "\r\n");
delay(5000);
}