/* LowerPower_Blink_GT832E_01 Turns on an LED on/off to blink at 10Hz Controlled by BLE Uart Disables Advertising most of the time while not connected. Advertising on for 2sec off for 18sec. (c)2022 Forward Computing and Control Pty. Ltd. This example code is in the public domain. */ #include #include int led = 14; // Use P0.14 has the 'led' pin bool ledOn = false; lp_timer ledTimer; const unsigned long DELAY_TIME = 50; // ms = lp_BLESerial ble; lp_timer BLE_AdvertisingRestartTimer; // the advert restart timer const unsigned long BLE_ADVERTISING_ms = 20 * 1000;// restart advertising every 20sec const uint16_t BLE_ADVERTISING_TIMEOUT_secs = 2;// run advertising for 2 seconds // pfodApp needs at least 2sec of advertising to scan and connect void restartAdvertising() { // called by timer every 20seconds ble.setAdvertising(true); // ignored if currently connected } void setup() { pinMode(led, OUTPUT); ble.setName("Led Control"); // set advertised name, default name is "Nordic BLE UART" ble.setAdvertisingTimeout(BLE_ADVERTISING_TIMEOUT_secs); // 2 sec BLE_AdvertisingRestartTimer.startTimer(BLE_ADVERTISING_ms, restartAdvertising); // restart advertising every 20sec for 2sec ble.begin(); // start advertising and be ready to accept connections ledTimer.startTimer(DELAY_TIME, handleLedTimer); } void loop() { sleep(); // just sleep here waiting for the timer/BLE to trigger // check for new BLE cmd 'a' starts blinking, 'b' stops blinking while (ble.available() ) { int i = ble.read(); if ('a' == i) { ledTimer.startTimer(DELAY_TIME, handleLedTimer); // start blinking } else if ('b' == i) { ledTimer.stop(); // stop blinking digitalWrite(led, LOW); // turn off } } } void handleLedTimer() { ledOn = !ledOn; // toggle state if (ledOn) { digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) } else { digitalWrite(led, LOW); // turn the LED off by making the voltage LOW } }