Code Arduino Not Working

Hello, I am having trouble receiving data to my appInventor. I think my code is wrong. Can someone please have a look at it and tell me if it seems wrong because i am not receiving anything in my monitor serial nor my app. It's my first time using Nano 33 BLE.
#include <ArduinoBLE.h>
int NbTopsFan; // Nombre de tours d'hélices
float Calc; // Calcul du volume
float Q; // Calcul du débit
int hallsensor = 2; // PIN DIGITALE 2

BLEService bleuart("19B10010-E8F2-537E-4F6C-D104768A1214"); // UUID du service BLE UART, on crée un service
BLECharacteristic txCharacteristic("19B10010-E8F2-537E-4F6C-D104768A1214", BLEWriteWithoutResponse, 20); // UUID de la caractéristique d'écriture

void rpm() {NbTopsFan++;}
void setup() {
Serial.begin(9600);
pinMode(hallsensor, INPUT);
attachInterrupt(digitalPinToInterrupt(hallsensor), rpm, RISING);
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");
while (1);}

BLE.setLocalName("Arduino Nano BLE 33");
BLE.setAdvertisedService(bleuart); //Définit le service BLE annoncé
bleuart.addCharacteristic(txCharacteristic);
BLE.addService(bleuart);
BLE.advertise();
Serial.println("En attente de la connexion BLE...");}

void loop() {
BLEDevice central = BLE.central();
if (central) {
Serial.print("Connecté à : ");
Serial.println(central.address());
while (central.connected()) {
NbTopsFan = 0;
delay(1000);
Q = (NbTopsFan / 9.5);
Calc = (Q / 60) + Calc;
String To_App = String(Calc, 3) + "," + String(Q, 3);
Serial.println(To_App);
txCharacteristic.writeValue(To_App.c_str(), To_App.length());
delay(200);}
Serial.print("Déconnecté de : ");
Serial.println(central.address());}
}

I suggest you read the forum guidelines and follow them. If you do that you are likely to get much more help. Code needs to be posted in code tags. This link will get you started: How to get the best out of this forum

You have two sketches pasted together.

Sketch #1

#include <ArduinoBLE.h>
int NbTopsFan; // Nombre de tours d'hélices
float Calc; // Calcul du volume
float Q; // Calcul du débit
int hallsensor = 2; // PIN DIGITALE 2

BLEService bleuart("19B10010-E8F2-537E-4F6C-D104768A1214"); // UUID du service BLE UART, on crée un service
BLECharacteristic txCharacteristic("19B10010-E8F2-537E-4F6C-D104768A1214", BLEWriteWithoutResponse, 20); // UUID de la caractéristique d'écriture

void rpm() {
  NbTopsFan++;
}
void setup() {
  Serial.begin(9600);
  pinMode(hallsensor, INPUT);
  attachInterrupt(digitalPinToInterrupt(hallsensor), rpm, RISING);
  if (!BLE.begin()) {
    Serial.println("starting Bluetooth® Low Energy module failed!");
    while (1);
  }

  BLE.setLocalName("Arduino Nano BLE 33");
  BLE.setAdvertisedService(bleuart); //Définit le service BLE annoncé
  bleuart.addCharacteristic(txCharacteristic);
  BLE.addService(bleuart);
  BLE.advertise();
  Serial.println("En attente de la connexion BLE...");
}

void loop() {
  BLEDevice central = BLE.central();
  if (central) {
    Serial.print("Connecté à : ");
    Serial.println(central.address());
    while (central.connected()) {
      NbTopsFan = 0;
      delay(1000);
      Q = (NbTopsFan / 9.5);
      Calc = (Q / 60) + Calc;
      String To_App = String(Calc, 3) + "," + String(Q, 3);
      Serial.println(To_App);
      txCharacteristic.writeValue(To_App.c_str(), To_App.length());
      delay(200);
    }
    Serial.print("Déconnecté de : ");
    Serial.println(central.address());
  }
}

Sketch #2

#include <MD_MAX72xx.h>
#define	MAX_DEVICES	2

const int maxX = MAX_DEVICES * 8 - 1;
const int maxY = 7;

#define	CLK_PIN		13
#define	DATA_PIN	11
#define	CS_PIN		10

#define VERT_PIN A0
#define HORZ_PIN A1
#define SEL_PIN  2

MD_MAX72XX mx = MD_MAX72XX(MD_MAX72XX::PAROLA_HW, CS_PIN, MAX_DEVICES);

int x = 0;
int y = 0;

void setup() {
  mx.begin();
  mx.control(MD_MAX72XX::INTENSITY, MAX_INTENSITY / 2);
  mx.clear();

  pinMode(VERT_PIN, INPUT);
  pinMode(HORZ_PIN, INPUT);
  pinMode(SEL_PIN, INPUT_PULLUP);
}

// the loop function runs over and over again forever
void loop() {
  int horz = analogRead(HORZ_PIN);
  int vert = analogRead(VERT_PIN);
  if (vert < 300) {
    y = min(y + 1, maxY);
  }
  if (vert > 700) {
    y = max(y - 1, 0);
  }
  if (horz > 700) {
    x = min(x + 1, maxX);
  }
  if (horz < 300) {
    x = max(x - 1, 0);
  }
  if (digitalRead(SEL_PIN) == LOW) {
    mx.clear();
  }
  mx.setPoint(y, x, true);
  mx.update();
  delay(100);
}
//BLECharacteristic txCharacteristic("19B10010-E8F2-537E-4F6C-D104768A1214", BLEWriteWithoutResponse, 20); // UUID de la caractéristique d'écriture
BLECharacteristic txCharacteristic("19B10010-E8F2-537E-4F6C-D104768A1214", BLERead, 20); // UUID de la caractéristique d'écriture

You want the central app to read the characteristic, not write to it.

With this change, using Light Blue, I can see the data output of calc and Q.

@xfpd

You have two sketches pasted together.

??

@cattledog - The original "sketch" had two setup() and two loop() ... so I separated and posted them.

Strange. I don't see any edits of the original post. I would have thought the forum tracks everything.

You are right... I must have double-pasted... oops. Sorry.

Sorry I didn't understand. I posted one sketch not two.

Yes, I made a mistake by pasting a second sketch. Sorry. (I would like to delete it, but that would make the thread read strange)

Thank you so much, you really did correct my code. I have one more question please if you familiar with app inventor.




Please Help me SOS

I do not know much about app inventor, but there is a forum where you might be able to ask your questions.

Hello, I don't receive anything in my BlueLight App. I can connect the BLE and that's it. And i only see the values in my serial monitor. And when i disconnect from the app. The serial monitor stops. I want to receive the data in my app. Can you please help me

I can see the data on my phone, so there maybe and issue with how you are using LightBlue.

After you connect you should be able to go to the bottom of the screen and see the characteristics.

Then press the little arrow to navigate to the screen for that characteristic. Then select the data format to be UTF-8 String and press the READ AGAIN button.

I am testing with a pwm signal of 500Hz on pin4 jumpered to pin3.
The 52.632 value is 500/9.5 = Q.

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