#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
// LCD ve Keypad tanımlamaları
LiquidCrystal_I2C lcd(0x27, 16, 2);
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {6,7,8,9};
byte colPins[COLS] = {2,3,4,5};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Ses sensörü pin tanımı
#define MIC_PIN A0
// Bellek optimizasyonu için daha küçük örnek boyutu
#define SAMPLE_COUNT 64 // 256 yerine 64
#define SAMPLE_DELAY 1
// Global değişkenleri azalt
int recordedSamples[SAMPLE_COUNT];
bool isRecording = false;
bool hasRecording = false;
// Referans özellikleri (tüm örnekler yerine özellikler saklanıyor)
int refFeatures[9][3]; // Her rakam için 3 özellik
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.clear();
// Referans özellikleri oluştur
generateReferenceFeatures();
lcd.setCursor(0, 0);
lcd.print("Sesli Rakam");
lcd.setCursor(0, 1);
lcd.print("Tanima Sistemi");
delay(2000);
showMainScreen();
}
void loop() {
char key = keypad.getKey();
if (key == 'A') {
recordAudio();
} else if (key == 'B') {
if (hasRecording) {
analyzeAudio();
} else {
lcd.clear();
lcd.print("Once kayit alin");
lcd.setCursor(0, 1);
lcd.print("A tusuna basin");
delay(2000);
showMainScreen();
}
}
delay(50);
}
void showMainScreen() {
lcd.clear();
lcd.print("A: Kayit Al");
lcd.setCursor(0, 1);
lcd.print("B: Analiz Et");
}
void recordAudio() {
lcd.clear();
lcd.print("Kayit basliyor...");
// Kısa bir bekleme
for (int i = 0; i < 3; i++) {
lcd.print(".");
delay(333);
}
lcd.clear();
lcd.print("Kayit aliniyor");
isRecording = true;
int maxVal = 0;
int minVal = 1023;
// Ses örneklerini al ve özellikleri hesapla
for (int i = 0; i < SAMPLE_COUNT; i++) {
recordedSamples[i] = analogRead(MIC_PIN);
if (recordedSamples[i] > maxVal) maxVal = recordedSamples[i];
if (recordedSamples[i] < minVal) minVal = recordedSamples[i];
// İlerleme göstergesi
if (i % (SAMPLE_COUNT/16) == 0) {
lcd.setCursor(i/(SAMPLE_COUNT/16), 1);
lcd.print("#");
}
delay(SAMPLE_DELAY);
}
isRecording = false;
hasRecording = true;
lcd.clear();
lcd.print("Kayit tamamlandi!");
delay(1000);
showMainScreen();
}
void analyzeAudio() {
lcd.clear();
lcd.print("Analiz ediliyor");
// Kayıtlı sesin özelliklerini çıkar
int features[3];
extractFeatures(recordedSamples, SAMPLE_COUNT, features);
// Referanslarla karşılaştır
int bestMatch = findBestMatch(features);
// Sonucu göster
lcd.clear();
lcd.print("Taninan rakam:");
lcd.setCursor(0, 1);
lcd.print(bestMatch);
Serial.print("Taninan rakam: ");
Serial.println(bestMatch);
delay(3000);
showMainScreen();
}
void extractFeatures(int samples[], int count, int features[]) {
// Ses örneklerinden özellik çıkarımı
int maxVal = 0;
int minVal = 1023;
long sum = 0;
for (int i = 0; i < count; i++) {
if (samples[i] > maxVal) maxVal = samples[i];
if (samples[i] < minVal) minVal = samples[i];
sum += samples[i];
}
// 1. Özellik: Genlik
features[0] = maxVal - minVal;
// 2. Özellik: Ortalama değer
features[1] = sum / count;
// 3. Özellik: Sıfır geçiş sayısı (basitleştirilmiş)
int zeroCrossings = 0;
int average = features[1];
for (int i = 1; i < count; i++) {
if ((samples[i-1] < average && samples[i] >= average) ||
(samples[i-1] >= average && samples[i] < average)) {
zeroCrossings++;
}
}
features[2] = zeroCrossings;
}
int findBestMatch(int features[]) {
int bestMatch = 0;
int bestScore = 1000000; // Düşük skor daha iyi
// Tüm referans rakamları ile karşılaştır
for (int digit = 0; digit < 9; digit++) {
int score = 0;
// Özellikler arasındaki farkları hesapla
for (int i = 0; i < 3; i++) {
int diff = features[i] - refFeatures[digit][i];
score += abs(diff);
}
Serial.print("Rakam ");
Serial.print(digit+1);
Serial.print(" skor: ");
Serial.println(score);
if (score < bestScore) {
bestScore = score;
bestMatch = digit + 1;
}
}
return bestMatch;
}
void generateReferenceFeatures() {
// Referans özellikleri oluştur (simülasyon)
// Gerçek uygulamada bu özellikler önceden kaydedilmiş seslerden çıkarılmalı
// Her rakam için tipik özellik değerleri
for (int digit = 0; digit < 9; digit++) {
// 1. Özellik: Genlik (rakam yüksekliğine göre artar)
refFeatures[digit][0] = 100 + digit * 20;
// 2. Özellik: Ortalama değer
refFeatures[digit][1] = 512;
// 3. Özellik: Sıfır geçiş sayısı (rakam karmaşıklığına göre artar)
refFeatures[digit][2] = 10 + digit * 3;
}
Serial.println("Referans ozellikler olusturuldu");
}