#include <EEPROM.h>

#include <ILI9341_t3.h>

//#include <font_Arial.h> // from ILI9341_t3

#include <XPT2046_Touchscreen.h>

#include <SPI.h>

#include <Encoder.h>

 

#define CS_PIN  8

#define TFT_DC  9

#define TFT_CS 10

// MOSI=11, MISO=12, SCK=13

 

 

const byte debounceTime = 50;

volatile unsigned long lastButtonPress;

volatile boolean buttonPressed = 0;

 

 

 

int navNum0;

int navNum1;

 

 

 

 

 

 

XPT2046_Touchscreen ts(CS_PIN);

#define TIRQ_PIN  29

//XPT2046_Touchscreen ts(CS_PIN);  // Param 2 - NULL - No interrupts

//XPT2046_Touchscreen ts(CS_PIN, 255);  // Param 2 - 255 - No interrupts

//XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN);  // Param 2 - Touch IRQ Pin - interrupt enabled polling

 

ILI9341_t3 tft = ILI9341_t3(TFT_CS, TFT_DC);

 

 

Encoder enc1(28, 27);

int buttonPin = 26;

int Los=0;

 

float temp_AD[8][10];

float AD [10];

 

 

int pumpe=30,Diff_A=2,Pumpe_trin1,Pumpe_trin2,Pumpe_trin,Pumpe_diff = 5,Aktemp;// pumpe test

double code;

float celsius;

int Vis = 0, Side = 0, Inst = 6, Tou = 120, Set = 0, Soltank = 0, Led_Display = 180;

int Output = 2, Bundtank = 0, Solgulv = 0, Trin = 0, ABC = 0, AB = 0, Startertid1 = 0, Displayvis = 0, Gem_hent = 3;

float Diffon = 8, Diffoff = 5, Bundtankinst = 55, Tid1 = 30, Tid2 = 10, Instid1 = 30, Instid2 = 10;

float Sol = 0, Tankbund = 0, Tanktop = 0, Gulv = 0, Tempsol = 0, Solretur = 0;

unsigned long previousTime = 0;

byte seconds ;

byte minutes ;

// Colors for fonts

int t = 1;

 

const uint16_t BLACK = 0x0000;

const uint16_t GREY = 0x7AEF;

const uint16_t BLUE = 0x001F;

const uint16_t RED = 0xF800;

const uint16_t GREEN = 0x07E0;

const uint16_t CYAN = 0x07FF;

const uint16_t MAGENTA = 0xF81F;

const uint16_t YELLOW = 0xFFE0;

const uint16_t WHITE = 0xFFFF;

 

void setup() {

 

 

 

  pinMode(22, INPUT );

  pinMode(7, OUTPUT );

  pinMode(23, OUTPUT );

  pinMode(1, OUTPUT );

  pinMode(2, OUTPUT);

  pinMode(3, OUTPUT);

  pinMode(4, OUTPUT );

  pinMode(5, OUTPUT );

  pinMode(6, OUTPUT );

  pinMode(11, OUTPUT);

  pinMode(12, OUTPUT);

  pinMode(29, OUTPUT);

  Eeprom_hent ();

  Serial.begin(38400);

  ts.begin();

  while (!Serial && (millis() <= 1000));

  pinMode(buttonPin, INPUT);

  attachInterrupt(digitalPinToInterrupt(buttonPin), button_ISR, CHANGE);

 

}

 

 

 

 

 

void Read (void) {

 

  code = analogRead(A0);

  Temp_code ();

  AD[1] = celsius;//Sol

 

  code = analogRead(A1);

  Temp_code ();

  AD[2] = celsius;//Tankbund

 

  code = analogRead(A2);

  Temp_code ();

  AD[3] = celsius;//Tanktop

 

  code = analogRead(A3);

  Temp_code ();

  AD[4] = celsius;//Gulv

 

  code = analogRead(A4);

  Temp_code ();

  AD[5] = celsius;//Solretur

 

}

 

 

