Hello everyone
I am trying to get an app running that should turn on an LED on Pin 4 of my Arduino 33 Nano BLE and then receive data when the chip got moved (when IMU detects perturbation it turns off the LED and should change a characteristic - customTouchedChar to 1). The final goal is to have a "reaction trainer", where the app will be connected to up to 6 Arduinos and will turn on the next LED as soon as the previous one was turned off.
So far I managed to program a button that will turn the first LED on and it also turns it off as soon as I move the chip. However, even though the characteristic "customTouchedChar" is changing (I tested this by printing it into the Serial port and by double-checking with nRF connect app) my app is not receiving any data (text of Label 1 is not changing at all - not even to sth received). Can anyone explain to me what I am doing wrong?
Here are the blocks I am using:
And the sketch:
// including the BLE and the sensor libraries
#include <ArduinoBLE.h>
#include <Arduino_LSM9DS1.h>
int ledPin = 4; // Pin to whichi the LED is connected
int time_delay = 200; // time delay between checking of touched property - might be changed to get better sensitivity
float x, y, z; // parameters for sensor output
float delta = 4; // senistivity of movement
// define the service and the characteristics
BLEService customService("19b10000-e8f2-537e-4f6c-d104768a1214"); // service
BLEUnsignedIntCharacteristic customTurnONChar("19b10001-e8f2-537e-4f6c-d104768a1214", BLERead | BLENotify| BLEWrite); // Phone input characteristic to turn on the LED
BLEUnsignedIntCharacteristic customTouchedChar("19b10002-e8f2-537e-4f6c-d104768a1214", BLERead | BLENotify); // Touched characteristic that communicates to the phone that the device was touched and turned off
void setup() {
// initialize IMU
if (!IMU.begin()){
Serial.println("Failed to initialize IMU!");
exit(1);
}
// initialize serial communication
Serial.begin(9600);
while (!Serial);
// define LED pins as output
pinMode(LED_BUILTIN, OUTPUT); // will be showing that connection is established
// initialize BLE
if (!BLE.begin()) {
Serial.println("BLE failed to Initiate");
delay(500);
while (1);
}
BLE.setLocalName("Pod_ONE");
BLE.setAdvertisedService(customService);
customService.addCharacteristic(customTouchedChar);
customService.addCharacteristic(customTurnONChar);
BLE.addService(customService);
customTouchedChar.writeValue(0);
BLE.advertise();
Serial.println("Bluetooth device is now active, waiting for connections...");
//String myaddress = BLE.address(); // find the address of the Arduino
//Serial.print("Local address is: ");
//Serial.println(myaddress); // print the address of the arduino
}
void read_Gyro() {
if (IMU.gyroscopeAvailable()){
IMU.readGyroscope(x, y, z);
}
}
void loop() {
BLEDevice central = BLE.central();
if (central) {
Serial.print("Connected to central: ");
Serial.println(central.address());
digitalWrite(LED_BUILTIN, HIGH); // turn on internal LED to show that BLE is connected with phone
while (central.connected()) {
delay(time_delay);
if (customTurnONChar.value()==1){
customTouchedChar.writeValue(0);
Serial.println("Phone");
digitalWrite(ledPin, HIGH);
// here is where we can start the timer
while(customTurnONChar.value()==1){
read_Gyro();
if(y < -delta || y > delta){
Serial.println("touched");
digitalWrite(ledPin, LOW);
customTouchedChar.writeValue(1);
Serial.println(customTouchedChar);
customTurnONChar.setValue(0);
// here is where we would stop the timer and send the value to phone
}
delay(time_delay);
}
}
}
Serial.print("Disconnected");
digitalWrite(LED_BUILTIN, LOW);
}
}