@@ -13,7 +13,6 @@ const float TEMPERATURE_CALIBRATION = -5.0;
13
13
14
14
#define SCIENCE_KIT_UUID (val ) (" 555a0002-" val " -467a-9538-01f0652c74e8" )
15
15
#define RESISTANCE_PIN A0
16
- #define INPUT_VOLTAGE 3.3
17
16
#define VOLTAGE_BUFFER_SIZE 16
18
17
19
18
// #define DEBUG 0
@@ -31,7 +30,7 @@ BLECharacteristic colorCharacteristic (SCIENCE_KIT_UUID("001
31
30
BLEUnsignedShortCharacteristic soundPressureCharacteristic (SCIENCE_KIT_UUID(" 0019" ), BLENotify);
32
31
BLEFloatCharacteristic resistanceCharacteristic (SCIENCE_KIT_UUID(" 0020" ), BLENotify);
33
32
34
- short voltageBufferIndex = 0 ;
33
+ byte voltageBufferIndex = 0 ;
35
34
bool voltageBufferFilled = false ;
36
35
short soundSampleBuffer[256 ];
37
36
short voltageSampleBuffer[VOLTAGE_BUFFER_SIZE];
@@ -53,23 +52,22 @@ uint16_t getSoundAverage() {
53
52
}
54
53
55
54
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 ) {
59
57
voltageBufferFilled = true ;
60
58
}
61
- voltageBufferIndex= voltageBufferIndex% VOLTAGE_BUFFER_SIZE;
59
+ voltageBufferIndex = (++ voltageBufferIndex) % VOLTAGE_BUFFER_SIZE;
62
60
}
63
61
64
62
uint16_t getVoltageAverage () {
65
- uint32_t avg = 0 ;
63
+ uint16_t avg = 0 ;
66
64
for (int i = 0 ; i < VOLTAGE_BUFFER_SIZE; i++) {
67
65
avg += voltageSampleBuffer[i];
68
66
}
69
67
if (voltageBufferFilled) {
70
- return avg/ VOLTAGE_BUFFER_SIZE;
68
+ return avg / VOLTAGE_BUFFER_SIZE;
71
69
}
72
- return avg/ voltageBufferIndex;
70
+ return avg / voltageBufferIndex;
73
71
}
74
72
75
73
// String to calculate the local and device name
@@ -247,7 +245,7 @@ void updateSubscribedCharacteristics() {
247
245
if (resistanceCharacteristic.subscribed ()){
248
246
readVoltage ();
249
247
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 );
252
250
}
253
251
}
0 commit comments