I'm trying to connect connect to my Arduino R4 using Bluetooth Serial Monitor on my Android phone. I can see the Arduino when it scans, but connection always fails. As an utter newb, I'm wondering if this is linked to my code...or if I'm just thick and missing something obvious. This is the code I'm using:
#include <ArduinoBLE.h>
const int sensorPin = A0;
// Calibration multiplier (adjust after calibration)
const float multiplier = 50.0;
float highestForce = 0;
unsigned long lastHighTime = 0;
const unsigned long clearInterval = 15000; // 15 seconds in milliseconds
void setup() {
Serial.begin(9600);
while (!Serial);
BLE.begin();
if (!BLE.begin()) {
Serial.println("starting BLE failed!");
while (1);
}
BLE.setLocalName("UNO R4");
BLE.advertise();
Serial.println("Bluetooth device active, waiting for connections...");
}
void loop() {
// Read sensor
int raw = analogRead(sensorPin);
float voltage = raw * (5.0 / 1023.0);
float force = voltage * multiplier;
// Update highest force if current force exceeds it
if (force > highestForce) {
highestForce = force;
lastHighTime = millis(); // Reset timer
}
// Check if 15 seconds have passed since last high force
if (millis() - lastHighTime >= clearInterval) {
highestForce = 0;
}
// Print output
Serial.print("Current Force: ");
Serial.print(force, 2);
Serial.print(" lbs\tHighest Force: ");
Serial.print(highestForce, 2);
Serial.println(" lbs");
delay(200);
}