#include <Adafruit_GFX.h>     // Core graphics library
#include <SPI.h>              // Needed for display
#include <Adafruit_ILI9341.h>
#include <Wire.h>             // Needed for FT6206
#include <Adafruit_FT6206.h>

// Constants for pins
#define TFT_CS 10
#define TFT_DC 9
#define TOUCH_PIN 40
#define ANALOG_PIN A0

// Objects for display and touchscreen
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
Adafruit_FT6206 ctp = Adafruit_FT6206();

// constanten en variabelen voor stroom meting
const int analogIn = A0;
double mVperAmp = 66; // use 100 for 20A Module and 66 for 30A Module
double RawValue= 0;
double ACSoffset = 2500; 
double Voltage = 0;
double Amps = 0;

// Definieer de pinnen voor de drukknoppen en de potentiometer
const int potPin = A0;
const int buttonPin1 = 5;
const int buttonPin2 = 6;
const int buttonPin3 = 7;

// Definieer de drempelwaarden voor elke modus
const int threshold1 = 300; // 30% drempel
const int threshold2 = 600; // 60% drempel

int potValue = 0;
bool buttonState1 = false;
bool buttonState2 = false;
bool buttonState3 = false;




void setup() {
  Serial.begin(9600);

  // Configureer de drukknoppen als invoer
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  
  // opstarten van tt scherm
  tft.begin();
  ctp.begin(TOUCH_PIN);
  if (!ctp.begin(TOUCH_PIN)) {
    Serial.println("Couldn't start FT6206 touchscreen controller");
    while (1); // Stop if touchscreen controller not found
  }
  Serial.println("Capacitive touchscreen started");
  tft.setRotation(1); // Landscape orientation
  tft.fillScreen(ILI9341_BLACK);
  boxen();

}

void loop() {
  
  modus();
  tht();

}



//                                          FUNCTIES
/////////////////////////////////////////////////////////////////////////////////////////////////



// functie om het verbruik in amp uit te meten
void current() {
  while (true) { // One way to make it run constantly
    RawValue = analogRead(analogIn);
    Voltage = (RawValue / 1024.0) * 5000; // Gets you mV
    Amps = ((Voltage - ACSoffset) / mVperAmp);

    //Serial.print("Amps = "); // shows the voltage measured
    //Serial.println(Amps,2); // the '2' after voltage allows you to display 2 digits after decimal point
    //delay(1000);
  }
}



// Functie om een gekleurde box met tekst op het display te tekenen
void drawTextBox(int x, int y, int width, int height, uint16_t color, const char* text) {
  tft.fillRect(x, y, width, height, color);
  tft.setTextColor(ILI9341_BLACK);
  tft.setTextSize(2);
  tft.setCursor(x + 10, y + 10);
  tft.print(text);
}


// functie om de snelheid weer te geven
void displaySpeed() {
  tft.fillScreen(ILI9341_BLACK); // Wis het scherm
  tft.setTextColor(ILI9341_WHITE);
  tft.setTextSize(3);
  tft.setCursor(10, 100);
  tft.print("Snelheid:  km/h");
}


// functie olm het verbruik weer te geven
void verbruik() {
  tft.fillScreen(ILI9341_BLACK); // Wis het scherm
  tft.setTextColor(ILI9341_WHITE);
  tft.setTextSize(3);
  tft.setCursor(10, 100);
  tft.print("verbruik: ");
  tft.print(Amps);
  tft.print(" A");
}


// functie om de batterij status weer te geven
void soc() {
  tft.fillScreen(ILI9341_BLACK); // Wis het scherm
  tft.setTextColor(ILI9341_WHITE);
  tft.setTextSize(3);
  tft.setCursor(10, 100);
  tft.print("baterij:  %");
}


// functie om de submenu boxen te maken
void boxen(){
  int screenWidth = tft.width();
  int boxWidth = screenWidth / 3;
  drawTextBox(0, 205, boxWidth, 35, ILI9341_RED, "Snelh");
  drawTextBox(boxWidth, 205, boxWidth, 35, ILI9341_BLUE, "verbr");
  drawTextBox(2 * boxWidth, 205, boxWidth, 35, ILI9341_GREEN, "soc");

  
}



