6
6
it will remotely control the BLE Peripheral's LED, when the button is pressed or released.
7
7
8
8
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
12
10
13
11
You can use it with another board that is compatible with this library and the
14
12
Peripherals -> LED example.
18
16
19
17
#include < ArduinoBLE.h>
20
18
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
+
21
54
// variables for button
22
- const int buttonPin = 2 ;
23
55
int oldButtonState = LOW;
56
+ int initialButtonState = LOW;
24
57
25
58
void setup () {
26
- Serial.begin (9600 );
59
+ Serial.begin (115200 );
27
60
while (!Serial);
28
61
29
62
// configure the button pin as input
@@ -32,10 +65,23 @@ void setup() {
32
65
// initialize the BLE hardware
33
66
BLE.begin ();
34
67
68
+ // Get initial button state
69
+ initialButtonState = digitalRead (buttonPin);
70
+ oldButtonState = initialButtonState;
71
+
35
72
Serial.println (" BLE Central - LED control" );
36
73
37
74
// 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 );
39
85
}
40
86
41
87
void loop () {
@@ -57,12 +103,30 @@ void loop() {
57
103
}
58
104
59
105
// 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 );
61
116
62
117
controlLed (peripheral);
63
118
64
119
// 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 );
66
130
}
67
131
}
68
132
@@ -110,7 +174,7 @@ void controlLed(BLEDevice peripheral) {
110
174
// button changed
111
175
oldButtonState = buttonState;
112
176
113
- if (buttonState) {
177
+ if (buttonState != initialButtonState ) {
114
178
Serial.println (" button pressed" );
115
179
116
180
// button is pressed, write 0x01 to turn the LED on
0 commit comments