/* lp_BLE_debounce_GT832E_01 Turns on an 'led' on/off at 10Hz Debounces pin A7 P0.31 -- Active low, switch input either GND or open This sketch sets up a Nordic BLE UART and sends the debounced button state when connected (c)2022 Forward Computing and Control Pty. Ltd. This example code is in the public domain. */ #include #include #include int led = 14; // Use P0.14 has the 'led' pin bool ledOn = false; int comparatorPin = A7; // P0.31 lp_BLESerial ble; lp_timer debounceTimer; uint32_t debounceTimeOut = 20; // ms int lastButtonState = -1; // not set initially int buttonState = -1; // not set initially void setup() { pinMode(comparatorPin, INPUT_PULLUP); // set compare pin with INPUT_PULLUP or INPUT_PULLDOWN to prevent floating pin triggers pinMode(led, OUTPUT); // initialize the digital pin as an output. ble.setName("Button Debounce"); // set advertised name, default name is "Nordic BLE UART" ble.begin(); // start advertising and be ready to accept connections lp_comparator_start(comparatorPin, REF_8_16Vdd, handlePinLevelChange); // always triggers pin LOW first, then if pin HIGH, will trigger HIGH } void loop() { sleep(); // just sleep here waiting for a trigger while (ble.available()) { int i = ble.read(); // clear BLE recieve buffer and ignore } } // called when pin changes state, pinState is state detected, HIGH or LOW void handlePinLevelChange(int pinState) { if (pinState != lastButtonState) { lastButtonState = pinState; debounceTimer.stop(); // stop last timer if any debounceTimer.startDelay(debounceTimeOut, handleDebounceTimeout); } } void handleDebounceTimeout() { buttonState = lastButtonState; // input has settled // ble.print("maxQ:"); ble.print(app_sched_queue_utilization_get()); //needs #define SCHEDULER_PROFILER in utility/app_schedule.h ble.print(' '); ble.print((buttonState == HIGH) ? 'H' : 'L'); digitalWrite(led, buttonState); // turn the LED on when input HIGH }