File tree Expand file tree Collapse file tree 2 files changed +18
-5
lines changed Expand file tree Collapse file tree 2 files changed +18
-5
lines changed Original file line number Diff line number Diff line change 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
2125int oldButtonState = LOW;
26+ int initialButtonState = LOW;
2227
2328void 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
Original file line number Diff line number Diff line change 1818#include < STM32duinoBLE.h>
1919
2020const 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
2327BLEService 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 ()) {
You can’t perform that action at this time.
0 commit comments