#include <U8g2lib.h> // Include the U8g2 library
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0); // Define the OLED object
const int symbol1 = 68; // Symbol for the first display (e.g., arrow)
const int symbol2 = 78; // Symbol for the second display (e.g., cross)
const int microphonePin = A0; // Analog pin connected to the microphone
const int relayPin = 2; // Digital pin connected to the relay
const int peakThreshold = 500; // Adjust this threshold according to your microphone sensitivity
const int peakCount = 3; // Number of peaks to detect
int del = 3000;
int peakDetected = 0; // Counter for the number of peaks detected
void setup() {
u8g2.begin(); // Initialize the OLED display
u8g2.setFont(u8g2_font_profont12_tr); // Choose a font for symbols
u8g2.clearBuffer(); // Clear the display buffer
pinMode(relayPin, OUTPUT); // Set relay pin as an output
digitalWrite(relayPin, LOW); // Initially turn off the relay
Serial.begin(9600); // Initialize serial communication for debugging
}
void loop() {
int audioValue = analogRead(microphonePin); // Read the audio input
Serial.print("Audio Value: ");
Serial.println(audioValue);
if (audioValue > peakThreshold) { // Peak detected
peakDetected++; // Increment the peak counter
delay(del);
if (peakDetected >= peakCount)
{
delay(del);
// Desired number of peaks detected
digitalWrite(relayPin, HIGH); // Turn on the relay
u8g2.clearBuffer(); // Clear the display buffer
// Show the first symbol
u8g2.drawGlyph(64, 32, symbol1); // Display the first symbol
u8g2.sendBuffer(); // Send the buffer to the OLED display
delay(5000); // Wait for 5 seconds
digitalWrite(relayPin, LOW); // Turn off the relay
peakDetected = 0; // Reset the peak counter
}
}
u8g2.clearBuffer(); // Clear the display buffer
u8g2.drawGlyph(64, 32, symbol2); // Display the second symbol
u8g2.sendBuffer(); // Send the buffer to the OLED display
}