Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- LED
- This example creates a BLE peripheral with service that contains a
- characteristic to control an LED.
- The circuit:
- - Arduino MKR WiFi 1010 or Arduino Uno WiFi Rev2 board
- You can use a generic BLE 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.
- */
- // https://learn.adafruit.com/multi-tasking-the-arduino-part-3
- #include <ArduinoBLE.h>
- BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service
- // BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
- BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
- BLEByteCharacteristic bluLedCharacteristic("73b63d33-0f5c-4405-8f19-7e82daae36c3", BLERead | BLEWrite);
- BLEByteCharacteristic redLedCharacteristic("52949dd9-bcdc-449b-b9ac-80cd9724477f", BLERead | BLEWrite);
- BLEByteCharacteristic greenLedCharacteristic("73d3a000-390c-4cfc-b647-117e98c7f517", BLERead | BLEWrite);
- const int ledPin = LED_BUILTIN; // pin to use for the LED
- const int bluLedPin = 6;
- const int redLedPin = 8;
- const int greenLedPin = 10;
- class Flasher
- {
- // Class Member Variables
- // These are initialized at startup
- int ledPin; // the number of the LED pin
- long OnTime; // milliseconds of on-time
- long OffTime; // milliseconds of off-time
- // These maintain the current state
- int ledState; // ledState used to set the LED
- unsigned long previousMillis; // will store last time LED was updated
- // Constructor - creates a Flasher
- // and initializes the member variables and state
- public:
- Flasher(int pin, long on, long off)
- {
- ledPin = pin;
- pinMode(ledPin, OUTPUT);
- OnTime = on;
- OffTime = off;
- ledState = LOW;
- previousMillis = 0;
- }
- void Update()
- {
- // check to see if it's time to change the state of the LED
- unsigned long currentMillis = millis();
- if((ledState == HIGH) && (currentMillis - previousMillis >= OnTime))
- {
- ledState = LOW; // Turn it off
- previousMillis = currentMillis; // Remember the time
- digitalWrite(ledPin, ledState); // Update the actual LED
- }
- else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime))
- {
- ledState = HIGH; // turn it on
- previousMillis = currentMillis; // Remember the time
- digitalWrite(ledPin, ledState); // Update the actual LED
- }
- }
- };
- Flasher redFlash(redLedPin, 100, 400);
- Flasher greenFlash(greenLedPin, 350, 350);
- bool redLedStatus = false;
- bool greenLedStatus = false;
- void setup() {
- Serial.begin(9600);
- while (!Serial);
- // set LED pin to output mode
- pinMode(ledPin, OUTPUT);
- pinMode(bluLedPin, OUTPUT);
- // pinMode(redLedPin, OUTPUT);
- // pinMode(greenLedPin, OUTPUT);
- // begin initialization
- if (!BLE.begin()) {
- Serial.println("starting BLE failed!");
- while (1);
- }
- // set advertised local name and service UUID:
- BLE.setLocalName("MKR1010 - BLE LED service");
- BLE.setAdvertisedService(ledService);
- // add the characteristic to the service
- ledService.addCharacteristic(switchCharacteristic);
- ledService.addCharacteristic(bluLedCharacteristic);
- ledService.addCharacteristic(redLedCharacteristic);
- ledService.addCharacteristic(greenLedCharacteristic);
- // add service
- BLE.addService(ledService);
- // set the initial value for the characeristic:
- switchCharacteristic.writeValue(0);
- bluLedCharacteristic.writeValue(0);
- redLedCharacteristic.writeValue(0);
- greenLedCharacteristic.writeValue(0);
- // start advertising
- BLE.advertise();
- Serial.println("BLE LED Peripheral");
- }
- void loop() {
- // listen for BLE peripherals to connect:
- BLEDevice central = BLE.central();
- // if a central is connected to peripheral:
- if (central) {
- Serial.print("Connected to central: ");
- // print the central's MAC address:
- Serial.println(central.address());
- // while the central is still connected to peripheral:
- while (central.connected()) {
- // if the remote device wrote to the characteristic,
- // use the value to control the LED:
- if (switchCharacteristic.written()) {
- if (switchCharacteristic.value()) { // any value other than 0
- Serial.println("LED on");
- digitalWrite(ledPin, HIGH); // will turn the LED on
- } else { // a 0 value
- Serial.println(F("LED off"));
- digitalWrite(ledPin, LOW); // will turn the LED off
- }
- }
- // bluLed
- if (bluLedCharacteristic.written()) {
- if (bluLedCharacteristic.value()) { // any value other than 0
- Serial.println("BLU LED on");
- digitalWrite(bluLedPin, HIGH); // will turn the LED on
- } else { // a 0 value
- Serial.println(F("BLU LED off"));
- digitalWrite(bluLedPin, LOW); // will turn the LED off
- }
- }
- // redLed
- if (redLedCharacteristic.written()) {
- if (redLedCharacteristic.value() > 3) { // any value other than 0
- //Serial.println("RED LED on");
- //digitalWrite(redLedPin, HIGH); // will turn the LED on
- //Serial.println("RED LED Flasher update");
- //led1.Update();
- } else { // a 0 value
- //Serial.println(F("RED LED off"));
- //digitalWrite(redLedPin, LOW); // will turn the LED off
- }
- }
- // greenLed
- if (greenLedCharacteristic.written()) {
- if (greenLedCharacteristic.value()) { // any value other than 0
- Serial.println("GREEN LED flashing");
- //digitalWrite(greenLedPin, HIGH); // will turn the LED on
- greenLedStatus = true;
- } else { // a 0 value
- Serial.println(F("GREEN LED off"));
- // digitalWrite(greenLedPin, LOW); // will turn the LED off
- greenLedStatus = false;
- }
- }
- }
- // when the central disconnects, print it out:
- Serial.print(F("Disconnected from central: "));
- Serial.println(central.address());
- }
- redFlash.Update();
- greenFlash.Update();
- Serial.println(F("post Update()"));
- /*
- if (greenLedStatus == true) {
- greenLedFlash.Update();
- }
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement