1
+ /* *
2
+ * Record output from SDP810 infrasonic microphone and send it to the Serial output
3
+ *
4
+ */
5
+
6
+ #include < Wire.h>
7
+ #include < SDPSensors.h> // https://github.com/UT2UH/SDP3x-Arduino/tree/SDP8x
8
+ #include < AudioTools.h>
9
+
10
+ using namespace audio_tools ;
11
+ typedef int16_t sound_t ; // sound will be represented as int16_t
12
+
13
+ const int sample_rate = 1024 ;
14
+ const int buffer_size = 64 ;
15
+ bool status;
16
+ int16_t pressure;
17
+
18
+ SDP8XX Mic = SDP8XX(Address5, DiffPressure, Wire);
19
+ TimerAlarmRepeating samplingTimer (1 );
20
+ NBuffer<int16_t > buffer (buffer_size, 3 );
21
+
22
+ // callback to record the sound data into a buffer
23
+ void writeSample (void *ptr) {
24
+ status = Mic.readMeasurement (&pressure, NULL , NULL );
25
+ sound_t sample = pressure;
26
+ buffer.write (sample);
27
+ }
28
+
29
+ // Arduino Setup
30
+ void setup (void ) {
31
+ Serial.begin (115200 );
32
+
33
+ // setup SDP810 infrasound microphone
34
+ Wire.begin ();
35
+ Mic.begin ();
36
+ Mic.startContinuous (false );
37
+
38
+ // start the timer to record the data
39
+ long waitUs = 1000000 / sample_rate;
40
+ samplingTimer.start (writeSample, waitUs, TimerAlarmRepeating::US);
41
+
42
+ }
43
+
44
+ void printSampleRate () {
45
+ static long next_time;
46
+ if (millis ()>next_time){
47
+ next_time = next_time+1000 ;
48
+ Serial.print (" sample rate: " );
49
+ Serial.println (buffer.sampleRate ());
50
+ }
51
+ }
52
+
53
+ void printSamples () {
54
+ static sound_t array[buffer_size][2 ];
55
+ size_t len = buffer.readFrames (array);
56
+ for (int j=0 ;j<len;j++){
57
+ Serial.print (array[j][0 ]);
58
+ Serial.print (" " );
59
+ Serial.println (array[j][1 ]);
60
+ }
61
+ }
62
+
63
+ // Arduino loop - repeated processing
64
+ void loop () {
65
+ // copy sound data from samples to Serial
66
+ printSamples ();
67
+ printSampleRate ();
68
+ }
0 commit comments