This repository has been archived by the owner on Sep 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publishBPM.ino
432 lines (342 loc) · 14.1 KB
/
publishBPM.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//#include "application.h"
//#include "math.h"
// This #include statement was automatically added by the Particle IDE.
// This is needed for the HTU21D so that it doesn't iterfere with the music level
#include "SparkIntervalTimer/SparkIntervalTimer.h"
// This #include statement was automatically added by the Particle IDE.
#include "HTU21D/HTU21D.h"
//debugging
//if this is defined then serial will work
//#define SERIALout
//SYSTEM_MODE(AUTOMATIC); //Just to have the blink start immediately
SYSTEM_THREAD(ENABLED);
//adding a recalibration for the mic
unsigned long ONE_DAY_MILLIS = 24 * 60 * 60 * 1000;
unsigned long lastSync = 0;
//millis() + 12*60*60*1000+ 60*1000*30;
/////////////////TEMP/HUM VARIABLES
//HTU sensor power
byte htupow = A0;
//init the temp sensor
HTU21D myHumidity;
//string to pass
String passData = "NOT YET CALIBRATED";
//temporary humidity & temperature for sensor reset
float temporaryH = 40;
float temporaryT = 5;
//read the hum / tem sensor
unsigned long interval=19000; // the time we need to wait, set the time to 19s because otherwise we'll lose the cloud connection
unsigned long previousMillis=0; // millis() returns an unsigned long.
//this variable is used to set which sensor value is read
byte sensorHorT = 0;
// led when the HTU is being read
bool ledState = false; // state variable for the LED
////////////////////////////////
// these are mic variables
//start timer for
IntervalTimer myTimer;
// another timer for mic calibration
IntervalTimer micCal;
//microphone array to hold the samples
const int maxIterations = 256; // this is the max number of sample iterations there are 40 sample intervals in 1s
//volatile double fftData[maxIterations]; //this is where we will hold the fft data
volatile int sampleIterator = 0; // iterates through the samples
volatile unsigned long oneSample = 0;
//const int maxIntervals = 40; //intervals that are to be checked against the average 40 = 5s
const int maxIntervals = 8; //intervals that are to be checked against the average 8 = 1s
volatile int intervalIterator = 0; // iterates throught the intervals
volatile long sampleSqAdd = 0; //add all the squares of the samples
//volatile long sampleSqAddFFT = 0; //add all the FFT
volatile unsigned long intervalEnergy[maxIntervals]; // store the various FFT intervals here
volatile unsigned long totalEnergy = 0; // add everything together to get the full energy per second
//volatile unsigned long totalEnergyFFT = 0; // add everything together to get the full energy per second
volatile unsigned long average5sPublish = 0; // this is the 5s average sound power to be published
volatile unsigned long tenSecondAve[10];
volatile int tenIterator = 0;
//volatile int numberOfBeats = 0; //rolling count of bpm
//volatile int beatPublish = 0; //bpm that goes out
volatile unsigned long passThisEnergy = 0;
volatile int totalNoise = 0;
volatile int noiseIterator = 0;
int tempThreshold = 0;
float pi = 3.1415927;
float secondsLoop = 0.125 * maxIntervals; //one frequency sample is 8Hz, so we can calculate the average of
boolean beatDetectionStarted = false; //this checks that the beat is not calculated twice
boolean blockingCheck = false; // this makes sure that when blocking occurs then the false BPM is passed along
byte microphoneCheck = A1; //mic analogue input
byte micDig = A2; //mic digital toggle for
int sensorReading = 0; // variable to store the value read from the sensor pin
int threshold = 0; //this is the threshold variable
String testVar1 = "";
String testVar2 = "";
const uint8_t ledPin = D7; //blink
String infotwerk = "e-twerk @ Kertsi: Temperature (C) / Humidity (Rel %) / Volume (dB)";
void setup() {
//reset to 0 the ten second ave
for (int i=0;i<10;i++){
tenSecondAve[i]=0;
}
ONE_DAY_MILLIS = 16*60*60*1000 + 40*60*1000;
//led test
pinMode(ledPin, OUTPUT);
digitalWriteFast(ledPin, HIGH);
Particle.variable("bileinfo", infotwerk);
Particle.variable("diskotappi",passData);
// Particle.variable("test1",testVar1);
// Particle.variable("test2",testVar2);
// Particle.variable("torstai", torstai, STRING);
//initialize the sound energy history buffer
for (int i = 0; i< maxIntervals; i++){
intervalEnergy[i]=0;
}
//set microphoe to low
pinMode(micDig, OUTPUT);
digitalWrite(micDig, LOW);
delay(50);
pinMode(microphoneCheck, INPUT);
//calculate what is silent
int totalNoise = 0;
int iteratorNoise = 250;
for (int i = 0; i < iteratorNoise; i++ ){
totalNoise += analogRead(microphoneCheck);
delay(50);
}
// testVar1 = (String)totalNoise;
//silence downscale by 10
threshold = totalNoise / iteratorNoise ;
//testVar2 = (String)threshold;
digitalWrite(htupow, HIGH);
myHumidity.begin();
//wait for the sensor to start
delay(1000);
// this is 8 * 512 samples
myTimer.begin(getData, 488, uSec);
micCal.begin(calibrateFunc, 50, hmSec);
}
void loop() {
//sensorReading = analogRead(microphoneCheck);
//check if 20 seconds have passed and it is time to read the sensor
unsigned long currentMillis = millis(); // grab current time
// check if "interval" time has passed
//Serial.println(currentMillis - previousMillis);
//read temperature every e.g. 20s and humidity every 20s
if (currentMillis < previousMillis){
previousMillis = 0;
}
if ((unsigned long)(currentMillis - previousMillis) >= interval) {
blockingCheck = true;
readTempHumd();
//save the "current" time
previousMillis = millis();
}
//delay(500);
if (currentMillis < lastSync){
lastSync = 0;
}
if ((unsigned long)(currentMillis - lastSync) >= ONE_DAY_MILLIS) {
// Request time synchronization from the Particle Cloud
//Particle.syncTime();
ONE_DAY_MILLIS = 24 * 60 * 60 * 1000;
//calculate what is silent
/*
int iteratorNoise = 250;
for (int i = 0; i < iteratorNoise; i++ ){
totalNoise += analogRead(microphoneCheck);
delay(50);
}
*/
lastSync = millis();
threshold = tempThreshold ;
}
String tempTs(temporaryT,2);
String tempHs(temporaryH,2);
if (average5sPublish < 65){
passData = "e-twerk @ kertsi: "+ tempTs +"°C, humidity: "+tempHs + "%, " + "vol: ALLE 70" + " dB";
//+ (String)(totalEnergy / 1000) ;
} else if (average5sPublish > 65 && average5sPublish < 100){
passData = "e-twerk @ kertsi: "+ tempTs +"°C, humidity: "+tempHs + "%, vol: " + String(average5sPublish) + " dB";
//+(String)(totalEnergy / 1000) ;
} else {
passData = "e-twerk @ kertsi: "+ tempTs +"°C, humidity: "+tempHs + "%, vol: " + "YLI 95" + " dB";
//+ (String)(totalEnergy / 1000) ;
}
//+ ", volume: " + String(average5sPublish) + ", BPM:" + String(beatPublish);
//passData = " TEST: " + String(outputTester) + " TEST2: " + String(outputTester2) + " no: "+outputter;
}
//read the temperature or humidity
void readTempHumd(){
if (sensorHorT == 0){
//float temp = 0;
float temp = myHumidity.readTemperature();
if (temp != 998){
temporaryT = temp;
}else{
resetHTU();
}
sensorHorT = 1;
}else{
//float hum = 0;
float hum = myHumidity.readHumidity();
if (hum != 998){
temporaryH = hum;
} else {
resetHTU();
}
sensorHorT = 0;
}
}
//reset the temp sensor
void resetHTU(){
digitalWrite(htupow, LOW);
delay(1000);
digitalWrite(htupow, HIGH);
delay(1000);
}
void blinkBPM(){
// "toggles" the state, when beat is detected flick the led
digitalWriteFast(ledPin, !pinReadFast(ledPin)); // sets the LED based on ledState
}
void getData(){
oneSample = abs(analogRead(microphoneCheck));
//sampleSqAdd += oneSample; //val 3350 is a result of testing in the environment
sampleSqAdd += abs(oneSample-threshold);
//sampleSqAddFFT = abs(oneSample-threshold);
++sampleIterator;
//average5sPublish = oneSample;
if(sampleIterator == maxIterations){
sampleIterator = 0;
totalEnergy += sampleSqAdd;
//totalEnergyFFT += sampleSqAddFFT;
sampleSqAdd = 0;
//sampleSqAddFFT = 0;
intervalIterator++;
//every 5s take a reading of the vol levels
if(intervalIterator == maxIntervals){
tenSecondAve[tenIterator] = totalEnergy ;
if (tenIterator == 9){
tenIterator = 0;
for (int i = 0; i<10; i++){
passThisEnergy += tenSecondAve[i]/10;
}
passThisEnergy /= 1000;
if (passThisEnergy < 15){
average5sPublish = 60;
} else if (passThisEnergy > 15 && passThisEnergy < 23){
average5sPublish = 70;
} else if (passThisEnergy > 23 && passThisEnergy < 45){
average5sPublish = 80;
} else if (passThisEnergy > 45 && passThisEnergy < 73){
average5sPublish = 90;
} else if (passThisEnergy > 73 ){
average5sPublish = 100;
}
passThisEnergy = 0;
} else {
tenIterator++;
}
intervalIterator = 0;
totalEnergy = 0;
/*
if (totalEnergy / 1000 < 15){
average5sPublish = 60;
} else if (totalEnergy / 1000 > 15 && totalEnergy / 1000 < 23){
average5sPublish = 70;
} else if (totalEnergy / 1000> 23 && totalEnergy / 1000 < 45){
average5sPublish = 80;
} else if (totalEnergy / 1000 > 45 && totalEnergy / 1000 < 73){
average5sPublish = 90;
} else if (totalEnergy / 1000 > 73 ){
average5sPublish = 100;
}
*/
}
}
//add the squares of the samples
unsigned long oneSample = abs(analogRead(microphoneCheck)-threshold);
sampleSqAdd += sq(oneSample);
fftData[sampleIterator] = (double)oneSample;
++sampleIterator;
if(sampleIterator == maxIterations){ //we've hit the end of one interval and it is time to add everything together
sampleIterator = 0; //reset the iterator
totalEnergy += sampleSqAdd /100; //add to total energy for the sound level
double midFFTholderR; //REAL
double midFFTholderI; //IMAGINARY
double loopEnergyFFT;
//we need to calculate the FFT, we use the Cooley-Tukey transform 80 - 256 Hz, and calculate the energy for that!
for (int j = 5; j < 16 ; j++){
for (int i = 0; i < maxIterations / 2; i+=2 ){
midFFTholderR = fftData[i]*cos(j*i*pi/64)+fftData[i+1]*cos((1+2*i)*j*pi/128);
midFFTholderI = fftData[i]*sin(j*i*pi/64)+fftData[i+1]*sin((1+2*i)*j*pi/128);
}
loopEnergyFFT += sqf(midFFTholderR) + sqf(midFFTholderI); //FFT energy addition
}
//outputTester= (long)loopEnergyFFT;
//check for the beat if one add it!
intervalEnergy[intervalIterator] = loopEnergyFFT; // add it to the energy history buffer
//time to calculate the total energy from the buffer, which is 0 in the beginning
double averageLocalEnergy = 0;
for (int i = 0; i< maxIntervals; i++){
averageLocalEnergy += intervalEnergy[i] / maxIntervals;
}
/* WE'RE NOT USING VARIANCE IN V 1.0
double variance = 0; // this is the variance needed for our device
//calculate variance
for (int i = 0; i<maxIntervals; i++){
variance += sq((averageLocalEnergy - intervalEnergy[i])/1000) / maxIntervals;
}
//use linear regression model [from a paper], as we used almost half the samples just multiply by 2
double C = max(1.8142857 - 0.0025714 * variance / 3, 1.5);
outputTester = numberOfBeats;
*/
double c = 1.5; //we will try this for the treshold
//outputTester2= (unsigned long)( c * averageLocalEnergy);
//check if the beat goes above the threshold
if (loopEnergyFFT > c * averageLocalEnergy){
beatDetectionStarted = true;
// outputter = "YEES";
} else {
//to remove the possibility that the beat is calculated twice, we have to wait until the energy drops below the threshold
if (beatDetectionStarted){
numberOfBeats += 1;
blinkBPM();
// outputter = "HAPPENED";
}
beatDetectionStarted = false;
//outputter = "NOO";
}
sampleSqAdd = 0; // reset samples to next interval
//iterate to the next interval
intervalIterator++;
if(intervalIterator == maxIntervals){ // check if the max number of intervals has been reached
//reset interval
intervalIterator = 0;
if(blockingCheck){
blockingCheck = false;
} else {
beatPublish = (int)(numberOfBeats * 60 / secondsLoop) ; // this is the beats, might need to do FFT, but that can be updated
}
numberOfBeats = 0; // reset beats counter
//sound level calculation per second
average5sPublish = totalEnergy / secondsLoop;
totalEnergy = 0;
// outputTester2= (unsigned long)average5sPublish;
outputTester = (unsigned long)secondsLoop;
}
}
}
void calibrateFunc(){
if(noiseIterator < 256){
totalNoise += oneSample;
} else {
tempThreshold = totalNoise / 256;
totalNoise = 0;
noiseIterator = 0;
}
noiseIterator++;
}
unsigned long sq(long product){
return product * product;
}
double sqf(double product){
return product * product;
}