Hello,
I am trying to use the Arduino BLE Sense for my project, and while it is connected via Bluetooth, it can't handle a simple switch case with multiple analogwrites, and it becomes unresponsive. But it can just write multiple analogwrites, while it is not connected to Bluetooth. (EDIT: it can't write more than 4 in any case no matter connected or not connected to Bluetooth, 4 is maximum)
In the code below, it breaks after running analogWrite(M4, 255);
I have used two arduino ble 33 senses and both became responsive during writing multiple PWMs.
I can't understand what caused it to crash. The same code works on the Arduino IOT without issues.
#include <ArduinoBLE.h>
BLEService ledService("180A"); // BLE LED Service
// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("2A57", BLERead | BLEWrite);
int M1 = 5;
int M2 = 6;
int M3 = 9;
int M4 = 17;
int M5 = 4;
int M6 = 7;
int M7 = 3;
int M8 = 2;
int M9 = 8;
int M10 = 10;
int M11 = 16;
int M12 = 14;
void setup() {
Serial.begin(9600);
//while (!Serial);
// set LED's pin to output mode
pinMode(LEDR, OUTPUT);
pinMode(LEDG, OUTPUT);
pinMode(LEDB, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(M1, OUTPUT);
pinMode(M2, OUTPUT);
pinMode(M3, OUTPUT);
pinMode(M4, OUTPUT);
pinMode(M5, OUTPUT);
pinMode(M6, OUTPUT);
pinMode(M7, OUTPUT);
pinMode(M8, OUTPUT);
pinMode(M9, OUTPUT);
pinMode(M10, OUTPUT);
pinMode(M11, OUTPUT);
pinMode(M12, OUTPUT);
digitalWrite(LED_BUILTIN, LOW); // when the central disconnects, turn off the LED
digitalWrite(LEDR, HIGH); // will turn the LED off
digitalWrite(LEDG, HIGH); // will turn the LED off
digitalWrite(LEDB, HIGH); // will turn the LED off
// begin initialization
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy failed!");
while (1);
}
// set advertised local name and service UUID:
BLE.setLocalName("Nano 33 BLE Sense");
BLE.setAdvertisedService(ledService);
// add the characteristic to the service
ledService.addCharacteristic(switchCharacteristic);
// add service
BLE.addService(ledService);
// set the initial value for the characteristic:
switchCharacteristic.writeValue(0);
// start advertising
BLE.advertise();
// Serial.println("BLE LED Peripheral");
}
void loop() {
// listen for Bluetooth® Low Energy peripherals to connect:
BLEDevice central = BLE.central();
// if a central is connected to peripheral:
if (central) {
Serial.print("Connected to central: ");
// print the central's MAC address:
Serial.println(central.address());
digitalWrite(LED_BUILTIN, HIGH); // turn on the LED to indicate the connection
// while the central is still connected to peripheral:
while (central.connected()) {
// if the remote device wrote to the characteristic,
// use the value to control the LED:
if (switchCharacteristic.written()) {
switch (switchCharacteristic.value()) { // any value other than 0
case 1:
Serial.println("All on");
analogWrite(M1, 255);
analogWrite(M2, 255);
analogWrite(M3, 255);
analogWrite(M4, 255);
analogWrite(M5, 255);
analogWrite(M6, 255);
analogWrite(M7, 255);
analogWrite(M8, 255);
analogWrite(M9, 255);
analogWrite(M10, 255);
analogWrite(M11, 255);
analogWrite(M12, 255);
digitalWrite(LEDR, LOW); // will turn the LED off
digitalWrite(LEDG, HIGH); // will turn the LED off
digitalWrite(LEDB, LOW); // will turn the LED on
delay (320);
analogWrite(M1, 0);
analogWrite(M2, 0);
analogWrite(M3, 0);
analogWrite(M4, 0);
analogWrite(M5, 0);
analogWrite(M6, 0);
analogWrite(M7, 0);
analogWrite(M8, 0);
analogWrite(M9, 0);
analogWrite(M10, 0);
analogWrite(M11, 0);
analogWrite(M12, 0);
digitalWrite(LEDR, HIGH); // will turn the LED off
digitalWrite(LEDG, HIGH); // will turn the LED off
digitalWrite(LEDB, HIGH); // will turn the LED off
break;
default:
Serial.println(F("LEDs off"));
digitalWrite(LEDR, HIGH); // will turn the LED off
digitalWrite(LEDG, HIGH); // will turn the LED off
digitalWrite(LEDB, HIGH); // will turn the LED off
break;
}
}
}
// when the central disconnects, print it out:
//Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
digitalWrite(LED_BUILTIN, LOW); // when the central disconnects, turn off the LED
digitalWrite(LEDR, HIGH); // will turn the LED off
digitalWrite(LEDG, HIGH); // will turn the LED off
digitalWrite(LEDB, HIGH); // will turn the LED off
}
}