@@ -12,6 +12,9 @@ const int VERSION = 0x00000001;
12
12
const float TEMPERATURE_CALIBRATION = -5.0 ;
13
13
14
14
#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
15
18
16
19
// #define DEBUG 0
17
20
@@ -26,8 +29,12 @@ BLEFloatCharacteristic humidityCharacteristic (SCIENCE_KIT_UUID("001
26
29
BLEUnsignedIntCharacteristic proximityCharacteristic (SCIENCE_KIT_UUID(" 0017" ), BLENotify);
27
30
BLECharacteristic colorCharacteristic (SCIENCE_KIT_UUID(" 0018" ), BLENotify, 4 * sizeof(int ));
28
31
BLEUnsignedShortCharacteristic soundPressureCharacteristic (SCIENCE_KIT_UUID(" 0019" ), BLENotify);
32
+ BLEFloatCharacteristic resistanceCharacteristic (SCIENCE_KIT_UUID(" 0020" ), BLENotify);
29
33
34
+ short voltageBufferIndex = 0 ;
35
+ bool voltageBufferFilled = false ;
30
36
short soundSampleBuffer[256 ];
37
+ short voltageSampleBuffer[VOLTAGE_BUFFER_SIZE];
31
38
32
39
void onPDMdata () {
33
40
// query the number of bytes available
@@ -45,6 +52,26 @@ uint16_t getSoundAverage() {
45
52
return sqrt (avg);
46
53
}
47
54
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
+
48
75
// String to calculate the local and device name
49
76
String name;
50
77
unsigned long lastNotify = 0 ;
@@ -75,6 +102,8 @@ void setup() {
75
102
76
103
delay (2000 );
77
104
105
+ pinMode (RESISTANCE_PIN, INPUT); // Used for reading resistance
106
+
78
107
if (!APDS.begin ()) {
79
108
printSerialMsg (" Failed to initialized APDS!" );
80
109
blinkLoop ();
@@ -142,6 +171,7 @@ void setup() {
142
171
service.addCharacteristic (proximityCharacteristic);
143
172
service.addCharacteristic (colorCharacteristic);
144
173
service.addCharacteristic (soundPressureCharacteristic);
174
+ service.addCharacteristic (resistanceCharacteristic);
145
175
146
176
versionCharacteristic.setValue (VERSION);
147
177
@@ -213,4 +243,11 @@ void updateSubscribedCharacteristics() {
213
243
float pressure = BARO.readPressure ();
214
244
pressureCharacteristic.writeValue (pressure);
215
245
}
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
+ }
216
253
}
0 commit comments