Connecting Arduino Nano 33 to smartphone

Hi everyone,

I am trying to make an app using the Arduino 33 Nano. The idea is to utilize the gyroscope. The app will allow me to preset a value and then every up down motion will count as 1. This will then go to the MIT app inventor. The idea behind the app is to set a value. Use the arduino and then and then the LED will turn red when that value has been met. Im struggling with configuring the code and having the arduino connect to my phone via bluetooth. Any tips to make this app run?

How did you determine it is not running?

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for advice on (nor for problems with) your project. See About the Installation & Troubleshooting category.

There is no Nano 33, there are Nano 33 BLE, Nano 33 BLE Sense and Nano 33 IoT. Feel free to move your topic to one of these dedicated categories yourself if needed.

#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
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

///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 ) ;
BLEFloatCharacteristic repCharacteristic( UUID_repCharacteristic , BLERead | BLENotify ) ;
BLEFloatCharacteristic targetCharacteristic ( UUID_targetCharacteristic , BLEWrite | BLENotify ) ;
BLEFloatCharacteristic resetCharacteristic( UUID_resetCharacteristic , BLERead | BLENotify ) ;

//Setting up the arduinos outputs
void setup () {
Serial.begin(115200);//begin the bluetooth
BLE.begin();
BLE.setLocalName("RepTrack1.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
if (!IMU.begin()) {
Serial.println ("Failed to initialize IMU!");
while (1);
}
}
}
void loop()
{
static long preMillis = 0;

// looks for BLE central devices
BLEDevice central = BLE.central();

// Checks for connection
int command=0;
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
    presetValue=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 (repCharacteristic >= targetCharacteristic){//(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);

}

}

Could someone please review my code and tell me why I am getting a error message saying C:\Users\Maybelle\AppData\Local\Temp\untitled1375503030.tmp\sketch_dec20a\sketch_dec20a.ino:6:6: note: 'void loop()' previously defined here

void loop() {

  ^~~~

RepTrack code(1):88:9: error: 'updateApp' was not declared in this scope

     updateApp(); // Calls function to send values to app

     ^~~~~~~~~

exit status 1
redefinition of 'void setup()'

C:\Users\Maybelle\AppData\Local\Temp\arduino_modified_sketch_313783\RepTrack code(1).ino: In function 'void setup()':

RepTrack code(1):27:6: error: redefinition of 'void setup()'

You can only have one (or no) loop functions per sketch
(Though, is it a sketch if it doesn't have a loop, because then, it has to have a main function? Questions, questions, questions)

Your topic was moved to its current location as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

It will help you get the best out of the forum in the future.

Thank you

Edit: I now notice this is the second topic you have created in Installation and Trouble Shooting. Please take more care where you post your questions otherwise you will be prevented from posting.

Thank you.

Do you know how I can fix this?

You could start by posting your code correctly, so that others can copy and study it.
Does your source could have any other tabs in the IDE?

U-oh. I don't like the look of that "(1)"

As requested to, pls post using code tags.

Right before void loop() you have 3 of these: }, but I counted only 2 of these: { before. These go kind of hand in hand... This might though not be your only problem.

How do I post it correctly? Its not letting me upload anything from a file so I copy and pasted it

#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
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

///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 ) ;
BLEFloatCharacteristic repCharacteristic( UUID_repCharacteristic , BLERead | BLENotify ) ; 
BLEFloatCharacteristic targetCharacteristic ( UUID_targetCharacteristic , BLEWrite | BLENotify ) ;
BLEFloatCharacteristic resetCharacteristic( UUID_resetCharacteristic , BLERead | BLENotify ) ; 

//Setting up the arduinos outputs
void setup () {
  Serial.begin(115200);//begin the bluetooth
  BLE.begin();
  BLE.setLocalName("RepTrack1.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
  if (!IMU.begin()) {
    Serial.println ("Failed to initialize IMU!");
    while (1);
  }  
}
void loop() 
  {
  static long preMillis = 0;
  
  // looks for BLE central devices
  BLEDevice central = BLE.central();

  // Checks for connection
  int command=0;
  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
        presetValue=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 (repCharacteristic >= targetCharacteristic){//(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);
   
  }
  
}

Good job on posting your code in the correct manner.

The code you just posted compiles for a Nano33 BLE successfully for me with both 1.8.19 and 2.0.3.

Are you still having an issue?

You CANNOT have any .ino files in the sketch directory that are not PART of the sketch. You clearly have at least one .ino file that does not belong there.

If there are updates with the app and libraries will that cause issues? it wont compile

Your problem has nothing to do with libraries. It is YOUR code that is the problem. It is finding TWO setup functions, TWO loop functions, and NO updateApp function.

#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
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

///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 ) ;
BLEFloatCharacteristic repCharacteristic( UUID_repCharacteristic , BLERead | BLENotify ) ;
BLEFloatCharacteristic targetCharacteristic ( UUID_targetCharacteristic , BLEWrite | BLENotify ) ;
BLEFloatCharacteristic resetCharacteristic( UUID_resetCharacteristic , BLERead | BLENotify ) ;

//Setting up the arduinos outputs
void setup () {
Serial.begin(115200);//begin the bluetooth
BLE.begin();
BLE.setLocalName("RepTrack1.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
if (!IMU.begin()) {
Serial.println ("Failed to initialize IMU!");
while (1);
}
}
void loop()
{
static long preMillis = 0;

// looks for BLE central devices
BLEDevice central = BLE.central();

// Checks for connection
int command=0;
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
    presetValue=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 (repCharacteristic >= targetCharacteristic){//(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);

}

}

It is very inconvenient to look at your code, because it is not formatted and doesn't have a code tags. Offering such a code to forum is disrespectful to users.
Please edit your message and insert code in code tags.

I am sorry I have never used this before and I do not know how to do that. It wont let me upload a file

It seems like you skipped the reading forum guidelines.

Read the forum guidelines to see how to properly post code and some good information on how to ask the question.
Please do not post again until you read it completely.