Skip to content

Commit 38b5c5f

Browse files
committed
chore(examples): enhance button support
Signed-off-by: Frederic Pillon <[email protected]>
1 parent 36cc974 commit 38b5c5f

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

examples/Central/LedControl/LedControl.ino

+12-3
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,28 @@
1717
#include <STM32duinoBLE.h>
1818

1919
// variables for button
20-
const int buttonPin = 2;
20+
#ifdef USER_BTN
21+
const int buttonPin = USER_BTN; // set buttonPin to on-board button
22+
#else
23+
const int buttonPin = PC13; // set buttonPin to digital pin PC13 */
24+
#endif
2125
int oldButtonState = LOW;
26+
int initialButtonState = LOW;
2227

2328
void setup() {
2429
Serial.begin(9600);
2530
while (!Serial);
2631

2732
// configure the button pin as input
28-
pinMode(buttonPin, INPUT);
33+
pinMode(buttonPin, INPUT_PULLUP);
2934

3035
// initialize the Bluetooth® Low Energy hardware
3136
BLE.begin();
3237

38+
// Get initial button state
39+
initialButtonState = digitalRead(buttonPin);
40+
oldButtonState = initialButtonState;
41+
3342
Serial.println("Bluetooth® Low Energy Central - LED control");
3443

3544
// start scanning for peripherals
@@ -108,7 +117,7 @@ void controlLed(BLEDevice peripheral) {
108117
// button changed
109118
oldButtonState = buttonState;
110119

111-
if (buttonState) {
120+
if (buttonState != initialButtonState) {
112121
Serial.println("button pressed");
113122

114123
// button is pressed, write 0x01 to turn the LED on

examples/Peripheral/ButtonLED/ButtonLED.ino

+6-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
#include <STM32duinoBLE.h>
1919

2020
const int ledPin = LED_BUILTIN; // set ledPin to on-board LED
21-
const int buttonPin = 4; // set buttonPin to digital pin 4
21+
#ifdef USER_BTN
22+
const int buttonPin = USER_BTN; // set buttonPin to on-board button
23+
#else
24+
const int buttonPin = PC13; // set buttonPin to digital pin PC13 */
25+
#endif
2226

2327
BLEService ledService("19B10010-E8F2-537E-4F6C-D104768A1214"); // create service
2428

@@ -32,7 +36,7 @@ void setup() {
3236
while (!Serial);
3337

3438
pinMode(ledPin, OUTPUT); // use the LED as an output
35-
pinMode(buttonPin, INPUT); // use button pin as an input
39+
pinMode(buttonPin, INPUT_PULLUP); // use button pin as an input
3640

3741
// begin initialization
3842
if (!BLE.begin()) {

0 commit comments

Comments
 (0)