From 79e080418147238025c54d2452966fd0effc2ffa Mon Sep 17 00:00:00 2001 From: Manuel Zomer <47141795+manuelzomer@users.noreply.github.com> Date: Tue, 24 Nov 2020 13:44:49 +0100 Subject: [PATCH 1/3] Add resistance capability for BLE Sense Adds the functionality to measure resistance with the Nano 33 BLE Sense. --- .../Nano33BLESenseFirmware.ino | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/examples/Nano33BLESenseFirmware/Nano33BLESenseFirmware.ino b/examples/Nano33BLESenseFirmware/Nano33BLESenseFirmware.ino index 8375a67..609c2b0 100644 --- a/examples/Nano33BLESenseFirmware/Nano33BLESenseFirmware.ino +++ b/examples/Nano33BLESenseFirmware/Nano33BLESenseFirmware.ino @@ -12,6 +12,9 @@ const int VERSION = 0x00000001; const float TEMPERATURE_CALIBRATION = -5.0; #define SCIENCE_KIT_UUID(val) ("555a0002-" val "-467a-9538-01f0652c74e8") +#define RESISTANCE_PIN A0 +#define INPUT_VOLTAGE 3.3 +#define VOLTAGE_BUFFER_SIZE 16 //#define DEBUG 0 @@ -26,8 +29,12 @@ BLEFloatCharacteristic humidityCharacteristic (SCIENCE_KIT_UUID("001 BLEUnsignedIntCharacteristic proximityCharacteristic (SCIENCE_KIT_UUID("0017"), BLENotify); BLECharacteristic colorCharacteristic (SCIENCE_KIT_UUID("0018"), BLENotify, 4 * sizeof(int)); BLEUnsignedShortCharacteristic soundPressureCharacteristic(SCIENCE_KIT_UUID("0019"), BLENotify); +BLEFloatCharacteristic resistanceCharacteristic (SCIENCE_KIT_UUID("0020"), BLENotify); +short voltageBufferIndex = 0; +bool voltageBufferFilled = false; short soundSampleBuffer[256]; +short voltageSampleBuffer[VOLTAGE_BUFFER_SIZE]; void onPDMdata() { // query the number of bytes available @@ -45,6 +52,26 @@ uint16_t getSoundAverage() { return sqrt(avg); } +void readVoltage() { + voltageSampleBuffer[voltageBufferIndex] = analogRead(RESISTANCE_PIN); + voltageBufferIndex++; + if (!voltageBufferFilled && voltageBufferIndex == VOLTAGE_BUFFER_SIZE) { + voltageBufferFilled = true; + } + voltageBufferIndex=voltageBufferIndex%VOLTAGE_BUFFER_SIZE; +} + +uint16_t getVoltageAverage() { + uint32_t avg = 0; + for (int i = 0; i < VOLTAGE_BUFFER_SIZE; i++) { + avg += voltageSampleBuffer[i]; + } + if (voltageBufferFilled) { + return avg/VOLTAGE_BUFFER_SIZE; + } + return avg/voltageBufferIndex; +} + // String to calculate the local and device name String name; unsigned long lastNotify = 0; @@ -75,6 +102,8 @@ void setup() { delay(2000); + pinMode(RESISTANCE_PIN, INPUT); // Used for reading resistance + if (!APDS.begin()) { printSerialMsg("Failed to initialized APDS!"); blinkLoop(); @@ -142,6 +171,7 @@ void setup() { service.addCharacteristic(proximityCharacteristic); service.addCharacteristic(colorCharacteristic); service.addCharacteristic(soundPressureCharacteristic); + service.addCharacteristic(resistanceCharacteristic); versionCharacteristic.setValue(VERSION); @@ -213,4 +243,11 @@ void updateSubscribedCharacteristics() { float pressure = BARO.readPressure(); pressureCharacteristic.writeValue(pressure); } + + if(resistanceCharacteristic.subscribed()){ + readVoltage(); + uint16_t measuredValue = getVoltageAverage(); + float measuredVoltage = measuredValue / 1024.0f * INPUT_VOLTAGE; + resistanceCharacteristic.writeValue(measuredVoltage); + } } From 11b095d7e13bd54ff09362544844776ebdb947ea Mon Sep 17 00:00:00 2001 From: Manuel Zomer <47141795+manuelzomer@users.noreply.github.com> Date: Fri, 27 Nov 2020 16:13:35 +0100 Subject: [PATCH 2/3] Use Voltage Ratio For Resistance Use voltage ratio instead of absolute voltage for the resistance calculation. --- .../Nano33BLESenseFirmware.ino | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/examples/Nano33BLESenseFirmware/Nano33BLESenseFirmware.ino b/examples/Nano33BLESenseFirmware/Nano33BLESenseFirmware.ino index 609c2b0..5466f36 100644 --- a/examples/Nano33BLESenseFirmware/Nano33BLESenseFirmware.ino +++ b/examples/Nano33BLESenseFirmware/Nano33BLESenseFirmware.ino @@ -13,7 +13,6 @@ const float TEMPERATURE_CALIBRATION = -5.0; #define SCIENCE_KIT_UUID(val) ("555a0002-" val "-467a-9538-01f0652c74e8") #define RESISTANCE_PIN A0 -#define INPUT_VOLTAGE 3.3 #define VOLTAGE_BUFFER_SIZE 16 //#define DEBUG 0 @@ -31,7 +30,7 @@ BLECharacteristic colorCharacteristic (SCIENCE_KIT_UUID("001 BLEUnsignedShortCharacteristic soundPressureCharacteristic(SCIENCE_KIT_UUID("0019"), BLENotify); BLEFloatCharacteristic resistanceCharacteristic (SCIENCE_KIT_UUID("0020"), BLENotify); -short voltageBufferIndex = 0; +byte voltageBufferIndex = 0; bool voltageBufferFilled = false; short soundSampleBuffer[256]; short voltageSampleBuffer[VOLTAGE_BUFFER_SIZE]; @@ -53,23 +52,22 @@ uint16_t getSoundAverage() { } void readVoltage() { - voltageSampleBuffer[voltageBufferIndex] = analogRead(RESISTANCE_PIN); - voltageBufferIndex++; - if (!voltageBufferFilled && voltageBufferIndex == VOLTAGE_BUFFER_SIZE) { + voltageSampleBuffer[voltageBufferIndex] = analogRead(RESISTANCE_PIN); + if (!voltageBufferFilled && voltageBufferIndex == VOLTAGE_BUFFER_SIZE - 1) { voltageBufferFilled = true; } - voltageBufferIndex=voltageBufferIndex%VOLTAGE_BUFFER_SIZE; + voltageBufferIndex = (++voltageBufferIndex) % VOLTAGE_BUFFER_SIZE; } uint16_t getVoltageAverage() { - uint32_t avg = 0; + uint16_t avg = 0; for (int i = 0; i < VOLTAGE_BUFFER_SIZE; i++) { avg += voltageSampleBuffer[i]; } if (voltageBufferFilled) { - return avg/VOLTAGE_BUFFER_SIZE; + return avg / VOLTAGE_BUFFER_SIZE; } - return avg/voltageBufferIndex; + return avg / voltageBufferIndex; } // String to calculate the local and device name @@ -247,7 +245,7 @@ void updateSubscribedCharacteristics() { if(resistanceCharacteristic.subscribed()){ readVoltage(); uint16_t measuredValue = getVoltageAverage(); - float measuredVoltage = measuredValue / 1024.0f * INPUT_VOLTAGE; - resistanceCharacteristic.writeValue(measuredVoltage); + float voltageRatio = 1024.0f / measuredValue; + resistanceCharacteristic.writeValue(voltageRatio); } } From f1a5ed63cfdd8d62f7704c04053a19bbf750be6e Mon Sep 17 00:00:00 2001 From: Manuel Zomer <47141795+manuelzomer@users.noreply.github.com> Date: Fri, 4 Dec 2020 14:58:44 +0100 Subject: [PATCH 3/3] Update Resistance Buffer Reading If the resistance buffer is not completely filled yet, the buffer reading gets adapted. --- .../Nano33BLESenseFirmware/Nano33BLESenseFirmware.ino | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/examples/Nano33BLESenseFirmware/Nano33BLESenseFirmware.ino b/examples/Nano33BLESenseFirmware/Nano33BLESenseFirmware.ino index 5466f36..3cdd20e 100644 --- a/examples/Nano33BLESenseFirmware/Nano33BLESenseFirmware.ino +++ b/examples/Nano33BLESenseFirmware/Nano33BLESenseFirmware.ino @@ -61,13 +61,11 @@ void readVoltage() { uint16_t getVoltageAverage() { uint16_t avg = 0; - for (int i = 0; i < VOLTAGE_BUFFER_SIZE; i++) { + byte upperBound = voltageBufferFilled ? VOLTAGE_BUFFER_SIZE : voltageBufferIndex; + for (int i = 0; i < upperBound; i++) { avg += voltageSampleBuffer[i]; } - if (voltageBufferFilled) { - return avg / VOLTAGE_BUFFER_SIZE; - } - return avg / voltageBufferIndex; + return avg / upperBound; } // String to calculate the local and device name