Skip to content

Commit 79e0804

Browse files
committed
Add resistance capability for BLE Sense
Adds the functionality to measure resistance with the Nano 33 BLE Sense.
1 parent 1ae793c commit 79e0804

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

examples/Nano33BLESenseFirmware/Nano33BLESenseFirmware.ino

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ const int VERSION = 0x00000001;
1212
const float TEMPERATURE_CALIBRATION = -5.0;
1313

1414
#define SCIENCE_KIT_UUID(val) ("555a0002-" val "-467a-9538-01f0652c74e8")
15+
#define RESISTANCE_PIN A0
16+
#define INPUT_VOLTAGE 3.3
17+
#define VOLTAGE_BUFFER_SIZE 16
1518

1619
//#define DEBUG 0
1720

@@ -26,8 +29,12 @@ BLEFloatCharacteristic humidityCharacteristic (SCIENCE_KIT_UUID("001
2629
BLEUnsignedIntCharacteristic proximityCharacteristic (SCIENCE_KIT_UUID("0017"), BLENotify);
2730
BLECharacteristic colorCharacteristic (SCIENCE_KIT_UUID("0018"), BLENotify, 4 * sizeof(int));
2831
BLEUnsignedShortCharacteristic soundPressureCharacteristic(SCIENCE_KIT_UUID("0019"), BLENotify);
32+
BLEFloatCharacteristic resistanceCharacteristic (SCIENCE_KIT_UUID("0020"), BLENotify);
2933

34+
short voltageBufferIndex = 0;
35+
bool voltageBufferFilled = false;
3036
short soundSampleBuffer[256];
37+
short voltageSampleBuffer[VOLTAGE_BUFFER_SIZE];
3138

3239
void onPDMdata() {
3340
// query the number of bytes available
@@ -45,6 +52,26 @@ uint16_t getSoundAverage() {
4552
return sqrt(avg);
4653
}
4754

55+
void readVoltage() {
56+
voltageSampleBuffer[voltageBufferIndex] = analogRead(RESISTANCE_PIN);
57+
voltageBufferIndex++;
58+
if (!voltageBufferFilled && voltageBufferIndex == VOLTAGE_BUFFER_SIZE) {
59+
voltageBufferFilled = true;
60+
}
61+
voltageBufferIndex=voltageBufferIndex%VOLTAGE_BUFFER_SIZE;
62+
}
63+
64+
uint16_t getVoltageAverage() {
65+
uint32_t avg = 0;
66+
for (int i = 0; i < VOLTAGE_BUFFER_SIZE; i++) {
67+
avg += voltageSampleBuffer[i];
68+
}
69+
if (voltageBufferFilled) {
70+
return avg/VOLTAGE_BUFFER_SIZE;
71+
}
72+
return avg/voltageBufferIndex;
73+
}
74+
4875
// String to calculate the local and device name
4976
String name;
5077
unsigned long lastNotify = 0;
@@ -75,6 +102,8 @@ void setup() {
75102

76103
delay(2000);
77104

105+
pinMode(RESISTANCE_PIN, INPUT); // Used for reading resistance
106+
78107
if (!APDS.begin()) {
79108
printSerialMsg("Failed to initialized APDS!");
80109
blinkLoop();
@@ -142,6 +171,7 @@ void setup() {
142171
service.addCharacteristic(proximityCharacteristic);
143172
service.addCharacteristic(colorCharacteristic);
144173
service.addCharacteristic(soundPressureCharacteristic);
174+
service.addCharacteristic(resistanceCharacteristic);
145175

146176
versionCharacteristic.setValue(VERSION);
147177

@@ -213,4 +243,11 @@ void updateSubscribedCharacteristics() {
213243
float pressure = BARO.readPressure();
214244
pressureCharacteristic.writeValue(pressure);
215245
}
246+
247+
if(resistanceCharacteristic.subscribed()){
248+
readVoltage();
249+
uint16_t measuredValue = getVoltageAverage();
250+
float measuredVoltage = measuredValue / 1024.0f * INPUT_VOLTAGE;
251+
resistanceCharacteristic.writeValue(measuredVoltage);
252+
}
216253
}

0 commit comments

Comments
 (0)