Skip to content

Commit 99b9f47

Browse files
committed
Adding Serial Control to Microphone Test
1 parent e85d475 commit 99b9f47

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

examples/test_microphone/test_microphone.ino

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ short sampleBuffer[256];
1212
volatile int samplesRead;
1313

1414
bool record = false;
15+
bool commandRecv = false;
1516

1617
void setup() {
1718
Serial.begin(9600);
@@ -28,7 +29,7 @@ void setup() {
2829
}
2930

3031
Serial.println("Welcome to the microphone test for the built-in microphone on the Nano 33 BLE Sense\n");
31-
Serial.println("Use the on-shield button to start and stop an audio recording");
32+
Serial.println("Use the on-shield button or send the command 'click' to start and stop an audio recording");
3233
Serial.println("Open the Serial Plotter to view the corresponding waveform");
3334
}
3435

@@ -39,15 +40,33 @@ void loop() {
3940
record = !record;
4041
}
4142

42-
if (samplesRead) {
43+
// see if a command was sent over the serial monitor and record it if so
44+
String command;
45+
while (Serial.available()) {
46+
char c = Serial.read();
47+
if ((c != '\n') && (c != '\r')) {
48+
command.concat(c);
49+
}
50+
else if (c == '\r') {
51+
commandRecv = true;
52+
command.toLowerCase();
53+
}
54+
}
55+
56+
// parse the command if applicable
57+
if (commandRecv && command == "click") {
58+
commandRecv = false;
59+
record = !record;
60+
}
4361

62+
// display the audio if applicable
63+
if (samplesRead) {
4464
// print samples to serial plotter
4565
if (record) {
4666
for (int i = 0; i < samplesRead; i++) {
4767
Serial.println(sampleBuffer[i]);
4868
}
4969
}
50-
5170
// clear read count
5271
samplesRead = 0;
5372
}

0 commit comments

Comments
 (0)