// functie voor de tutch menu
void tht() {
  delay(10);

  if (ctp.touched()) {
    TS_Point p = ctp.getPoint();
  
    p.x = map(p.x, 0, 320, 320, 0);
    p.y = map(p.y, 0, 240, 240, 0);

    Serial.print("("); Serial.print(p.y);
    Serial.print(", "); Serial.print(p.x);
    Serial.println(")");

    int screenWidth = tft.width();
    int boxWidth = screenWidth / 3;

    // Controleer of de aanraking binnen de grenzen van de "Snelh" box ligt
    if (p.y >= 135 && p.y <= 240 && p.x >= 286 && p.x <= 320) {
      
      displaySpeed(); // Toon de snelheid
      boxen();
      delay(2000);
      //tft.fillScreen(ILI9341_BLACK); // Wis het scherm
    }

    if (p.y >=28 && p.y <= 135 && p.x >= 286 && p.x <= 320) {
     
      current();
      verbruik(); // Toon het verbruik in A
      boxen();
      delay(2000);
      //tft.fillScreen(ILI9341_BLACK); // Wis het scherm
    }

    if (p.y >=-80 && p.y <= 28 && p.x >= 286 && p.x <= 320) {
      
      soc(); // Toon het batterij percentage
      boxen();
      delay(2000);
      //tft.fillScreen(ILI9341_BLACK); // Wis het scherm
    }
   

  }
}


//funtie omde modusen te kiezen en aan te suren
void modus() {
  // Lees de waarde van de potentiometer
  potValue = analogRead(potPin);

  // Lees de status van de drukknoppen
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);

  // Controleer welke modus is geselecteerd
  if (buttonState1 == HIGH && buttonState2 == LOW && buttonState3 == LOW) {
    // Modus 1: 30% van de potentiometerwaarde
    int scaledValue = map(potValue, 0, 1023, 0, threshold1);
    Serial.print("Modus 1 - Geschaalde Waarde: ");
    Serial.println(scaledValue);
    
    tft.fillRect(210, 20, 200, 30, ILI9341_BLACK); // X, Y, breedte, hoogte, kleur
    
    tft.setTextColor(ILI9341_WHITE);
    tft.setTextSize(2);
    tft.setCursor(240, 20);
    tft.print("eco");

  } else if (buttonState1 == LOW && buttonState2 == HIGH && buttonState3 == LOW) {
    // Modus 2: 60% van de potentiometerwaarde
    int scaledValue = map(potValue, 0, 1023, 0, threshold2);
    Serial.print("Modus 2 - Geschaalde Waarde: ");
    Serial.println(scaledValue);

    tft.fillRect(210, 20, 200, 30, ILI9341_BLACK); // X, Y, breedte, hoogte, kleur

    tft.setTextColor(ILI9341_WHITE);
    tft.setTextSize(2);
    tft.setCursor(210, 20);
    tft.print("comfort");

  } else if (buttonState1 == LOW && buttonState2 == LOW && buttonState3 == HIGH) {
    // Modus 3: 100% van de potentiometerwaarde
    Serial.print("Modus 3 - Geschaalde Waarde: ");
    Serial.println(potValue);

    tft.fillRect(210, 20, 200, 30, ILI9341_BLACK); // X, Y, breedte, hoogte, kleur

    tft.setTextColor(ILI9341_WHITE);
    tft.setTextSize(2);
    tft.setCursor(240, 20);
    tft.print("Sport");

  } else if (buttonState1 == LOW && buttonState2 == LOW && buttonState3 == LOW) {
    Serial.println("kies een modus");
    tft.fillRect(210, 20, 200, 30, ILI9341_BLACK); // X, Y, breedte, hoogte, kleur
  }

  // Een korte vertraging om debouncing van de drukknoppen te voorkomen
  delay(50);
}
$abcdeabcde151015202530fghijfghij