Skip to content

Commit b3c0e0b

Browse files
authored
Merge pull request #7 from manuelzomer/resistance
Added functionality to measure resistance
2 parents 2064958 + f1a5ed6 commit b3c0e0b

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

examples/Nano33BLESenseFirmware/Nano33BLESenseFirmware.ino

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const int VERSION = 0x00000001;
1313
const float TEMPERATURE_CALIBRATION = -5.0;
1414

1515
#define SCIENCE_KIT_UUID(val) ("555a0002-" val "-467a-9538-01f0652c74e8")
16+
#define RESISTANCE_PIN A0
17+
#define VOLTAGE_BUFFER_SIZE 16
1618

1719
//#define DEBUG 0
1820

@@ -27,8 +29,12 @@ BLEFloatCharacteristic humidityCharacteristic (SCIENCE_KIT_UUID("001
2729
BLEUnsignedIntCharacteristic proximityCharacteristic (SCIENCE_KIT_UUID("0017"), BLENotify);
2830
BLECharacteristic colorCharacteristic (SCIENCE_KIT_UUID("0018"), BLENotify, 4 * sizeof(int));
2931
BLEUnsignedShortCharacteristic soundPressureCharacteristic(SCIENCE_KIT_UUID("0019"), BLENotify);
32+
BLEFloatCharacteristic resistanceCharacteristic (SCIENCE_KIT_UUID("0020"), BLENotify);
3033

34+
byte voltageBufferIndex = 0;
35+
bool voltageBufferFilled = false;
3136
short soundSampleBuffer[256];
37+
short voltageSampleBuffer[VOLTAGE_BUFFER_SIZE];
3238

3339
void onPDMdata() {
3440
// query the number of bytes available
@@ -46,6 +52,23 @@ uint16_t getSoundAverage() {
4652
return sqrt(avg);
4753
}
4854

55+
void readVoltage() {
56+
voltageSampleBuffer[voltageBufferIndex] = analogRead(RESISTANCE_PIN);
57+
if (!voltageBufferFilled && voltageBufferIndex == VOLTAGE_BUFFER_SIZE - 1) {
58+
voltageBufferFilled = true;
59+
}
60+
voltageBufferIndex = (++voltageBufferIndex) % VOLTAGE_BUFFER_SIZE;
61+
}
62+
63+
uint16_t getVoltageAverage() {
64+
uint16_t avg = 0;
65+
byte upperBound = voltageBufferFilled ? VOLTAGE_BUFFER_SIZE : voltageBufferIndex;
66+
for (int i = 0; i < upperBound; i++) {
67+
avg += voltageSampleBuffer[i];
68+
}
69+
return avg / upperBound;
70+
}
71+
4972
// String to calculate the local and device name
5073
String name;
5174
unsigned long lastNotify = 0;
@@ -76,6 +99,8 @@ void setup() {
7699

77100
delay(2000);
78101

102+
pinMode(RESISTANCE_PIN, INPUT); // Used for reading resistance
103+
79104
if (!APDS.begin()) {
80105
printSerialMsg("Failed to initialized APDS!");
81106
blinkLoop();
@@ -143,6 +168,7 @@ void setup() {
143168
service.addCharacteristic(proximityCharacteristic);
144169
service.addCharacteristic(colorCharacteristic);
145170
service.addCharacteristic(soundPressureCharacteristic);
171+
service.addCharacteristic(resistanceCharacteristic);
146172

147173
versionCharacteristic.setValue(VERSION);
148174

@@ -212,4 +238,11 @@ void updateSubscribedCharacteristics() {
212238
float pressure = BARO.readPressure();
213239
pressureCharacteristic.writeValue(pressure);
214240
}
241+
242+
if(resistanceCharacteristic.subscribed()){
243+
readVoltage();
244+
uint16_t measuredValue = getVoltageAverage();
245+
float voltageRatio = 1024.0f / measuredValue;
246+
resistanceCharacteristic.writeValue(voltageRatio);
247+
}
215248
}

0 commit comments

Comments
 (0)