@@ -13,6 +13,8 @@ const int VERSION = 0x00000001;
13
13
const float TEMPERATURE_CALIBRATION = -5.0 ;
14
14
15
15
#define SCIENCE_KIT_UUID (val ) (" 555a0002-" val " -467a-9538-01f0652c74e8" )
16
+ #define RESISTANCE_PIN A0
17
+ #define VOLTAGE_BUFFER_SIZE 16
16
18
17
19
// #define DEBUG 0
18
20
@@ -27,8 +29,12 @@ BLEFloatCharacteristic humidityCharacteristic (SCIENCE_KIT_UUID("001
27
29
BLEUnsignedIntCharacteristic proximityCharacteristic (SCIENCE_KIT_UUID(" 0017" ), BLENotify);
28
30
BLECharacteristic colorCharacteristic (SCIENCE_KIT_UUID(" 0018" ), BLENotify, 4 * sizeof(int ));
29
31
BLEUnsignedShortCharacteristic soundPressureCharacteristic (SCIENCE_KIT_UUID(" 0019" ), BLENotify);
32
+ BLEFloatCharacteristic resistanceCharacteristic (SCIENCE_KIT_UUID(" 0020" ), BLENotify);
30
33
34
+ byte voltageBufferIndex = 0 ;
35
+ bool voltageBufferFilled = false ;
31
36
short soundSampleBuffer[256 ];
37
+ short voltageSampleBuffer[VOLTAGE_BUFFER_SIZE];
32
38
33
39
void onPDMdata () {
34
40
// query the number of bytes available
@@ -46,6 +52,23 @@ uint16_t getSoundAverage() {
46
52
return sqrt (avg);
47
53
}
48
54
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
+
49
72
// String to calculate the local and device name
50
73
String name;
51
74
unsigned long lastNotify = 0 ;
@@ -76,6 +99,8 @@ void setup() {
76
99
77
100
delay (2000 );
78
101
102
+ pinMode (RESISTANCE_PIN, INPUT); // Used for reading resistance
103
+
79
104
if (!APDS.begin ()) {
80
105
printSerialMsg (" Failed to initialized APDS!" );
81
106
blinkLoop ();
@@ -143,6 +168,7 @@ void setup() {
143
168
service.addCharacteristic (proximityCharacteristic);
144
169
service.addCharacteristic (colorCharacteristic);
145
170
service.addCharacteristic (soundPressureCharacteristic);
171
+ service.addCharacteristic (resistanceCharacteristic);
146
172
147
173
versionCharacteristic.setValue (VERSION);
148
174
@@ -212,4 +238,11 @@ void updateSubscribedCharacteristics() {
212
238
float pressure = BARO.readPressure ();
213
239
pressureCharacteristic.writeValue (pressure);
214
240
}
241
+
242
+ if (resistanceCharacteristic.subscribed ()){
243
+ readVoltage ();
244
+ uint16_t measuredValue = getVoltageAverage ();
245
+ float voltageRatio = 1024 .0f / measuredValue;
246
+ resistanceCharacteristic.writeValue (voltageRatio);
247
+ }
215
248
}
0 commit comments