Hello I have problems making my code function when the Arduino is connected to a external powersupply. Have been troubleshooting for a while now and after a lot of testing I have concluded that there is something in my code that only allows it to work when it is connected with usb. In short im setting up a peripheral that need to work without a usb connection. I have done testing with simpler code (make a LED blink etc) and in that case the arduino works, so i dont think it is the powersupply that is the problem (which is 5 v). please help
#include <ArduinoBLE.h>
int ledPin = 2;
int testPin = 3;
char* deviceServiceUuid = "19B10010-E8F2-537E-4F6C-D104768A1214";
char* deviceServiceCharacteristicUuid = "19B10011-E8F2-537E-4F6C-D104768A1214";
BLEService ledService(deviceServiceUuid);
// create switch characteristic and allow remote device to read and write
BLEByteCharacteristic ledCharacteristic(deviceServiceCharacteristicUuid, BLERead | BLEWrite);
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(testPin, OUTPUT);
BLE.begin();
// set the local name peripheral advertises
BLE.setLocalName("LED");
// set the UUID for the service this peripheral advertises:
BLE.setAdvertisedService(ledService);
// add the characteristics to the service
ledService.addCharacteristic(ledCharacteristic);
// add the service
BLE.addService(ledService);
ledCharacteristic.writeValue(0);
// start advertising
BLE.advertise();
}
void loop() {
digitalWrite(testPin,HIGH);
BLE.poll();
char ledValue = digitalRead(ledPin);
if (ledCharacteristic.written()) {
// update LED, either central has written to characteristic or button state has changed
if (ledCharacteristic.value()) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}
}
}