Hi, thanks in advance.
So I am trying to create a simple device where will mesure the distance using ultrasonic sensor and send the distance to a Central. But I got an issue when I use together the pulseIn() method with BLE Central Connected.
My board: Arduino Nano 33 BLE Sense
Can you give me guide about this issue? Thanks
Andre Arruda
================= Code ===============
#include
#include
BLEService yetiService("19B10010-E8F2-537E-4F6C-D104768A1214"); // create service
BLEUnsignedCharCharacteristic distanceLevelChar("19B10011-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify);
#define ECHO_PIN 5
#define TRIG_PIN 6
#define MAX_DISTANCE 400
#define MIN_DISTANCE 2
// SETUP
void setup()
{
if (!HTS.begin())
{
while (1);
}
pinMode(ECHO_PIN, INPUT);
pinMode(TRIG_PIN, OUTPUT);
if (!BLE.begin())
{
while (1);
}
BLE.setLocalName("PROJECT");
BLE.setAdvertisedService(yetiService);
yetiService.addCharacteristic(distanceLevelChar);
BLE.addService(yetiService);
distanceLevelChar.writeValue(0);
BLE.advertise();
}
void loop()
{
BLEDevice central = BLE.central();
if (central)
{
while (central.connected())
{
updateDistance();
distanceLevelChar.writeValue(7);
}
}
}
float updateDistance()
{
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
float duration = pulseIn(ECHO_PIN, HIGH); // !!!!!!!!! Here is the issue. THANKS
/*float duration = (duration / 2) * 0.0343;
if (distance >= MIN_DISTANCE && distance <= MAX_DISTANCE)
{
return distance;
}
*/
return 0;
}
Would it be possible for you to be a bit more specific about what you mean by "issue".
Is the code/board freezing? Is it not returning a good result? Have you tried a timeout on pulseIn to see if it is just not getting a high value? Does using pulseIn cause the BLE to disconnect?
It might be best practice to have your duration variable as an unsigned long as pulseIn() returns an unsigned long, not a float.
If it is freezing/getting stuck waiting then that might be down to your wiring, doubly make sure everything is wired up correctly. Also, have you tried the approach used in the Arduino Ultrasonic library:
There they use this kind of code:
previousMicros = micros();
while(!digitalRead(echo) && (micros() - previousMicros) <= timeout); // wait for the echo pin HIGH or timeout
previousMicros = micros();
while(digitalRead(echo) && (micros() - previousMicros) <= timeout); // wait for the echo pin LOW or timeout
return micros() - previousMicros; // duration
This is basically what pulseIn() does but I tried to find out how pulseIn() works and it seems a bit more complicated.
From my recollection, the BLE doesn't like any kind of delay() or delays functions whilst it is working so you need to find workarounds that allow the BLE to work whilst also collecting the echos. So that might mean you will have keep time whilst also allowing while (central.connected()) to loop.
Hi,
I have the same problem with my Nano 33 LBE and apparently when pulsein is called it leaves no "spare processor" time for the bluetooth module to behave correctly and this makes the remote device disconnect. If we replace the pulsin with a new pulsin that is of course not as accurate as the "native one" and introduce a delay of 1us like this:
// wait for any previous pulse to end
while (digitalRead(pin)){
wait_us(1);
if (micros() - begin >= timeout)
return 0;
}
// wait for the pulse to start
while (!digitalRead(pin)){
wait_us(1);
if (micros() - begin >= timeout)
return 0;
}
uint32_t pulseBegin = micros();
// wait for the pulse to stop
while (digitalRead(pin)){
wait_us(1);
if (micros() - begin >= timeout)
return 0;
}
uint32_t pulseEnd = micros();
return pulseEnd - pulseBegin;
}
it works. But as I said this method is not very accurate which lead to bad measurements...
Any idea how to fix this?
Thanks