/*
* Simple Wokwi Microphone Demo
*/
#include <SD.h>
File myFile;
// change this to match your SD shield or module;
const int chipSelect = 10;
void setup() {
Serial.begin(115200);
pinMode(5, OUTPUT);
if (SD.begin()) {
Serial.println("SD card is initialized. Ready to go");
}
else {
Serial.println("Failed");
return;
}
myFile = SD.open("audio.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
myFile.println("Audio Buffer"); // SD CARD SAVE
// close the file:
myFile.close();
Serial.println("Done");
} else {
// if the file didn't open, print an error:
Serial.println("Error opening audio.txt");
}
}
void loop(){
myFile = SD.open("audio.txt", FILE_WRITE);
if (myFile) {
record();
}
else {
Serial.println("Error opening file");
}
delay(10000);
}
void record() {
int timer=0;
while(timer<100000){
int sample = analogRead(A0);
myFile.println(sample);
Serial.println(sample);
digitalWrite(13, abs(sample - 512) > 50);
timer=timer+1;
delay(1);
}
myFile.flush();
myFile.close();
}