66 it will remotely control the BLE 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+ - STEVAL-MKSBOX1V1, B-L475E-IOT01A1, or a Nucleo board plus the X-NUCLEO-IDB05A1 or the X-NUCLEO-BNRG2A1
1210
1311 You can use it with another board that is compatible with this library and the
1412 Peripherals -> LED example.
1816
1917#include < ArduinoBLE.h>
2018
19+ #if defined(ARDUINO_STEVAL_MKSBOX1V1)
20+ /* STEVAL-MKSBOX1V1 */
21+ SPIClass SpiHCI (PC3, PD3, PD1);
22+ HCISpiTransportClass HCISpiTransport (SpiHCI, SPBTLE_1S, PD0, PD4, PA8, 1000000 , SPI_MODE1);
23+ BLELocalDevice BLE (&HCISpiTransport);
24+ const int buttonPin = PG1; // set buttonPin to digital pin PG1
25+ #elif defined(ARDUINO_DISCO_L475VG_IOT)
26+ /* B-L475E-IOT01A1 */
27+ SPIClass SpiHCI (PC12, PC11, PC10);
28+ HCISpiTransportClass HCISpiTransport (SpiHCI, SPBTLE_RF, PD13, PE6, PA8, 8000000 , SPI_MODE0);
29+ BLELocalDevice BLE (&HCISpiTransport);
30+ const int buttonPin = PC13; // set buttonPin to digital pin PC13
31+ #else
32+ /* Shield IDB05A1 with SPI clock on D3 */
33+ SPIClass SpiHCI (D11, D12, D3);
34+ HCISpiTransportClass HCISpiTransport (SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000 , SPI_MODE0);
35+ BLELocalDevice BLE (&HCISpiTransport);
36+ const int buttonPin = PC13; // set buttonPin to digital pin PC13
37+ /* Shield IDB05A1 with SPI clock on D13 */
38+ /* #define SpiHCI SPI
39+ HCISpiTransportClass HCISpiTransport(SpiHCI, SPBTLE_RF, A1, A0, D7, 8000000, SPI_MODE0);
40+ BLELocalDevice BLE(&HCISpiTransport);
41+ const int buttonPin = PC13; // set buttonPin to digital pin PC13 */
42+ /* Shield BNRG2A1 with SPI clock on D3 */
43+ /* SPIClass SpiHCI(D11, D12, D3);
44+ HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1);
45+ BLELocalDevice BLE(&HCISpiTransport);
46+ const int buttonPin = PC13; // set buttonPin to digital pin PC13 */
47+ /* Shield BNRG2A1 with SPI clock on D13 */
48+ /* #define SpiHCI SPI
49+ HCISpiTransportClass HCISpiTransport(SpiHCI, BLUENRG_M2SP, A1, A0, D7, 1000000, SPI_MODE1);
50+ BLELocalDevice BLE(&HCISpiTransport);
51+ const int buttonPin = PC13; // set buttonPin to digital pin PC13 */
52+ #endif
53+
2154// variables for button
22- const int buttonPin = 2 ;
2355int oldButtonState = LOW;
56+ int initialButtonState = LOW;
2457
2558void setup () {
26- Serial.begin (9600 );
59+ Serial.begin (115200 );
2760 while (!Serial);
2861
2962 // configure the button pin as input
@@ -32,10 +65,23 @@ void setup() {
3265 // initialize the BLE hardware
3366 BLE.begin ();
3467
68+ // Get initial button state
69+ initialButtonState = digitalRead (buttonPin);
70+ oldButtonState = initialButtonState;
71+
3572 Serial.println (" BLE Central - LED control" );
3673
3774 // start scanning for peripherals
38- BLE.scanForUuid (" 19b10000-e8f2-537e-4f6c-d104768a1214" );
75+ int ret = 1 ;
76+ do
77+ {
78+ ret = BLE.scanForUuid (" 19b10000-e8f2-537e-4f6c-d104768a1214" );
79+ if (ret == 0 )
80+ {
81+ BLE.end ();
82+ BLE.begin ();
83+ }
84+ } while (ret == 0 );
3985}
4086
4187void loop () {
@@ -57,12 +103,30 @@ void loop() {
57103 }
58104
59105 // stop scanning
60- BLE.stopScan ();
106+ int ret = 1 ;
107+ do
108+ {
109+ ret = BLE.stopScan ();
110+ if (ret == 0 )
111+ {
112+ BLE.end ();
113+ BLE.begin ();
114+ }
115+ } while (ret == 0 );
61116
62117 controlLed (peripheral);
63118
64119 // peripheral disconnected, start scanning again
65- BLE.scanForUuid (" 19b10000-e8f2-537e-4f6c-d104768a1214" );
120+ ret = 1 ;
121+ do
122+ {
123+ ret = BLE.scanForUuid (" 19b10000-e8f2-537e-4f6c-d104768a1214" );
124+ if (ret == 0 )
125+ {
126+ BLE.end ();
127+ BLE.begin ();
128+ }
129+ } while (ret == 0 );
66130 }
67131}
68132
@@ -110,7 +174,7 @@ void controlLed(BLEDevice peripheral) {
110174 // button changed
111175 oldButtonState = buttonState;
112176
113- if (buttonState) {
177+ if (buttonState != initialButtonState ) {
114178 Serial.println (" button pressed" );
115179
116180 // button is pressed, write 0x01 to turn the LED on
0 commit comments