/* lp_BLE_compartor This sketch sets up a Nordic BLE UART and sends the pin input level Above/Below reference level on each change (c)2018 Forward Computing and Control Pty. Ltd. This example code is in the public domain. */ #include #include // Pin 13 has an LED connected on most Arduino boards, including NanoV2 int led = 13; int comparatorPin = 2; // D2/A2 lp_BLESerial ble; void setup() { pinMode(comparatorPin, INPUT_PULLDOWN); // set compare pin with INPUT_PULLUP or INPUT_PULLDOWN to prevent floating pin triggers // but the internal pullup/pulldown resistor is ~13K which draws an extra 254uA when INPUT_PULLUP is grounded or INPUT_PULLDOWN is connected to Vdd // for very low power use pinMode(2, INPUT) and supply a high value external pullup or pulldown resistor e.g. 100K draws ~33uA // ADVERTISING ~90uA, CONNECTED ~90uA when pin input grounded. i.e. LED off and no current through pull-down resistor pinMode(led, OUTPUT); // initialize the digital pin as an output. ble.setName("Pin Change"); // set advertised name, default name is "Nordic BLE UART" ble.begin(); // start advertising and be ready to accept connections lp_comparator_start(comparatorPin, REF_8_16Vdd, handlePinLevelChange); // always triggers pin LOW first, then if pin HIGH, will trigger HIGH } void loop() { sleep(); // just sleep here waiting a trigger while (ble.available()) { int i = ble.read(); // clear BLE recieve buffer and ignore } } // called when pin changes state, pinState is state detected, // HIGH when Above and LOW when Below the reference voltage void handlePinLevelChange(int pinState) { if (ble.isConnected()) { ble.print(millis()); ble.print(','); ble.print((pinState == HIGH) ? 'H' : 'L'); ble.println(); } digitalWrite(led, pinState); // turn the LED on when Above }