File tree 2 files changed +18
-5
lines changed
2 files changed +18
-5
lines changed Original file line number Diff line number Diff line change 17
17
#include < STM32duinoBLE.h>
18
18
19
19
// 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
21
25
int oldButtonState = LOW;
26
+ int initialButtonState = LOW;
22
27
23
28
void setup () {
24
29
Serial.begin (9600 );
25
30
while (!Serial);
26
31
27
32
// configure the button pin as input
28
- pinMode (buttonPin, INPUT );
33
+ pinMode (buttonPin, INPUT_PULLUP );
29
34
30
35
// initialize the Bluetooth® Low Energy hardware
31
36
BLE.begin ();
32
37
38
+ // Get initial button state
39
+ initialButtonState = digitalRead (buttonPin);
40
+ oldButtonState = initialButtonState;
41
+
33
42
Serial.println (" Bluetooth® Low Energy Central - LED control" );
34
43
35
44
// start scanning for peripherals
@@ -108,7 +117,7 @@ void controlLed(BLEDevice peripheral) {
108
117
// button changed
109
118
oldButtonState = buttonState;
110
119
111
- if (buttonState) {
120
+ if (buttonState != initialButtonState ) {
112
121
Serial.println (" button pressed" );
113
122
114
123
// button is pressed, write 0x01 to turn the LED on
Original file line number Diff line number Diff line change 18
18
#include < STM32duinoBLE.h>
19
19
20
20
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
22
26
23
27
BLEService ledService (" 19B10010-E8F2-537E-4F6C-D104768A1214" ); // create service
24
28
@@ -32,7 +36,7 @@ void setup() {
32
36
while (!Serial);
33
37
34
38
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
36
40
37
41
// begin initialization
38
42
if (!BLE.begin ()) {
You can’t perform that action at this time.
0 commit comments