void Temp_code (void) {

 

  if (code <= 41) {

    celsius = -35 + (code - 41) / 2.21;

  }

  if (code > 41 && code <= 55) {

    celsius = -30 + (code - 55) / 2.86;

  }

  if (code > 55 && code <= 73) {

    celsius = -25 + (code - 73) / 3.64;

  }

  if (code > 73 && code <= 96) {

    celsius = -20 + (code - 96) / 4.55;

  }

  if (code > 96 && code <= 124) {

    celsius = -15 + (code - 124) / 5.56;

  }

  if (code > 124 && code <= 157) {

    celsius = -10 + (code - 157) / 6.65;

  }

  if (code > 157 && code <= 196) {

    celsius = -5 + (code - 196) / 7.77;

  }

  if (code > 196 && code <= 240) {

    celsius = 0 + (code - 240) / 8.85;

  }

  if (code > 240 && code <= 289) {

    celsius = 5 + (code - 289) / 9.82;

  }

  if (code > 289 && code <= 342) {

    celsius = 10 + (code - 342) / 10.60;

  }

  if (code > 342 && code <= 398) {

    celsius = 15 + (code - 398) / 11.12;

  }

  if (code > 398 && code <= 455) {

    celsius = 20 + (code - 455) / 11.36;

  }

  if (code > 455 && code <= 512) {

    celsius = 25 + (code - 512) / 11.32;

  }

  if (code > 512 && code <= 566) {

    celsius = 30 + (code - 566) / 11.00;

  }

  if (code > 566 && code <= 619) {

    celsius = 35 + (code - 619) / 10.44;

  }

  if (code > 619 && code <= 667) {

    celsius = 40 + (code - 667) / 9.73;

  }

  if (code > 667 && code <= 712) {

    celsius = 45 + (code - 712) / 8.90;

  }

  if (code > 712 && code <= 752) {

    celsius = 50 + (code - 752) / 8.03;

  }

  if (code > 752 && code <= 788) {

    celsius = 55 + (code - 788) / 7.13;

  }

  if (code > 788 && code <= 819) {

    celsius = 60 + (code - 819) / 6.29;

  }

  if (code > 819 && code <= 847) {

    celsius = 65 + (code - 847) / 5.50;

  }

  if (code > 847 && code <= 870) {

    celsius = 70 + (code - 870) / 4.77;

  }

  if (code > 870 && code <= 891) {

    celsius = 75 + (code - 891) / 4.12;

  }

  if (code > 891 && code <= 909) {

    celsius = 80 + (code - 909) / 3.56;

  }

  if (code > 909 && code <= 924) {

    celsius = 85 + (code - 924) / 3.05;

  }

  if (code > 924 && code <= 937) {

    celsius = 90 + (code - 937) / 2.62;

  }

  if (code > 937 && code <= 948) {

    celsius = 95 + (code - 948) / 2.25;

  }

  if (code > 948 && code <= 958) {

    celsius = 100 + (code - 958) / 1.93;

  }

  if (code > 958 && code <= 966) {

    celsius = 105 + (code - 966) / 1.66;

  }

  if (code > 966 && code <= 974) {

    celsius = 110 + (code - 974) / 1.42;

  }

  if (code > 974 && code <= 980) {

    celsius = 115 + (code - 980) / 1.22;

  }

  if (code > 980 && code <= 985) {

    celsius = 120 + (code - 985) / 1.06;

  }

  if (code > 985 && code <= 989) {

    celsius = 125 + (code - 989) / 0.91;

  }

  if (code > 989 && code <= 993) {

    celsius = 130 + (code - 993) / 0.79;

  }

  if (code > 993 && code <= 997) {

    celsius = 135 + (code - 997) / 0.68;

  }

  if (code > 997 && code <= 1000) {

    celsius = 140 + (code - 1000) / 0.59;

  }

  if (code > 1000 ) {

    celsius = 145 + (code - 1002) / 0.52;

  }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

void Eeprom_gem (void) {

 

  tft.begin();

  tft.setRotation(3);

  tft.setTextSize(3);

  tft.setTextColor(GREEN);

  tft.fillScreen(RED);

  tft.setCursor(0, 0);

  tft.print("  Gemmer i Eprom ");

  Displayvis = 0;

  delay (500);

  tft.fillScreen(RED);

 

 

  int a;

  a = EEPROM.read(0);

 

  if (a != Diffon) {

    EEPROM.write (0, Diffon);

    tft.setCursor(0, 0);

    tft.print("Opdater Diff on ");

  }

  else {

 

    tft.setCursor(0, 0);

    tft.print("  Diff on OK ");

    //Displayvis = 0;

 

  }

  delay (120);

  a = EEPROM.read(1);

  if (a != Diffoff) {

    EEPROM.write (1, Diffoff);

    tft.setCursor(0, 30);

    tft.print("Opdater Diff on");

  }

  else {

 

    tft.setCursor(0, 30);

    tft.print("  Diff on OK");

 

  }

  delay (120);

  a = EEPROM.read(2);

  if (a != Bundtankinst) {

    EEPROM.write (2, Bundtankinst);

    tft.setCursor(0, 60);

    tft.print("Opdater Bundtankinst");

  }

  else {

 

    tft.setCursor(0, 60);

    tft.print("  Bundtankinst OK");

 

  }

  delay (120);

  a = EEPROM.read(3);

  if (a != Instid1) {

    EEPROM.write (3, Instid1);

    tft.setCursor(0, 90);

    tft.print("Opdater Instid 1");

  }

  else {

 

    tft.setCursor(0, 90);

    tft.print("  Instid1 OK");

 

  }

  delay (120);

  a = EEPROM.read(4);

  if (a != Instid2) {

    EEPROM.write (4, Instid2);

    tft.setCursor(0, 120);

    tft.print("Opdater Instid 2 ");

    delay (500);

  }

  else {

 

    tft.setCursor(0, 120);

    tft.print("  Instid 2 OK  ");

 

  }

  Vis = 0;

}

 

 

 

 

void Eeprom_hent (void) {

  int a;

  a = EEPROM.read(0);

  if (a < 100) {

    Diffon = a;

  }

  a = EEPROM.read(1);

  if (a < 100) {

    Diffoff = a;

  }

  a = EEPROM.read(2);     //Henter verdier i eeprom hvis der er gemt nogle

  if (a < 100) {

    Bundtankinst = a;

  }

  a = EEPROM.read(3);

  if (a != 255) {

    Instid1 = a;

  }

  a = EEPROM.read(4);

  if (a != 30) {

    Instid2 = a;

  }

}

 

 

 

 

 

 

 

 

void Input (void) {

 

 

  if (Tankbund - Diffon > Bundtankinst)

    Bundtank = 1;

 

 

  if (Tankbund - Diffoff < Bundtankinst)

    Bundtank = 0;

 

  if (Sol - Diffon > Tankbund)

    Soltank = 1;

 

  if (Sol - Diffoff < Tankbund)

    Soltank = 0;

 

  if (Sol - Diffon > Gulv)

    Solgulv = 1;

 

  if (Sol - Diffoff < Gulv)

    Solgulv = 0;

}

 

 

 

void Program (void) {

  if (Bundtank == 1) {

    if (Solgulv == 1) {

      Output = 1;

      Startertid1 = 0;

      Trin = 1;

      Tid1 = Instid1;

      Tid2 = Instid2;

      //   Pumper i gulv

 

 

    }

    else if (Solgulv == 0) {

      Startertid1 = 0;

      Output = 2;

      Trin = 2;

      Tid1 = Instid1;

      Tid2 = Instid2;

      //             Stopper alt

 

 

 

    }

  }

 

  if (Bundtank == 0 ) {

    if (Soltank == 1 ) {

      Startertid1 = 0;

      Output = 3;        //  Pumper i tank

      Trin = 3;

      Tid1 = Instid1;

      Tid2 = Instid2;

    }

    else if ((Solgulv == 1) && (Startertid1 == 0)) {

 

      Startertid1 = 1;                    // Starter tid1

 

 

 

 

    }

    else if ((Soltank == 0 ) && (Solgulv == 0))  {

      Output = 2;

      //    Stopper alt

      Trin = 6;

      Startertid1 = 0;

      Tid1 = Instid1;

      Tid2 = Instid2;

 

 

    }

 

  }

}

 

 

 

 

 

void Time (void) {

  int Pir = digitalRead(22);

 

  if (Pir == HIGH) {

    Tou = Led_Display;

 

  }

  /*  Serial.begin(9600);

    Serial.print("Pir ");

    Serial.println(Pir);

 

    Serial.print("T0u ");

    Serial.println(Tou);

    Serial.print("Tid1 = ");

    // Serial.println(Tid1);

    //  Serial.print("Tid2 = ");

    //Serial.println(Tid2);

 

  */

 

 

 

 

 

 

 

  if (Tou >= 0) {

    Tou -- ;

  }

 

 

  if (Tou >= 0) {

    digitalWrite(23, HIGH);

  }

  if (Tou <= 0) {

    digitalWrite(23, LOW);

  }

 

 

 

 

 

 

  if (millis() >= (previousTime)) {

    previousTime = previousTime + 1000;  // use 100000 for uS

    seconds = seconds + 1;

 

    if (seconds == 60) {

      seconds = 0;

      minutes = minutes + 1;

 

    }

 

 

    if (minutes == 60) {

      delay (80);

      minutes = 0;

 

    }

 

 

 

 

    if (Startertid1 == 1) {             // Sætter tid1 til instilling starter tid1 (lang tid)

      Tid1 = Instid1;

      Startertid1 = 2;

    }

    if (Startertid1 == 2) {

      Trin = 7;

      Output = 1;

      if (seconds == 0) {

        Tid1 = Tid1 - 1;                            // Tæller tid1 ned

 

 

      }

    }

    if ((Tid1 == 0) && (Startertid1 == 2)) {

      Startertid1 = 3;

      Tid2 = Instid2;                                // Sætter tid2 til instilling starter tid2 (kort tid)

      //Stopper sol pumpe for teste om stigene temp

      Tempsol = 0;

    }

    if (Startertid1 == 3 ) {

      Output = 2;

      Trin = 8;

      if (seconds == 0) {

        Tid2 = Tid2 - 1;

 

 

      }

    }

 

    if (((Tid2 == 0) &&  (Startertid1 == 3) && (Sol > Tempsol))) {

 

 

      Tid2 = Instid2;

      Tempsol = Sol;

    }

    if (((Tid2 <= 0) &&  (Startertid1 == 3) && (Sol <= Tempsol))) {

 

      Tid1 = Instid1;

      Startertid1 = 2;

 

 

 

    }

  }

 

}

 

 

 

void Out (void) {

 

 

 

  if (Output == 1) {

 

    Aktemp = Gulv;

    Pumpe();

    digitalWrite(4, HIGH); //Gulv 3 vejs ventil

 

   if (Pumpe_trin == 1){

    digitalWrite(1, HIGH); //Pumpe start trin 1

    digitalWrite(2, LOW);

    digitalWrite(3, LOW);

   }

   if (Pumpe_trin == 2){

    digitalWrite(1, HIGH); //Pumpe start trin 2

    digitalWrite(2, HIGH);

    digitalWrite(3, LOW);

   }

   if (Pumpe_trin == 3){

    digitalWrite(1, HIGH); //Pumpe start trin 3

    digitalWrite(2, HIGH);

    digitalWrite(3, HIGH);

   }

   /* digitalWrite(1, HIGH); //Sol pumpe on

    digitalWrite(2, HIGH); //Gulv pumpe on   //pumper i gulv

    digitalWrite(3, HIGH); //3 vejsventil on

 

*/

 

  }

  if (Output == 2) {

 

     pumpe=30;

    digitalWrite(1, LOW); //Trin 1 off

    digitalWrite(2, LOW); //Trin 2 off

    digitalWrite(3, LOW); //Trin 3 off

    digitalWrite(4, LOW); //3 vejsventil off

 

 

 

  }

 

  if (Output == 3) {

 

      digitalWrite(4, LOW); //3 vejsventil off (Gulv varme)

      Aktemp = Tankbund;

    Pumpe();

 

 

   if (Pumpe_trin == 1){

    digitalWrite(1, HIGH); //Pumpe start trin 1

    digitalWrite(2, LOW);

    digitalWrite(3, LOW);

   }

   if (Pumpe_trin == 2){

    digitalWrite(1, HIGH); //Pumpe start trin 2

    digitalWrite(2, HIGH);

    digitalWrite(3, LOW);

   }

   if (Pumpe_trin == 3){

    digitalWrite(1, HIGH); //Pumpe start trin 3

    digitalWrite(2, HIGH);

    digitalWrite(3, HIGH);

   }

 

 

 

 

/*

    digitalWrite(1, HIGH); //Sol pumpe on

    digitalWrite(2, LOW);

    digitalWrite(2, LOW);

    digitalWrite(4, LOW);       //Starter sol pumpe (Fylder i tank)

 

 

*/

 

 

  }

}

 

void VisButton(void)

{

 

  tft.fillRoundRect(0, 200, 50, 45, 20, RED);

  tft.fillRoundRect(60, 200, 50, 45, 20, RED);

  tft.fillRoundRect(130, 200, 50, 45, 20, RED);

  tft.fillRoundRect(200, 200, 50, 45, 20, RED);

  tft.fillRoundRect(270, 200, 50, 45, 20, RED);

 

  tft.setTextColor(WHITE);

  tft.setTextSize(2);

 

  tft.setCursor(1, 215);

  tft.println( "Side");

 

  tft.setCursor(61, 215);

  tft.println( "Inst");

 

  tft.setTextSize(4);

  tft.setCursor(145, 210);

  tft.println( "+");

 

  tft.setTextSize(4);

  tft.setCursor(215, 210);

  tft.println( "-");

 

  tft.setTextSize(2);

  tft.setCursor(277, 215);

  tft.println( "Gem");

 

}

 

 

 

 

 

 

void Vis_0 (void) {

 

 

  tft.begin();

  tft.setRotation(3);

  tft.setTextSize(3);

  tft.setTextColor(GREEN);

  tft.fillScreen(BLACK);

 

  tft.setCursor(0, 0);

  tft.print("Sol      = ");

 

  tft.setCursor(0, 30);

  tft.print("Tanktop  = ");

 

  tft.setCursor(0, 60);

  tft.print("Tankbund = ");

 

  tft.setCursor(0, 90);

  tft.print("Gulv     = ");

 

  tft.setCursor(0, 120);

  tft.print("Solretur = ");

 

  tft.setCursor(0, 150);

  tft.print("UR = ");

 

 

 

 

  Set = 1;

  VisButton();

  Vis = 1;

}

 

void Vis_1 (void) {

 

 

  tft.begin();

  tft.setTextSize(3);

  tft.setRotation(3);

 

  tft.setTextColor(GREEN, BLACK);

 

  tft.setCursor(190, 0);

  tft.print(Sol, 1);

  tft.setCursor(300, 0);

  tft.println("C");

  tft.setCursor(190, 30);

  tft.println(Tanktop, 1);

  tft.setCursor(300, 30);

  tft.println("C");

  tft.setCursor(190, 60);

  tft.println(Tankbund, 1);

  tft.setCursor(300, 60);

  tft.println("C");

  tft.setCursor(190, 90);

  tft.println(Gulv, 1);

  tft.setCursor(300, 90);

  tft.println("C");

  tft.setCursor(190, 120);

  tft.println(Solretur, 1);

  tft.setCursor(300, 120);

  tft.println("C");

  tft.setCursor(100, 150);

  if (minutes < 10) { //PRINT A 0 IN FRONT OF THE SECONDS IF LESS THAN 10

    tft.print('0');

    tft.print(minutes, DEC);

  }

  else {

    tft.print(minutes, DEC);

 

  }

 

  // tft.print (minutes, DEC);

  tft.print (":");

  if (seconds < 10) { //PRINT A 0 IN FRONT OF THE SECONDS IF LESS THAN 10

    tft.print('0');

    tft.print(seconds, DEC);

  }

  else {

    tft.print(seconds, DEC);

 

  }

  if (Trin == 1) {

    tft.setCursor(0, 180);

    tft.setTextColor(YELLOW, BLACK);

    tft.setTextSize(2);

    tft.println("I gulv                   ");

 

  }

  if ((Trin == 2) || ( Trin == 6)) {

    tft.setCursor(0, 180);

    tft.setTextColor(YELLOW, BLACK);

    tft.setTextSize(2);

    tft.println("Stopper                  ");

 

  }

  if (Trin == 3) {

    tft.setCursor(0, 180);

    tft.setTextColor(YELLOW, BLACK);

    tft.setTextSize(2);

    tft.println("I Tank                   ");

 

  }

  if (Trin == 7) {

    tft.setCursor(0, 180);

    tft.setTextColor(YELLOW, BLACK);

    tft.setTextSize(2);

    tft.println("I gulv i x tid1          ");

 

  }

  if (Trin == 8) {

    tft.setCursor(0, 180);

    tft.setTextColor(YELLOW, BLACK);

    tft.setTextSize(2);

    tft.println("Pause for at temp stiger");

 

  }

 

 

 

 

}

 

 

void Vis_2 (void) {

 

 

 

  tft.begin();

  tft.setRotation(3);

  tft.setTextSize(3);

  tft.setTextColor(GREEN);

  tft.fillScreen(BLACK);

 

 

  tft.setCursor(0, 30);

  tft.print("Bund Tank= ");

 

  tft.setCursor(0, 60);

  tft.print("Diff on  = ");

 

  tft.setCursor(0, 90);

  tft.print("Diff off = ");

 

  tft.setCursor(0, 120);

  tft.print("Tid 1    = ");

 

  tft.setCursor(0, 150);

  tft.print("Tid 2    = ");

  VisButton();

  Vis = 3;

 

}

void Vis_3 (void) {

 

 

  tft.begin();

 

  tft.setTextSize(3);

  delay (80);

  tft.setRotation(3);

  tft.setTextColor(YELLOW, BLACK);

  tft.setCursor(0, 0);

  tft.print(Bundtank);

  tft.setCursor(30, 0);

  tft.println("BT");

  tft.setCursor(105, 0);

  tft.println(Soltank);

  tft.setCursor(135, 0);

  tft.println("ST");

  tft.setCursor(205, 0);

  tft.println(Solgulv);

  tft.setCursor(235, 0);

  tft.println("SG");

  tft.setTextSize(2);

  tft.setCursor(80, 180);

  tft.println(Trin);

  tft.setCursor(0, 180);

  tft.println("Trin");

  tft.setCursor(240, 180);

  tft.println(Output);

  tft.setCursor(120, 180);

  tft.println("Output");

 

 

 

  tft.setTextSize(3);

  tft.setCursor(200, 30);

  tft.println(Bundtankinst, 1);

  tft.setCursor(200, 60);

  tft.println (Diffon, 1);

  tft.setCursor(200, 90);

  tft.println(Diffoff, 1);

 

  tft.setCursor(200, 120);

 

  tft.println(Instid1, 1);

 

  tft.setCursor(200, 150);

 

  tft.println(Instid2, 1);

 

 

  if (Inst == 0) {

 

  tft.fillRect(300, 60, 90, 20, BLACK);

    tft.fillRect(300, 30, 90, 20, RED);

    tft.setTextSize(2);

    tft.setTextColor(WHITE);

  tft.setCursor(277, 215);

  tft.println( "Gem");

  tft.setTextColor(BLACK);

  tft.setCursor(277, 5);

  tft.println( "OUT");

 

  }

  if (Inst == 1) {

    tft.fillRect(300, 30, 90, 20, BLACK);

   tft.fillRect(300, 90, 90, 20, BLACK);

    tft.fillRect(300, 60, 90, 20, RED);

  }

  if (Inst == 2) {

 

    tft.fillRect(300, 60, 90, 20, BLACK);

   tft.fillRect(300, 120, 90, 20, BLACK);

    tft.fillRect(300, 90, 90, 20, RED);

  }

  if (Inst == 3) {

    tft.fillRect(300, 90, 90, 20, BLACK);

   tft.fillRect(300, 150, 90, 20, BLACK);

    tft.fillRect(300, 120, 90, 20, RED);

 

  }

  if (Inst == 4) {

    tft.fillRect(300, 120, 90, 20, BLACK);

    tft.fillRect(300, 150, 90, 20, RED);

tft.setTextSize(2);

    tft.setTextColor(WHITE);

  tft.setCursor(277, 215);

  tft.println( "Gem");

  }

 

 if (Inst == 5) {

   tft.fillRect(300, 30, 90, 20, BLACK);

    tft.fillRect(300, 150, 90, 20, BLACK);

    tft.setTextSize(2);

    tft.setTextColor(GREEN);

  tft.setCursor(277, 215);

  tft.println( "Gem");

 tft.setTextColor(BLACK);

  tft.setCursor(277, 5);

  tft.println( "OUT");

//Gem

  }

   if (Inst == 6) {

    tft.fillRect(300, 30, 90, 20, BLACK);

    tft.setTextSize(2);

    tft.setTextColor(RED);

  tft.setCursor(277, 5);

  tft.println( "OUT");

  tft.setTextColor(WHITE);

  tft.setCursor(277, 215);

  tft.println( "Gem");

//Gem

  }

}

void Vis_4 (void) {

 

  tft.begin();

  tft.setRotation(3);

  tft.setTextSize(3);

  tft.setTextColor(GREEN);

  tft.fillScreen(BLACK);

 

 

  tft.setCursor(0, 30);

  tft.print("Instid 1 = ");

 

  tft.setCursor(0, 60);

  tft.print("Tid1     = ");

 

  tft.setCursor(0, 90);

  tft.print("Instid 2 = ");

 

  tft.setCursor(0, 120);

  tft.print("Tid2     = ");

 

  tft.setCursor(0, 150);

  tft.print("UR = ");

 

  VisButton();

  Vis = 5;

}

 

void Vis_5 (void) {

 

 

  tft.begin();

 

  tft.setTextSize(3);

  tft.setRotation(3);

  tft.setTextColor(GREEN, BLACK);

  if (Trin == 1) {

    tft.setCursor(0, 0);

    tft.setTextColor(YELLOW, BLACK);

    tft.setTextSize(2);

    tft.println("I gulv                   ");

 

  }

  if ((Trin == 2) || ( Trin == 6)) {

    tft.setCursor(0, 0);

    tft.setTextColor(YELLOW, BLACK);

    tft.setTextSize(2);

    tft.println("Stopper                  ");

 

  }

  if (Trin == 3) {

    tft.setCursor(0, 0);

    tft.setTextColor(YELLOW, BLACK);

    tft.setTextSize(2);

    tft.println("I Tank                   ");

 

  }

  if (Trin == 7) {

    tft.setCursor(0, 0);

    tft.setTextColor(YELLOW, BLACK);

    tft.setTextSize(2);

    tft.println("I gulv i x tid1          ");

 

  }

  if (Trin == 8) {

    tft.setCursor(0, 0);

    tft.setTextColor(YELLOW, BLACK);

    tft.setTextSize(2);

    tft.println("Pause for at temp stiger");

 

  }

 

  tft.setTextColor(GREEN, BLACK);

  tft.setTextSize(3);

 

  tft.setCursor(200, 30);

 

  tft.println(Instid1, 1);

  tft.setCursor(200, 60);

 

  tft.println(Tid1, 1);

  tft.setCursor(200, 90);

 

  tft.println(Instid2, 1);

 

  tft.setCursor(200, 120);

 

  tft.println(Tid2, 1);

 

  tft.setCursor(100, 150);

  if (minutes < 10) { //PRINT A 0 IN FRONT OF THE SECONDS IF LESS THAN 10

    tft.print('0');

    tft.print(minutes, DEC);

  }

  else {

    tft.print(minutes, DEC);

 

  }

 

  // tft.print (minutes, DEC);

  tft.print (":");

  if (seconds < 10) { //PRINT A 0 IN FRONT OF THE SECONDS IF LESS THAN 10

    tft.print('0');

    tft.print(seconds, DEC);

  }

  else {

    tft.print(seconds, DEC);

 

  }

 

}

 

 

 

void Touch (void) {

 

 

 

  //if (pp.y > 200 && pp.y < 230  && pp.x > 3 && pp.x < 47) {

  tft.setRotation(3);

  TS_Point p = ts.getPoint();

 

  //Serial.print(", y =  ");

  //Serial.print(p.y);

  //Serial.print(", x =  ");

  //Serial.println(p.x);

  //delay(30);

 

 

  if (p.y > 3205 && p.y < 3837  && p.x > 400 && p.x < 903) {  //Side

 

    if (Vis == 1) {

      Vis = 2;

    }

    if (Vis == 3) {

      Vis = 4;

    }

    if (Vis == 5) {

      Vis = 6;

    }

    if (Vis == 6) {

      Vis = 0;

    }

 

  //  Serial.print("Vis");  //  TS_Point p = ts.getPoint();

    //Serial.println(Vis);

 

 

  }

 

  if (p.y > 3269 && p.y < 3795  && p.x > 1030 && p.x < 1687) { //Inst

 

    Inst++;

    if (Inst >= 6) {

      Inst = 0;

    }

 

 

  }

 

  if (Vis == 3) {

    if (p.y > 3500 && p.y < 3600  && p.x > 2014 && p.x < 22050) { //+ tasten

 

 

      if ((Inst == 0) && (Bundtankinst <= 100)) {

        Bundtankinst = Bundtankinst + 1;

      }

      if ((Inst == 1) && (Diffon < 20)) {

        Diffon = Diffon + 1 ;

 

      }

      if ((Inst == 2) && (Diffoff < 20)) {

        Diffoff = Diffoff + 1;

      }

      if ((Inst == 3) && (Instid1 < 180)) {

        Instid1 = Instid1 + 1;

      }

      if ((Inst == 4) && (Instid2 < 30)) {

        Instid2 = Instid2 + 1;

      }

      // Stil  +

 

 

 

    }

 

 

    if (p.y > 3450 && p.y < 3511 && p.x > 2730 && p.x < 2780) {  // - Tasten

 

 

      if ((Inst == 0) && (Bundtankinst <= 100)) {

        Bundtankinst = Bundtankinst - 1;

      }

      if ((Inst == 1) && (Diffon > 0)) {

        Diffon = Diffon - 1;

 

      }

      if ((Inst == 2) && (Diffoff > 0)) {

        Diffoff = Diffoff - 1;

      }

      if ((Inst == 3) && (Instid1 > 0)) {

        Instid1 = Instid1 - 1;

      }

      if ((Inst == 4) && (Instid2 > 0)) {

        Instid2 = Instid2 - 1;

      }

 

      // Stil -

 

      //Serial.print("Diffon ");

      //Serial.print(Diffon);

      //Serial.print("Inst ");

      //Serial.print(Inst);

 

    }

  }

 

 

  if (p.y > 3185 && p.y < 3779 && p.x > 3357 && p.x < 3967 ) { // Gem

    Gem_hent = 2;

 

  }

}

void menuNav(byte dir)

{

  //LEFT

   if  ((dir == 0)&&(Los==2)){

         if ((Inst == 0) && (Bundtankinst <= 100)) {

        Bundtankinst = Bundtankinst - 1;

      }

      if ((Inst == 1) && (Diffon > 0)) {

        Diffon = Diffon - 1;

      }

      if ((Inst == 2) && (Diffoff > 0)) {

        Diffoff = Diffoff - 1;

      }

      if ((Inst == 3) && (Instid1 > 0)) {

        Instid1 = Instid1 - 1;

      }

      if ((Inst == 4) && (Instid2 > 0)) {

        Instid2 = Instid2 - 1;

      }

   }

 

 

   if ((dir == 0)&&(Los==1)){

 

    Inst--;

    if (Inst < 0) {

      Inst = 6;

    }

   }

 

  if ((dir == 0)&&(Los==0))

  {

    if (Vis == 1) {

    Vis = 2;

  }

  if (Vis == 3) {

    Vis = 4;

  }

  if (Vis == 5) {

    Vis = 6;

  }

  if (Vis == 6) {

    Vis = 0;

  }

 

 

  }

 

 

  //RIGHT

 if  ((dir == 1)&&(Los==2)){

 

    if ((Inst == 0) && (Bundtankinst <= 100)) {

        Bundtankinst = Bundtankinst + 1;

      }

      if ((Inst == 1) && (Diffon < 20)) {

        Diffon = Diffon + 1 ;

 

      }

      if ((Inst == 2) && (Diffoff < 20)) {

        Diffoff = Diffoff + 1;

      }

      if ((Inst == 3) && (Instid1 < 180)) {

        Instid1 = Instid1 + 1;

      }

      if ((Inst == 4) && (Instid2 < 30)) {

        Instid2 = Instid2 + 1;

      }

      // Stil  +

 }

 

 

  if ((dir == 1)&&(Los==1)){

 

    Inst++;

    if (Inst >= 7) {

      Inst = 0;

    }

  }

  {

 

  }

  if ((dir == 1)&&(Los==0))

 

  {

 

 

 

  if (Vis == 1) {

    Vis = 6;

  }

  if (Vis == 3) {

    Vis = 0;

  }

  if (Vis == 5) {

    Vis = 2;

  }

  if (Vis == 6) {

    Vis = 4;

  }

 

  }

 

}

 

void buttonNav()

{

if (((Los ==1)&&(Vis ==3)&&(Inst==6))) {

Los = 0;

}

else if (((Los ==1)&&(Vis ==3)&&(Inst==5))) {

   Gem_hent = 2;

 

}

 else if ((Los == 1)&&(Vis ==3)) {

   Los =2;

 

  }

 else if ((Los == 0)&&(Vis ==3)) {

   Los =1;

 

  }

   else if ((Los == 2)&&(Vis ==3)) {

   Los =1;

 

  }

  else  {

   Los =0;

  }

 

}

 

void button_ISR()

{

 

  if (digitalRead(buttonPin) == HIGH)

 

  {

 

    buttonPressed = 1;

    buttonNav();

    delay (500);

  }

 

  else {

    delay (500);

    buttonPressed = 0;

 

 

  }

}

 

 

void Pumpe (void)

{

  /*

    Serial.begin(9600);

    Serial.print("pumpe ");

    Serial.println(pumpe);

Serial.print("Diff_A ");

    Serial.println(Diff_A);

 

 delay (200);

 */

  if (pumpe  >= 1){

       Pumpe_trin = 3;

 //     Serial.print("Pumpe_trin= ");

  //  Serial.println(Pumpe_trin);

       pumpe--;

  }

  if (pumpe==0){

 /*

    Serial.print("Pumpe_trin1= ");

    Serial.println(Pumpe_trin1);

 Serial.print(" Pumpe_trin2= ");

    Serial.println(Pumpe_trin2);

 Serial.print("Pumpe_trin= ");

    Serial.println(Pumpe_trin);

 delay (500);

*/

  if (Solretur - Pumpe_diff > Aktemp)

     Pumpe_trin1 = 1;

 

  if (Solretur - Pumpe_diff + Diff_A < Aktemp)

     Pumpe_trin1 = 0;

 

  if (Solretur - Pumpe_diff * 2 > Aktemp)

     Pumpe_trin2 = 1;

 

  if (Solretur - Pumpe_diff * 2 + Diff_A < Aktemp) // (Aktemp) aktuel Temp Gulv eller Tankbund

    Pumpe_trin2 = 0;

 

   if ((Pumpe_trin1 == 0)&&(Pumpe_trin2 == 0)) {

       Pumpe_trin = 1;

   }

   if ((Pumpe_trin1 == 1)&&(Pumpe_trin2 == 0)) {

 

       Pumpe_trin = 2;

  }

   if ((Pumpe_trin1 == 1)&&(Pumpe_trin2 == 1)) {

 

       Pumpe_trin = 3;

  }

   }

 delay (300);

};

 

 

 

 

 

 

 

 

 

 

 

 

 float Middel_AD (int channel){

  int x=0;

  float temp=0;

  //Serial.begin(9600);

   // Serial.print("channel ");

    //Serial.println(channel);

 

 //delay (500);

 

  for (x=8; x>=0; x--){

    temp_AD[channel-1][x+1] = temp_AD[channel-1][x];  //flyt data  en op

  //Serial.print("x ");

    //Serial.println(x);

    //delay (500);

  };

  //Serial.print("temp ");

    //Serial.println(temp);

    //delay (500);

  temp_AD[channel-1][0] = AD[channel];     // indlæs ny data AD[channel]*100/255;analogRead(A4);

 

  for (x=0; x<=9; x++){

    //Serial.print("temp ");

    //Serial.println(temp);

    //delay (500);

    temp = temp + temp_AD[channel-1][x];           // summen af data

 Serial.println(temp);

  };

  temp = abs(temp / 10);                        // kun heltals delen

 

  return(temp);

};

 

 

 

 

 

 

 

void loop () {

 

 

  int newEnc1 = enc1.read() / 4;

  if (newEnc1 > 0)  //left

  {

 

    enc1.write(0);

    menuNav(1);

  }

  if (newEnc1 < 0) //right

  {

 

    enc1.write(0);

    menuNav(0);

  }

 

  if (ts.touched()) {

    Tou = Led_Display; //Tid der gÃ¥rd igen skærm slukker

 

    Touch ();

  }

    Sol = Middel_AD(1);

    Tankbund = Middel_AD(2);

    Tanktop = Middel_AD(3);

    Gulv = Middel_AD(4);

    Solretur = Middel_AD(5);

   // A6 = Middel_AD(6);

    //Solretur = Middel_AD(7);

    //A8 = Middel_AD(8);

 

  Time();

  Program ();

  Read ();

  //Pumpe ();

  if (Gem_hent == 2) {

    Eeprom_gem ();

    delay (1000);

    Gem_hent = 0;

     Vis = 2;

  }

  Input ();

  Out ();

//Serial.println("Vis");

 //Serial.println(Vis);

 //Serial.println("Los");

//Serial.println(Los);

 //Serial.println("Inst");

//Serial.println(Inst);

  if (Vis == 0) {

    Vis_0 ();

  }

  if (Vis == 1) {

    Vis_1 ();

  }

  if (Vis == 2) {

    Vis_2 ();

  }

  if (Vis == 3) {

    Vis_3 ();

  }

  if (Vis == 4) {

    Vis_4 ();

  }

  if (Vis == 5) {

    Vis_5 ();

  }

 

 

}

Display mit Touchscreen ILI9341 Arduino

Teensy 3.2