const int switchPin = 2; // Assuming sw1:1 is connected to D2
const int ledPin = 13; // Built-in LED
const int micPin = A0; // Microphone pin
bool isRecording = false;
bool wasSwitchOn = false; // Track the previous switch state
void setup() {
Serial.begin(9600);
pinMode(switchPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.println("Move the switch to start recording...");
}
void loop() {
int switchState = digitalRead(switchPin);
// Check for a transition from OFF to ON
if (switchState == HIGH && !wasSwitchOn) {
startRecording();
wasSwitchOn = true;
}
// Check for a transition from ON to OFF
else if (switchState == LOW && wasSwitchOn) {
stopRecording();
wasSwitchOn = false;
}
if (isRecording) {
// Read microphone value
int micValue = analogRead(micPin);
// Print microphone value to the plotter
Serial.println(micValue);
// Add some delay to avoid spamming the plotter with data
delay(50);
}
}
void startRecording() {
isRecording = true;
digitalWrite(ledPin, HIGH);
Serial.println("Recording started...");
}
void stopRecording() {
isRecording = false;
digitalWrite(ledPin, LOW);
Serial.println("Recording stopped...");
delay(500); // Add a delay to ensure a single "Recording stopped..." message
}