Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.65 KB | None | 0 0
  1. /*
  2.   LED
  3.  
  4.   This example creates a BLE peripheral with service that contains a
  5.   characteristic to control an LED.
  6.  
  7.   The circuit:
  8.   - Arduino MKR WiFi 1010 or Arduino Uno WiFi Rev2 board
  9.  
  10.   You can use a generic BLE central app, like LightBlue (iOS and Android) or
  11.   nRF Connect (Android), to interact with the services and characteristics
  12.   created in this sketch.
  13.  
  14.   This example code is in the public domain.
  15. */
  16.  
  17. // https://learn.adafruit.com/multi-tasking-the-arduino-part-3
  18.  
  19.  
  20.  
  21. #include <ArduinoBLE.h>
  22.  
  23. BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service
  24.  
  25. // BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
  26. BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
  27.  
  28. BLEByteCharacteristic bluLedCharacteristic("73b63d33-0f5c-4405-8f19-7e82daae36c3", BLERead | BLEWrite);
  29. BLEByteCharacteristic redLedCharacteristic("52949dd9-bcdc-449b-b9ac-80cd9724477f", BLERead | BLEWrite);
  30. BLEByteCharacteristic greenLedCharacteristic("73d3a000-390c-4cfc-b647-117e98c7f517", BLERead | BLEWrite);
  31.  
  32.  
  33.  
  34. const int ledPin = LED_BUILTIN; // pin to use for the LED
  35. const int bluLedPin = 6;
  36. const int redLedPin = 8;
  37. const int greenLedPin = 10;
  38.  
  39.  
  40. class Flasher
  41. {
  42.   // Class Member Variables
  43.     // These are initialized at startup
  44.   int ledPin;      // the number of the LED pin
  45.   long OnTime;     // milliseconds of on-time
  46.   long OffTime;    // milliseconds of off-time
  47.  
  48.   // These maintain the current state
  49.   int ledState;                 // ledState used to set the LED
  50.   unsigned long previousMillis;   // will store last time LED was updated
  51.  
  52.   // Constructor - creates a Flasher
  53.   // and initializes the member variables and state
  54.   public:
  55.   Flasher(int pin, long on, long off)
  56.   {
  57.   ledPin = pin;
  58.   pinMode(ledPin, OUTPUT);    
  59.    
  60.   OnTime = on;
  61.   OffTime = off;
  62.  
  63.   ledState = LOW;
  64.   previousMillis = 0;
  65.   }
  66.   void Update()
  67.   {
  68.     // check to see if it's time to change the state of the LED
  69.     unsigned long currentMillis = millis();
  70.      
  71.     if((ledState == HIGH) && (currentMillis - previousMillis >= OnTime))
  72.     {
  73.       ledState = LOW;  // Turn it off
  74.       previousMillis = currentMillis;  // Remember the time
  75.       digitalWrite(ledPin, ledState);  // Update the actual LED
  76.     }
  77.     else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime))
  78.     {
  79.       ledState = HIGH;  // turn it on
  80.       previousMillis = currentMillis;   // Remember the time
  81.       digitalWrite(ledPin, ledState);   // Update the actual LED
  82.     }
  83.   }
  84. };
  85.  
  86.  
  87. Flasher redFlash(redLedPin, 100, 400);
  88. Flasher greenFlash(greenLedPin, 350, 350);
  89.  
  90. bool redLedStatus = false;
  91. bool greenLedStatus = false;
  92.  
  93. void setup() {
  94.   Serial.begin(9600);
  95.   while (!Serial);
  96.  
  97.   // set LED pin to output mode
  98.   pinMode(ledPin, OUTPUT);
  99.  
  100.   pinMode(bluLedPin, OUTPUT);
  101.   // pinMode(redLedPin, OUTPUT);
  102.   // pinMode(greenLedPin, OUTPUT);
  103.  
  104.   // begin initialization
  105.   if (!BLE.begin()) {
  106.     Serial.println("starting BLE failed!");
  107.  
  108.     while (1);
  109.   }
  110.  
  111.   // set advertised local name and service UUID:
  112.   BLE.setLocalName("MKR1010 - BLE LED service");
  113.   BLE.setAdvertisedService(ledService);
  114.  
  115.   // add the characteristic to the service
  116.   ledService.addCharacteristic(switchCharacteristic);
  117.  
  118.   ledService.addCharacteristic(bluLedCharacteristic);
  119.   ledService.addCharacteristic(redLedCharacteristic);
  120.   ledService.addCharacteristic(greenLedCharacteristic);
  121.  
  122.   // add service
  123.   BLE.addService(ledService);
  124.  
  125.   // set the initial value for the characeristic:
  126.   switchCharacteristic.writeValue(0);
  127.  
  128.   bluLedCharacteristic.writeValue(0);
  129.   redLedCharacteristic.writeValue(0);
  130.   greenLedCharacteristic.writeValue(0);
  131.  
  132.   // start advertising
  133.   BLE.advertise();
  134.  
  135.   Serial.println("BLE LED Peripheral");
  136. }
  137.  
  138. void loop() {
  139.   // listen for BLE peripherals to connect:
  140.   BLEDevice central = BLE.central();
  141.  
  142.   // if a central is connected to peripheral:
  143.   if (central) {
  144.     Serial.print("Connected to central: ");
  145.     // print the central's MAC address:
  146.     Serial.println(central.address());
  147.  
  148.     // while the central is still connected to peripheral:
  149.     while (central.connected()) {
  150.       // if the remote device wrote to the characteristic,
  151.       // use the value to control the LED:
  152.       if (switchCharacteristic.written()) {
  153.         if (switchCharacteristic.value()) {   // any value other than 0
  154.           Serial.println("LED on");
  155.           digitalWrite(ledPin, HIGH);         // will turn the LED on
  156.         } else {                              // a 0 value
  157.           Serial.println(F("LED off"));
  158.           digitalWrite(ledPin, LOW);          // will turn the LED off
  159.         }
  160.       }
  161.  
  162.       // bluLed
  163.       if (bluLedCharacteristic.written()) {
  164.         if (bluLedCharacteristic.value()) {   // any value other than 0
  165.           Serial.println("BLU LED on");
  166.           digitalWrite(bluLedPin, HIGH);         // will turn the LED on
  167.         } else {                              // a 0 value
  168.           Serial.println(F("BLU LED off"));
  169.           digitalWrite(bluLedPin, LOW);          // will turn the LED off
  170.         }
  171.       }
  172.  
  173.  
  174.       // redLed
  175.       if (redLedCharacteristic.written()) {
  176.         if (redLedCharacteristic.value() > 3) {   // any value other than 0
  177.           //Serial.println("RED LED on");
  178.           //digitalWrite(redLedPin, HIGH);         // will turn the LED on
  179.           //Serial.println("RED LED Flasher update");
  180.           //led1.Update();
  181.                  
  182.         } else {                              // a 0 value
  183.           //Serial.println(F("RED LED off"));
  184.           //digitalWrite(redLedPin, LOW);          // will turn the LED off
  185.         }
  186.       }
  187.  
  188.       // greenLed
  189.       if (greenLedCharacteristic.written()) {
  190.         if (greenLedCharacteristic.value()) {   // any value other than 0
  191.          
  192.           Serial.println("GREEN LED flashing");
  193.           //digitalWrite(greenLedPin, HIGH);         // will turn the LED on
  194.          
  195.           greenLedStatus = true;
  196.          
  197.         } else {                              // a 0 value
  198.           Serial.println(F("GREEN LED off"));
  199.           // digitalWrite(greenLedPin, LOW);          // will turn the LED off
  200.          
  201.           greenLedStatus = false;
  202.          
  203.         }
  204.       }
  205.      
  206.      
  207.     }
  208.  
  209.     // when the central disconnects, print it out:
  210.     Serial.print(F("Disconnected from central: "));
  211.     Serial.println(central.address());
  212.   }
  213.  
  214.   redFlash.Update();
  215.   greenFlash.Update();
  216.   Serial.println(F("post Update()"));
  217.  
  218.  
  219. /*
  220.   if (greenLedStatus == true) {
  221.    
  222.       greenLedFlash.Update();
  223.    
  224.     }
  225. */
  226.  
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement