Skip to content

Commit 11b095d

Browse files
committed
Use Voltage Ratio For Resistance
Use voltage ratio instead of absolute voltage for the resistance calculation.
1 parent 79e0804 commit 11b095d

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

examples/Nano33BLESenseFirmware/Nano33BLESenseFirmware.ino

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const float TEMPERATURE_CALIBRATION = -5.0;
1313

1414
#define SCIENCE_KIT_UUID(val) ("555a0002-" val "-467a-9538-01f0652c74e8")
1515
#define RESISTANCE_PIN A0
16-
#define INPUT_VOLTAGE 3.3
1716
#define VOLTAGE_BUFFER_SIZE 16
1817

1918
//#define DEBUG 0
@@ -31,7 +30,7 @@ BLECharacteristic colorCharacteristic (SCIENCE_KIT_UUID("001
3130
BLEUnsignedShortCharacteristic soundPressureCharacteristic(SCIENCE_KIT_UUID("0019"), BLENotify);
3231
BLEFloatCharacteristic resistanceCharacteristic (SCIENCE_KIT_UUID("0020"), BLENotify);
3332

34-
short voltageBufferIndex = 0;
33+
byte voltageBufferIndex = 0;
3534
bool voltageBufferFilled = false;
3635
short soundSampleBuffer[256];
3736
short voltageSampleBuffer[VOLTAGE_BUFFER_SIZE];
@@ -53,23 +52,22 @@ uint16_t getSoundAverage() {
5352
}
5453

5554
void readVoltage() {
56-
voltageSampleBuffer[voltageBufferIndex] = analogRead(RESISTANCE_PIN);
57-
voltageBufferIndex++;
58-
if (!voltageBufferFilled && voltageBufferIndex == VOLTAGE_BUFFER_SIZE) {
55+
voltageSampleBuffer[voltageBufferIndex] = analogRead(RESISTANCE_PIN);
56+
if (!voltageBufferFilled && voltageBufferIndex == VOLTAGE_BUFFER_SIZE - 1) {
5957
voltageBufferFilled = true;
6058
}
61-
voltageBufferIndex=voltageBufferIndex%VOLTAGE_BUFFER_SIZE;
59+
voltageBufferIndex = (++voltageBufferIndex) % VOLTAGE_BUFFER_SIZE;
6260
}
6361

6462
uint16_t getVoltageAverage() {
65-
uint32_t avg = 0;
63+
uint16_t avg = 0;
6664
for (int i = 0; i < VOLTAGE_BUFFER_SIZE; i++) {
6765
avg += voltageSampleBuffer[i];
6866
}
6967
if (voltageBufferFilled) {
70-
return avg/VOLTAGE_BUFFER_SIZE;
68+
return avg / VOLTAGE_BUFFER_SIZE;
7169
}
72-
return avg/voltageBufferIndex;
70+
return avg / voltageBufferIndex;
7371
}
7472

7573
// String to calculate the local and device name
@@ -247,7 +245,7 @@ void updateSubscribedCharacteristics() {
247245
if(resistanceCharacteristic.subscribed()){
248246
readVoltage();
249247
uint16_t measuredValue = getVoltageAverage();
250-
float measuredVoltage = measuredValue / 1024.0f * INPUT_VOLTAGE;
251-
resistanceCharacteristic.writeValue(measuredVoltage);
248+
float voltageRatio = 1024.0f / measuredValue;
249+
resistanceCharacteristic.writeValue(voltageRatio);
252250
}
253251
}

0 commit comments

Comments
 (0)