ArduinoBLE/o At Master · Arduino-libraries ... - GitHub

Skip to content Dismiss alert {{ message }} / ArduinoBLE Public
  • Notifications You must be signed in to change notification settings
  • Fork 232
  • Star 349
  • Code
  • Issues 114
  • Pull requests 23
  • Actions
  • Projects
  • Security
  • Insights
Additional navigation options

Files

 master

Breadcrumbs

  1. ArduinoBLE
  2. examples
  3. Peripheral
  4. ButtonLED
ButtonLED.inoBlameBlame

Latest commit

 

History

History93 lines (70 loc) · 2.84 KB master
  1. ArduinoBLE
  2. examples
  3. Peripheral
  4. ButtonLED
ButtonLED.inoTop

File metadata and controls

  • Code
  • Blame
93 lines (70 loc) · 2.84 KBRaw123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293/* Button LED This example creates a Bluetooth® Low Energy peripheral with service that contains a characteristic to control an LED and another characteristic that represents the state of the button. The circuit: - Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT, Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board. - Button connected to pin 4 You can use a generic Bluetooth® Low Energy central app, like LightBlue (iOS and Android) or nRF Connect (Android), to interact with the services and characteristics created in this sketch. This example code is in the public domain.*/ #include <ArduinoBLE.h> const int ledPin = LED_BUILTIN; // set ledPin to on-board LEDconst int buttonPin = 4; // set buttonPin to digital pin 4 BLEService ledService("19B10010-E8F2-537E-4F6C-D104768A1214"); // create service // create switch characteristic and allow remote device to read and writeBLEByteCharacteristic ledCharacteristic("19B10011-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);// create button characteristic and allow remote device to get notificationsBLEByteCharacteristic buttonCharacteristic("19B10012-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify); void setup() { Serial.begin(9600); while (!Serial); pinMode(ledPin, OUTPUT); // use the LED as an output pinMode(buttonPin, INPUT); // use button pin as an input // begin initialization if (!BLE.begin()) { Serial.println("starting Bluetooth® Low Energy module failed!"); while (1); } // set the local name peripheral advertises BLE.setLocalName("ButtonLED"); // set the UUID for the service this peripheral advertises: BLE.setAdvertisedService(ledService); // add the characteristics to the service ledService.addCharacteristic(ledCharacteristic); ledService.addCharacteristic(buttonCharacteristic); // add the service BLE.addService(ledService); ledCharacteristic.writeValue(0); buttonCharacteristic.writeValue(0); // start advertising BLE.advertise(); Serial.println("Bluetooth® device active, waiting for connections...");} void loop() { // poll for Bluetooth® Low Energy events BLE.poll(); // read the current button pin state char buttonValue = digitalRead(buttonPin); // has the value changed since the last read bool buttonChanged = (buttonCharacteristic.value() != buttonValue); if (buttonChanged) { // button state changed, update characteristics ledCharacteristic.writeValue(buttonValue); buttonCharacteristic.writeValue(buttonValue); } if (ledCharacteristic.written() || buttonChanged) { // update LED, either central has written to characteristic or button state has changed if (ledCharacteristic.value()) { Serial.println("LED on"); digitalWrite(ledPin, HIGH); } else { Serial.println("LED off"); digitalWrite(ledPin, LOW); } }} You can’t perform that action at this time.

Tag » Arduino Uno Wifi Rev2 Ble Example