No data transfer via bluetooth

I am trying to transfer data from my Arduino Nano 33 BLE using the LightBLue app. I can connect to the device but I do not see any data on it. I am using the gyroscope so I feel there should be some values connected to the Arduinos movement. Can someone look at my code and tell me if I am missing something?

#include <Arduino_LSM9DS1.h> //This is the library for the gyroscope
#include <ArduinoBLE.h> //The bluetooth library



float X,Y,Z;//Float variable to be used in the gyroscope

#define LED 13//defines LED as pin 13

///Establishes GUUID along wiith the three characteristics
const  char * UUID_RepTrackservice =  "84582cd0-3df0-4e73-9496-29010d7445dd" ; 
const  char * UUID_repCharacteristic =  "84582cd1-3df0-4e73-9496-29010d7445dd" ; 
const  char * UUID_targetCharacteristic =  "84582cd2-3df0-4e73-9496-29010d7445dd" ;
const  char * UUID_resetCharacteristic =  "84582cd3-3df0-4e73-9496-29010d7445dd" ;

//Establishes the service along with defining the characteristics as a float
BLEService RepTrackservice ( UUID_RepTrackservice );
BLEIntCharacteristic repCharacteristic( UUID_repCharacteristic, BLERead | BLENotify ); 
BLEIntCharacteristic targetCharacteristic ( UUID_targetCharacteristic, BLEWrite | BLENotify );
BLEIntCharacteristic resetCharacteristic( UUID_resetCharacteristic, BLERead | BLENotify ); 

int presetValue = 5;// max value for z axis to detect rep count 
int currentValue = 0;//Current z axis value 
int repAmount=0;//variable used to hold rep amount sent to app commented out for troubleshooting
int repTarget=0;//variable used to hold repetitions wanted sent from app commented out for troubleshooting
int reset=0;//Variable to reset the count sent from app commented out for troubleshooting

//Setting up the arduinos outputs
void setup () 
{
  Serial.begin(115200);//begin the bluetooth
  uint32_t t=millis();
  while (!Serial) // delay 5sec connection
  {
    if ((millis()-t) > 5000) break;
  }
  bool err=false;
  if (!IMU.begin()) 
    {
    Serial.println("IMU: failed");
    err=true;
    }
  Serial.println("IMU: ok");

  // init BLE
  if (!BLE.begin()) 
    {
    Serial.println("BLE: failed");
    err=true;
    }
  Serial.println("BLE: ok");

  // error: flash led forever
  if (err)
    {
    Serial.println("Init error. System halted");
    }

  BLE.setLocalName("RepTrack2.0");//sets the local name 
  BLE.setDeviceName("RepTrack");

  BLE.setAdvertisedService (RepTrackservice);//setting the service to be advertised 

  //Adds services to their own charcteristics
  RepTrackservice.addCharacteristic ( repCharacteristic ) ; 
  RepTrackservice.addCharacteristic ( targetCharacteristic ) ;
  RepTrackservice.addCharacteristic ( resetCharacteristic ) ; 

  BLE.addService ( RepTrackservice);//service is added to he BLE

  //Sets values so that they do not transmit random values
  repCharacteristic.writeValue ( 0 ); 
  targetCharacteristic.writeValue ( 0 );
  resetCharacteristic.writeValue ( 0 ); 

  BLE.advertise();//Publishes the service to start
 


  pinMode (LED, OUTPUT);//sets LED as an output
  digitalWrite(LED, LOW);//turns off LED in case it was on
 
}
void loop() 
{
  static long preMillis = 0;
  int command=0;
  // looks for BLE central devices
  BLEDevice central = BLE.central();

  // Checks for connection
  if (central) 
    {
    Serial.print("Connected to central: ");
    Serial.println(central.address()); // prints address
    
    // Loop while connected
    while (central.connected()) 
      {
    
      if (targetCharacteristic.written()) 
        {
        command = targetCharacteristic.value(); // retrieve value from app
        repTarget=command;
        Serial.print(F("commmand value:  "));
        Serial.println(command);
        }
      
      long curMillis = millis();
      if (preMillis>curMillis) preMillis=0; 
      if (curMillis - preMillis >= 10) // check values every 10mS
        {
        preMillis = curMillis;
        updateApp(); // Calls function to send values to app 
        }
      } 

    // To disconnect
    Serial.print(F("Disconnected from central: "));
    Serial.println(central.address());
    } 
}

//Updating the app function
void updateApp()
{
  //Starting the scan to monitor the gyroscope
  //Checking to see if the Gyro is online and then Reads all 3 axis
   if (IMU.gyroscopeAvailable()) 
    {
    IMU.readGyroscope(X, Y, Z);
  currentValue =Z;//sets current value to the value of Z from the gryo
   }
   //Comparison that is the current value of z is greater than or equal to the preset, then you increase the rep amount. This means a rep was done
  if (currentValue >= presetValue) 
  {
    repAmount=repAmount+1;
    repCharacteristic.writeValue(repAmount);//updates the live value
  }
  //If it was not greater then the repamount stays the same 
  else 
  {
    //repAmount=repAmount;
    repAmount=repAmount;
  }
  //displays values for visual purpose 
  Serial.print("Z:");
  Serial.print(Z);
  Serial.print("Current Value:");
 // Serial.println(currentValue);
 // Serial.println(repAmount);//Sends repamout to app
  Serial.println();
  delay(100);
  //Checks to see if the repAmount is greater than or equal to the rep target, if yes then the LED will light up 
   if (repAmount >= repTarget)
  {
    digitalWrite(LED, HIGH);
  }
  //If not then LED stays off
  else 
  {
    digitalWrite(LED, LOW);
  }
  delay(1000);// Delay to keep the system from readding two reps within a second
  //Reset for the rep amount, recieved from the app 
  if (reset==1)
  {
    digitalWrite(LED,LOW);
    repAmount=0;
    targetCharacteristic.writeValue(repAmount);
   
  }
  
}

You should view a lot diagnostic messages in the console with your gyroscope data. Do you see any?

1 Like

I have no activity in the serial monitor. I am unsure what I am doing wrong. Did I forget to include something?

You should at least see this:

If it is not a case - test your Serial connection and make sure that the board is started.

I do have some values you now but it wont show up in any bluetooth apps. I am using light blue and nrf connect

Capture

As far I can see, all values of the Z axis in the listing are lower than 1. Your program only send data to BLE if the value is more than 5...
I think that's why you don't get anything in BLE.

So I am not actively moving my device is that why it is not counting anything?

you can decrease the threshold value:

Also, according to program - it doesn't send the gyroscope values at all, it only send something named repAmount - I don't understand what this...

Are wrote the code yourself or just copy it from somebody project? Do you understand what the code do?

So maybe I can explain my project so it makes sense. We are trying to design an app connected to the Arduino via bluetooth. The app will allow is to preset a value like 5 and when the gyroscope reaches that limit the red LED will turn on. None of that is happening and I am not sure why

I have created this code with my partner. Its our senior project. RepAmount is the amount of times the arduino is going up and down.

So try to decrease presetValue to, for example, 1 and see what happened

Do you know why it is saying ok? I have never done any programming nor I have used arduino

Capture

What the difference from the post#5?
Are you changed something?

I set the value to 1 like you said.

It seems like your board is start to reset after some time of the work...

Sorry I dont have an idea about it.

It looks like it is restarting after the value has been met. I guess that is leading in the right direction. I wanted the red LED to come on instead. Do you know where I can change that?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.