Hello everybody
I'm pretty new to the Nano 33 BLE world.
I'm trying to communicate between 2 slaves Nano 33 BLE and 1 master Nano 33 BLE.
and almost every time (but not every time) when the second slave connects to the master , then the master is crashing and start blinking in a weird way (fast than slow than fast again).
did someone saw that behavior?
does it have a solution?
Thanks
yohai
my code:
master:
#include <ArduinoBLE.h>
#include <String>
//config
//0-timing 1 slave 1 master communication
//1-1 slave 1 master send to matlab
//2-2 slaves 1 master send to matlab
//3-2 slaves 1 master send to matlab 2 way communication
bool Debug = HIGH;
byte numSlaves = 3;
int WHAT=0;
//functions
void connecting(String, int);
void initChar(BLEDevice & , const char* , const char*, const char* , BLECharacteristic &, BLECharacteristic &, BLECharacteristic &);
void sendData(BLECharacteristic, BLECharacteristic,bool,int,int);
void sendMasterData(bool Debug);
uint16_t sync(BLECharacteristic ,BLECharacteristic ,bool ,int );
void setup() {
//setup serial
Serial.begin(115200);
// setup ble
BLE.begin();
//BLE.debug(Serial);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
}
void loop() {
int periphCount = BLE.peripheralCount();
BLEDevice p0 = BLE.peripheral(0);
BLECharacteristic valC0 , tssC0 , tsmC0;//s means slave m means master
switch (numSlaves) {
case 0:
{
if (periphCount < 1)
{
connecting("per1", 0);
BLE.stopScan();
}
//define charachtiristics
initChar( p0, "0101", "0102","0103", valC0, tssC0, tsmC0);
//while the slaves connected send data
while (p0.connected() )
{
uint32_t t0 = millis();
uint16_t ts, a;
valC0.readValue(a);
tssC0.readValue(ts);
Serial.print( "dt is ");
Serial.println(millis() - t0);
digitalWrite(LED_BUILTIN, HIGH);
}
break;
}
case 1:
{
if (periphCount < 1)
{
connecting("per1", 0);
BLE.stopScan();
}
//define charachtiristics
initChar( p0, "0101", "0102","0103", valC0, tssC0, tsmC0);
//while the slaves connected send data
while (p0.connected() )
{
uint32_t t0 = millis();
sendData(tssC0, valC0, Debug, 0,numSlaves);
Serial.print( "dt is ");
Serial.println(millis() - t0);
digitalWrite(LED_BUILTIN, HIGH);
}
break;
}
case 2:
{
if (periphCount < 2)
{
connecting("per1", 0);
connecting("per2", 1);
BLE.stopScan();
}
//define charachtiristics
initChar( p0, "0101", "0102","0103", valC0, tssC0, tsmC0);
BLEDevice p1 = BLE.peripheral(1);
BLECharacteristic valC1 , tsC1;
initChar( p0, "0101", "0102","0103", valC0, tssC0, tsmC0);
//while the slaves connected send data
while (p0.connected() && p1.connected())
{
uint32_t t0 = millis();
sendData(tssC0, valC0, Debug,numSlaves, 0);
sendData(tsC1, valC1, Debug,numSlaves, 1);
// Serial.println(BLE.peripheralCount());
// Serial.print( "dt is ");
// Serial.println(millis() - t0);
digitalWrite(LED_BUILTIN, HIGH);
}
break;
}
case 3:
{
if (periphCount < 2)
{
connecting("per1", 0);
connecting("per2", 1);
BLE.stopScan();
}
//define charachtiristics
initChar( p0, "0101", "0102","0103", valC0, tssC0, tsmC0);
BLEDevice p1 = BLE.peripheral(1);
BLECharacteristic valC1 , tssC1,tsmC1;
//delay(5000);
initChar( p1, "1101", "1102","1103", valC1, tssC1, tsmC1);
//while the slaves connected send data
while (p0.connected() && p1.connected())
{
uint32_t t0 = millis();
//exchanging time stamps with slave1
uint32_t dt0=sync(tssC0,tsmC0,Debug,0);
uint32_t dt1=sync(tssC1,tsmC1,Debug,1);
//sample data and time of saw tooth and send it to matlab
sendMasterData(Debug);
sendData(tssC0, valC0, Debug,numSlaves, 0);
sendData(tssC1, valC1, Debug,numSlaves, 1);
digitalWrite(LED_BUILTIN, HIGH);
}
break;
}
}
//connecting to the slaves
digitalWrite(LED_BUILTIN, LOW);
}
void connecting(String namee, int num)
{
//check if peripheral1 connected
BLEDevice p0 = BLE.peripheral(num);
//loop until peripheral1 is connected
BLE.scanForName(namee);
BLEDevice a0 = BLE.available();
while (!a0.connected())
{
if (Debug)
Serial.println("scaning...");
while (!a0)
{
a0 = BLE.available();
}
if (a0.connect())
{
if (Debug)
{
Serial.print("conected");
Serial.println(num + 1);
}
}
else
{
if (Debug)
{
Serial.print("failed to connect");
Serial.println(num + 1);
}
}
}
}
void initChar(BLEDevice &p0, const char* Char1name, const char* Char2name, const char* Char3name, BLECharacteristic &valC0, BLECharacteristic &tsC0, BLECharacteristic &tmC0)
{
p0.discoverAttributes();
valC0 = p0.characteristic(Char1name);
tsC0 = p0.characteristic(Char2name);
tmC0 = p0.characteristic(Char3name);
}
void sendData(BLECharacteristic tsC, BLECharacteristic valC, bool Debug, int numSlaves ,int slaveNum)
{
uint32_t tss,tse, a ,ts;
switch(numSlaves ){
case 0:
{
}
case 1:
{
tss=0;
valC.readValue(a);
tsC.readValue(ts);
tse=ts;
break;
}
case 2:
{
tss=millis();
valC.readValue(a);
//tsC.readValue(ts);
tse=millis();
break;
}
case 3:
{
tss=0;
valC.readValue(a);
//tsC.readValue(ts);
tse=ts;
tse=millis();
break;
}
}
if (Debug)
{
Serial.print ("hello from sendData ");
Serial.println(slaveNum);
Serial.print("a is ");
Serial.println(a);
Serial.print("dts is ");
Serial.println((tse-tss));
}
else
{
Serial.write(a);
Serial.write(a >> 8);
Serial.write(a >> 16);
Serial.write(a >> 24);
Serial.write(tse-tss);
Serial.write((tse-tss) >> 8);
Serial.write((tse-tss) >> 16);
Serial.write((tse-tss) >> 24);
}
}
void sendMasterData(bool Debug)
{
uint16_t val;
switch(WHAT)
{
case 0:
{
val=analogRead(A0);
}
case 1:
{
val=digitalRead(A0);
}
}
uint32_t ts=millis();
if (Debug)
{
Serial.println("hello from send2master");
Serial.print("a is ");
Serial.println(val);
Serial.print("ts is ");
Serial.println(ts);
Serial.println();
}
else
{
Serial.write(val);
Serial.write(val >> 8);
Serial.write(val >> 16);
Serial.write(val >> 24);
Serial.write(ts);
Serial.write((ts) >> 8);
Serial.write(ts >> 16);
Serial.write(ts >> 24);
}
}
uint16_t sync(BLECharacteristic tssC0,BLECharacteristic tsmC0,bool Debug,int slave)
{
//this function send tm1 in char tsmC0 and read ts2 in char tssC0 and than measure tm2
//and than print all of them
uint32_t ts2 ,tm2 ,tm1,dt;
//send to slave
tm1=millis();
tsmC0.writeValue(tm1);
//read from slave
tssC0.readValue(ts2);
tm2=millis();
dt=tm1/2-ts2+tm2/2;
if (Debug)
{
Serial.print("hello from sync");
Serial.println(slave);
Serial.print("tm1 is ");
Serial.println(tm1);
Serial.print("tm2 is ");
Serial.println(tm2);
Serial.print("ts2 is ");
Serial.println(ts2);
Serial.print("bye from sync");
Serial.println(slave);
}
else
{
Serial.write(dt);
Serial.write(dt >> 8);
Serial.write(dt >> 16);
Serial.write(dt >> 24);
}
}