/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: **Color Sensor**
	- Source Code NOT compiled for: Arduino Uno
	- Source Code created on: 2025-02-25 15:32:04

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* It should write the name of the red, yellow or */
	/* black colors detected by my color sensor on the */
	/* screen, but in Turkish. */
/****** END SYSTEM REQUIREMENTS *****/

/* START CODE */

/****** DEFINITION OF LIBRARIES *****/
#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
#include <SPI.h>
#include <limits.h> // INT_MAX için gerekli kütüphane

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
int readColor(int color);
void learnColors();
void waitForEnter();
const char* recognizeColor(int red, int green, int blue);

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
#define TFT_CS   10
#define TFT_DC   9
#define TFT_RST  8
#define S0 2
#define S1 3
#define S2 4
#define S3 5
#define OUT 6

Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);

// Renk yapısı
struct Color {
  int red;
  int green;
  int blue;
  const char* name;
};

// Ezberlenen renkler
Color learnedColors[3]; // Kırmızı, Sarı, Siyah için 3 renk
int learnedColorCount = 0;

void setup(void)
{
  // put your setup code here, to run once:
  Serial.begin(9600);  // Seri iletişimi başlat
  tft.initR(INITR_BLACKTAB); // ST7735S chip, black tab
  tft.fillScreen(ST7735_BLACK);
  tft.setTextColor(ST7735_WHITE);
  tft.setTextSize(1);
  
  // TCS3200 pinlerini ayarla
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  pinMode(OUT, INPUT);
  
  // TCS3200 frekans ölçekleme ayarı (20% frekans ölçekleme)
  digitalWrite(S0, HIGH);
  digitalWrite(S1, LOW);
  
  // Renkleri öğren
  learnColors();
}

void loop(void)
{
  // Renkleri oku
  int red = readColor(2);   // Kırmızı renk
  int green = readColor(1); // Yeşil renk
  int blue = readColor(0);  // Mavi renk
  
  // Renk ismini belirle
  const char* colorName = recognizeColor(red, green, blue);
  
  // Renk değerlerini ve ismini Serial Monitor'e yazdır
  Serial.print("Red: ");
  Serial.print(red);
  Serial.print(" | Green: ");
  Serial.print(green);
  Serial.print(" | Blue: ");
  Serial.print(blue);
  Serial.print(" | Renk: ");
  Serial.println(colorName);
  
  // Renk ismini TFT ekranda göster
  tft.fillScreen(ST7735_BLACK);
  tft.setCursor(10, 10);
  tft.print("Renk: ");
  tft.println(colorName);
  delay(1000); // 1 saniye bekle
}

int readColor(int color) {
  // Renk filtrelerini ayarla
  digitalWrite(S2, (color >> 1) & 0x01); // Bit manipülasyonu
  digitalWrite(S3, color & 0x01);
  
  // Frekans ölçümü
  delay(50); // Kararlılık için bekle
  return pulseIn(OUT, LOW); // LOW pals süresi
}

void learnColors() {
  Serial.println("Renk ogrenme modu...");
  
  // Kırmızı rengi öğren
  Serial.println("Kirmizi renge tutun ve ENTER tusuna basin...");
  waitForEnter();
  learnedColors[0] = {readColor(2), readColor(1), readColor(0), "Kırmızı"}; // Struct initialization fixed
  learnedColorCount++;
  Serial.println("Kirmizi renk ogrenildi!");
  
  // Sarı rengi öğren
  Serial.println("Sari renge tutun ve ENTER tusuna basin...");
  waitForEnter();
  learnedColors[1] = {readColor(2), readColor(1), readColor(0), "Sarı"}; // Struct initialization fixed
  learnedColorCount++;
  Serial.println("Sari renk ogrenildi!");
  
  // Siyah rengi öğren
  Serial.println("Siyah renge tutun ve ENTER tusuna basin...");
  waitForEnter();
  learnedColors[2] = {readColor(2), readColor(1), readColor(0), "Siyah"}; // Struct initialization fixed
  learnedColorCount++;
  Serial.println("Siyah renk ogrenildi!");
  Serial.println("Renk ogrenme tamamlandi!");
}

void waitForEnter() {
  while (true) {
    if (Serial.available() > 0) {
      char input = Serial.read();
      if (input == '\n' || input == '\r') { // Enter tuşu algılandı
        break;
      }
    }
  }
}

const char* recognizeColor(int red, int green, int blue) {
  // Öğrenilen renkler arasında en yakın olanı bul
  int minDistance = INT_MAX; // INT_MAX artık tanımlı
  const char* closestColor = "Bilinmeyen";
  
  for (int i = 0; i < learnedColorCount; i++) {
    int distance = abs(red - learnedColors[i].red) +
                   abs(green - learnedColors[i].green) +
                   abs(blue - learnedColors[i].blue);
    if (distance < minDistance) {
      minDistance = distance;
      closestColor = learnedColors[i].name;
    }
  }
  return closestColor;
}

/* END CODE */