66 it will remotely control the Bluetooth® Low Energy peripheral's LED, when the button is pressed or released.
77
88 The circuit:
9- - Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT,
10- Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board.
11- - Button with pull-up resistor connected to pin 2.
9+ - Boards with integrated BLE or Nucleo board plus one of BLE X-Nucleo shield::
10+ - B-L475E-IOT01A1
11+ - B_L4S5I_IOT01A
12+ - STEVAL-MKBOXPRO
13+ - STEVAL-MKSBOX1V1,
14+ - NUCLEO-WB15CC
15+ - P-NUCLEO-WB55RG
16+ - STM32WB5MM-DK
17+ - X-NUCLEO-IDB05A2
18+ - X-NUCLEO-IDB05A1
19+ - X-NUCLEO-BNRG2A1
1220
1321 You can use it with another board that is compatible with this library and the
1422 Peripherals -> LED example.
1523
1624 This example code is in the public domain.
1725*/
1826
19- #include < ArduinoBLE.h>
27+ #include < STM32duinoBLE.h>
28+
29+ #ifdef USER_BTN
30+ const int buttonPin = USER_BTN; // set buttonPin to on-board user button
31+ #else
32+ const int buttonPin = PC13; // set buttonPin to digital pin PC13 */
33+ #endif
34+
35+ #if defined(ARDUINO_STEVAL_MKBOXPRO)
36+ /* STEVAL-MKBOXPRO */
37+ SPIClass SpiHCI (PA7, PA6, PA5);
38+ HCISpiTransportClass HCISpiTransport (SpiHCI, BLUENRG_LP, PA2, PB11, PD4, 1000000 , SPI_MODE3);
39+ #if !defined(FAKE_BLELOCALDEVICE)
40+ BLELocalDevice BLEObj (&HCISpiTransport);
41+ BLELocalDevice& BLE = BLEObj;
42+ #endif
43+ #elif defined(ARDUINO_STEVAL_MKSBOX1V1)
44+ /* STEVAL-MKSBOX1V1 */
45+ SPIClass SpiHCI (PC3, PD3, PD1);
46+ HCISpiTransportClass HCISpiTransport (SpiHCI, SPBTLE_1S, PD0, PD4, PA8, 1000000 , SPI_MODE1);
47+ #if !defined(FAKE_BLELOCALDEVICE)
48+ BLELocalDevice BLEObj (&HCISpiTransport);
49+ BLELocalDevice& BLE = BLEObj;
50+ #endif
51+ #elif defined(ARDUINO_B_L475E_IOT01A) || defined(ARDUINO_B_L4S5I_IOT01A)
52+ /* B-L475E-IOT01A1 or B_L4S5I_IOT01A */
53+ SPIClass SpiHCI (PC12, PC11, PC10);
54+ HCISpiTransportClass HCISpiTransport (SpiHCI, SPBTLE_RF, PD13, PE6, PA8, 8000000 , SPI_MODE0);
55+ #if !defined(FAKE_BLELOCALDEVICE)
56+ BLELocalDevice BLEObj (&HCISpiTransport);
57+ BLELocalDevice& BLE = BLEObj;
58+ #endif
59+ #elif defined(ARDUINO_NUCLEO_WB15CC) || defined(ARDUINO_P_NUCLEO_WB55RG) ||\
60+ defined (ARDUINO_STM32WB5MM_DK) || defined(P_NUCLEO_WB55_USB_DONGLE)
61+ HCISharedMemTransportClass HCISharedMemTransport;
62+ #if !defined(FAKE_BLELOCALDEVICE)
63+ BLELocalDevice BLEObj (&HCISharedMemTransport);
64+ BLELocalDevice& BLE = BLEObj;
65+ #endif
66+ #else
67+ /* Shield IDB05A2 with SPI clock on D3 */
68+ SPIClass SpiHCI (D11, D12, D3);
69+ HCISpiTransportClass HCISpiTransport (SpiHCI, BLUENRG_M0, A1, A0, D7, 8000000 , SPI_MODE0);
70+ #if !defined(FAKE_BLELOCALDEVICE)
71+ BLELocalDevice BLEObj (&HCISpiTransport);
72+ BLELocalDevice& BLE = BLEObj;
73+ #endif
74+ /* Shield IDB05A2 with SPI clock on D13 */
75+ // #define SpiHCI SPI
76+ // HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M0, A1, A0, D7, 8000000, SPI_MODE0);
77+ // #if !defined(FAKE_BLELOCALDEVICE)
78+ // BLELocalDevice BLEObj(&HCISpiTransport);
79+ // BLELocalDevice& BLE = BLEObj;
80+ // #endif
81+ /* Shield IDB05A1 with SPI clock on D3 */
82+ // SPIClass SpiHCI(D11, D12, D3);
83+ // HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0);
84+ // #if !defined(FAKE_BLELOCALDEVICE)
85+ // BLELocalDevice BLEObj(&HCISpiTransport);
86+ // BLELocalDevice& BLE = BLEObj;
87+ // #endif
88+ /* Shield IDB05A1 with SPI clock on D13 */
89+ // #define SpiHCI SPI
90+ // HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0);
91+ // #if !defined(FAKE_BLELOCALDEVICE)
92+ // BLELocalDevice BLEObj(&HCISpiTransport);
93+ // BLELocalDevice& BLE = BLEObj;
94+ // #endif
95+ /* Shield BNRG2A1 with SPI clock on D3 */
96+ // SPIClass SpiHCI(D11, D12, D3);
97+ // HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1);
98+ // #if !defined(FAKE_BLELOCALDEVICE)
99+ // BLELocalDevice BLEObj(&HCISpiTransport);
100+ // BLELocalDevice& BLE = BLEObj;
101+ // #endif
102+ /* Shield BNRG2A1 with SPI clock on D13 */
103+ // #define SpiHCI SPI
104+ // HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1);
105+ // #if !defined(FAKE_BLELOCALDEVICE)
106+ // BLELocalDevice BLEObj(&HCISpiTransport);
107+ // BLELocalDevice& BLE = BLEObj;
108+ // #endif
109+ #endif
20110
21111// variables for button
22- const int buttonPin = 2 ;
23112int oldButtonState = LOW;
113+ int initialButtonState = LOW;
24114
25115void setup () {
26- Serial.begin (9600 );
116+ Serial.begin (115200 );
27117 while (!Serial);
28118
29119 // configure the button pin as input
30- pinMode (buttonPin, INPUT );
120+ pinMode (buttonPin, INPUT_PULLUP );
31121
32122 // initialize the Bluetooth® Low Energy hardware
33123 BLE.begin ();
34124
125+ // Get initial button state
126+ initialButtonState = digitalRead (buttonPin);
127+ oldButtonState = initialButtonState;
128+
35129 Serial.println (" Bluetooth® Low Energy Central - LED control" );
36130
37131 // start scanning for peripherals
38- BLE.scanForUuid (" 19b10000-e8f2-537e-4f6c-d104768a1214" );
132+ int ret = 1 ;
133+ do
134+ {
135+ ret = BLE.scanForUuid (" 19b10000-e8f2-537e-4f6c-d104768a1214" );
136+ if (ret == 0 )
137+ {
138+ BLE.end ();
139+ BLE.begin ();
140+ }
141+ } while (ret == 0 );
39142}
40143
41144void loop () {
@@ -57,12 +160,30 @@ void loop() {
57160 }
58161
59162 // stop scanning
60- BLE.stopScan ();
163+ int ret = 1 ;
164+ do
165+ {
166+ ret = BLE.stopScan ();
167+ if (ret == 0 )
168+ {
169+ BLE.end ();
170+ BLE.begin ();
171+ }
172+ } while (ret == 0 );
61173
62174 controlLed (peripheral);
63175
64176 // peripheral disconnected, start scanning again
65- BLE.scanForUuid (" 19b10000-e8f2-537e-4f6c-d104768a1214" );
177+ ret = 1 ;
178+ do
179+ {
180+ ret = BLE.scanForUuid (" 19b10000-e8f2-537e-4f6c-d104768a1214" );
181+ if (ret == 0 )
182+ {
183+ BLE.end ();
184+ BLE.begin ();
185+ }
186+ } while (ret == 0 );
66187 }
67188}
68189
@@ -110,7 +231,7 @@ void controlLed(BLEDevice peripheral) {
110231 // button changed
111232 oldButtonState = buttonState;
112233
113- if (buttonState) {
234+ if (buttonState != initialButtonState ) {
114235 Serial.println (" button pressed" );
115236
116237 // button is pressed, write 0x01 to turn the LED on
0 commit comments