The code of mine does not work, while multiple BlueSMIRF Bluetooth modules are connected to one Arduino. As seen in my code, I just used the reference code, found on your webpage: https://learn.sparkfun.com/tutorials/us … esmirf/all
However, as the setup in the code starts up the three modules, it does not run the rest of the code. Cannot find more information about the code, and help is much appreciated.
Hope someone can help me,
Sebastian.
Hello Sebastianx , and thanks for posting on the forums!
Unfortunately nobody can see your code when it’s included in a picture.
Please paste your code into a post, select all the code and then click the code display button in the toolbar above so that your code is visible.
This will enable other users to see what’s in your code and offer advice.
Oh, mb. Here’s the code.
#include <SoftwareSerial.h>
// Bluetooth
const int btTx = 2;
const int btRx = 3;
const int bt1Tx = 4;
const int bt1Rx = 5;
const int btMTx = 6;
const int btMRx = 7;
SoftwareSerial bluetooth(btTx, btRx);
SoftwareSerial bluetoothM(btMTx, btMRx);
SoftwareSerial bluetooth1(bt1Tx, bt1Rx);
// Variabler
int dataReceived = 0;
int fDoor_state = 0;
int bDoor_state = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
bluetooth.begin(115200); // The Bluetooth Mate defaults to 115200bps
bluetooth.print("$"); // Print three times individually
bluetooth.print("$");
bluetooth.print("$"); // Enter command mode
delay(100); // Short delay, wait for the Mate to send back CMD
bluetooth.println("U,9600,N"); // Temporarily Change the baudrate to 9600, no parity
// 115200 can be too fast at times for NewSoftSerial to relay the data reliably
bluetooth.begin(9600); // Start bluetooth serial at 9600
bluetoothM.begin(115200); // The Bluetooth Mate defaults to 115200bps
bluetoothM.print("$"); // Print three times individually
bluetoothM.print("$");
bluetoothM.print("$"); // Enter command mode
delay(100); // Short delay, wait for the Mate to send back CMD
bluetoothM.println("U,9600,N"); // Temporarily Change the baudrate to 9600, no parity
// 115200 can be too fast at times for NewSoftSerial to relay the data reliably
bluetoothM.begin(9600); // Start bluetooth serial at 9600
bluetooth1.begin(115200); // The Bluetooth Mate defaults to 115200bps
bluetooth1.print("$"); // Print three times individually
bluetooth1.print("$");
bluetooth1.print("$"); // Enter command mode
delay(100); // Short delay, wait for the Mate to send back CMD
bluetooth1.println("U,9600,N"); // Temporarily Change the baudrate to 9600, no parity
// 115200 can be too fast at times for NewSoftSerial to relay the data reliably
bluetooth1.begin(9600); // Start bluetooth serial at 9600
}
void loop() {
/*if (bluetooth.available()){*/
dataReceived = (char)bluetooth.read();
/*}
else if (bluetoothM.available()){
dataReceived = (char)bluetoothM.read();
}
else if (bluetooth1.available()){
dataReceived = (char)bluetooth1.read();
}*/
///////////////// DØRE /////////////////
if (dataReceived == '1'){
if (fDoor_state == 0) { // If door open
Serial.print("Opening");
bluetooth.print('o'); // open
fDoor_state = 1;
}else{
Serial.print("Closing");
bluetooth.print('c'); // lock
fDoor_state = 0;
}
}
if (dataReceived == '2'){
if (bDoor_state == 0) {
Serial.print("Opening");
bluetooth1.print('o');
bDoor_state = 1;
}else{
Serial.print("Closing");
bluetooth1.print('c');
bDoor_state = 0;
}
}
///////////////// MAGNETER /////////////////
if (dataReceived == 'a'){ // Magnet 1 (state a)
bluetoothM.print("f0");
}
if (dataReceived == 'b'){ // Magnet 1 (state b)
bluetoothM.print("f1");
}
if (dataReceived == 'd'){ // Magnet 2 (state a)
bluetoothM.print("b0");
}
if (dataReceived == 'e'){ // Magnet 2 (state b)
bluetoothM.print("b1");
}
dataReceived = 0;
}
Update: I got my code to run but still not the Bluetooth modules to respond with multiple setups.
OK, I think your issue is probably that the Mega can’t to software serial RX on the pins you’re using. From Arduino’s documentation:
Not all pins on the Mega and Mega 2560 support change interrupts, so only the following can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69).
You’re either going to need to move your RX pins and change your code to match the pins you’ve moved too, or since the Mega has three free hardware UARTs, switch your code to those and connect the Bluetooth modules to pins 14-19. The hardware UARTs are actually the preferred method since they don’t require any libraries or processor overhead to use.
Serial1 is on pins 19 & 18,
Serial2 is on pins 17 & 16,
Serial3 is on pins 15 & 14.