Hello, I am trying to use two buttons to write values to a Bluetooth characteristic inside interrupts. The code runs perfect but after about 20 button presses the code just stops working, so I'm thinking it maybe runs out of memory? Any ideas on the issue would be greatly appreciated.
#include <ArduinoBLE.h>
const int ledPin = LED_BUILTIN; // set ledPin to on-board LED
const int shiftUpButtonPin = 2;
const int shiftDownButtonPin = 3;
volatile byte shiftUpState = 0;
volatile byte shiftDownState = 0;
BLEService ShifterService("597dbdfd-2db2-42e3-ad54-d2aef36e4805");
BLEBooleanCharacteristic shiftUpCharacteristic("11bb186f-748a-4542-ac48-032e589d5e48", BLENotify);
BLEBooleanCharacteristic shiftDownCharacteristic("0c618810-b694-426b-aed1-357c35978eec", BLENotify);
void shiftUp(){
shiftUpState = !shiftUpState;
shiftUpCharacteristic.writeValue(shiftUpState);
digitalWrite(ledPin, shiftUpState);
}
void shiftDown(){
shiftDownState = !shiftDownState;
shiftDownCharacteristic.writeValue(shiftDownState);
digitalWrite(ledPin, shiftDownState);
}
void setup() {
Serial.begin(115200);
//while (!Serial);
pinMode(ledPin, OUTPUT);
pinMode(shiftUpButtonPin, INPUT_PULLUP);
pinMode(shiftDownButtonPin, INPUT_PULLUP);
if (!BLE.begin()) // initialize the BLE device
{ // 1 on success, 0 on failure
Serial.println("Failed to start BLE!");
while (1); // only wait out is to kill execution
}
BLE.setLocalName("Shifter");
BLE.setAdvertisedService(ShifterService);
ShifterService.addCharacteristic(shiftUpCharacteristic);
ShifterService.addCharacteristic(shiftDownCharacteristic);
BLE.addService(ShifterService);
shiftUpCharacteristic.setValue(0);
shiftDownCharacteristic.setValue(0);
BLE.advertise(); // start advertising
attachInterrupt(digitalPinToInterrupt(shiftUpButtonPin), shiftUp, CHANGE);
attachInterrupt(digitalPinToInterrupt(shiftDownButtonPin), shiftDown, CHANGE);
digitalWrite(ledPin, LOW);
Serial.println("Bluetooth device active, waiting for connections...");
}
void loop() {
BLEDevice central = BLE.central();
if(central){
while(central.connected()){
}
}
}