Arduino Mega, Touch, Motor, Skript, Hilfe

Hallo zusammen,
bitte um Hilfe, kurz zum Vorhaben:
Wir haben eine Maschine die wir mit einem Arduino Mega betrieben möchten. Sie sollte über einen Motor eine Welle drehen, die Welle sollte bei einer umdrehung anhalten, deswegen ist ein Taster (Grundstellung) verbaut. Noch ein weiter Taster(Karton eingelegt) Prüft ob etwas eingelegt ist.

Verwendet wurde:
-Arduino Mega
-TFT Touch Dispay 2.8 Zoll (Conrad Elegoo)
-2x Taster NC / NO
....

Anbei ist unser Code, leider funktioniert, wenn ich in dem Menüpunkt POS bing das Grundstellung fahren nicht, also wenn ich auf "fahren" drücke geht leider nichts.
Der Motor sollte mit PWM(pin 44) gesteuert werden, bzgl. der Drehzahl. Wir haben zusätzlich den pin 40 mit eingefügt, leider funktioniert dies auch nicht, auf dem pin liegt immer 5v an.

Kann mir jemand bei dem Code weiterhelfen?


#include <Elegoo_GFX.h>    // Core graphics library
#include <Elegoo_TFTLCD.h> // Hardware-specific library

#include <TouchScreen.h>
#include <SPI.h>
#include <SD.h>


#define YP A3  // must be an analog pin, use "An" notation!
#define XM A2  // must be an analog pin, use "An" notation!
#define YM 9   // can be a digital pin
#define XP 8   // can be a digital pin

//Touch For New ILI9341 TP
#define TS_MINX 120
#define TS_MAXX 900

#define TS_MINY 70
#define TS_MAXY 920

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

//PImage logo;

#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
// optional
#define LCD_RESET A4

// Assign human-readable names to some common 16-bit color values:
#define	BLACK   0x0000
#define	BLUE    0x001F
#define	RED     0xF800
#define	GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF
#define GRAY    0xDEFB

//#define SD_CS 10  //SD card pin on your shield

Elegoo_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);



int Motor = 44;
int motor1 = 40;
int posWelle = 22;
int KartonEingelegt = 32;
int WelleAusGrundstellung = 26;
int KeineSchachtelEingelegt = 34;

int j = 0;
int n = 0;
//int motor = 0;
int grundstellung = 0;
int start = 0;
int GrundstellungFahren = 0;
int taktWelle = 0;
int karton_eingelegt = 0;
int WelleGedreht = 0;
int MotorGelaufen = 0;
int TasterF1 = 0;
int TasterF2 = 0;
int turn = 0;
int KeineGrundstellung = 0;
int KeineSchachtel = 0;
int Startfreigabe = 0;
int Bildschirm5 = 0;

//long crontimer = millis();
//String runtime = "00:00:00";
//int t_sec = 0;
//int t_min = 0;
//int t_hour = 0;
int fbutton_id = 0;
//int clickcounter = 0;
int screen_id = 20;
int value_bar = 30;
//boolean runstop = true;

int sprache = 3;








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



  tft.reset();





 uint16_t identifier = tft.readID();

  if (identifier == 0x9325) {
    Serial.println(F("Found ILI9325 LCD driver"));
  } else if (identifier == 0x9328) {
    Serial.println(F("Found ILI9328 LCD driver"));
  } else if (identifier == 0x4535) {
    Serial.println(F("Found LGDP4535 LCD driver"));
  } else if (identifier == 0x7575) {
    Serial.println(F("Found HX8347G LCD driver"));
  } else if (identifier == 0x9341) {
    Serial.println(F("Found ILI9341 LCD driver"));
  } else if (identifier == 0x8357) {
    Serial.println(F("Found HX8357D LCD driver"));
  } else if (identifier == 0x0101)
  {
    identifier = 0x9341;
    Serial.println(F("Found 0x9341 LCD driver"));
  } else {
    Serial.print(F("Unknown LCD driver chip: "));
    Serial.println(identifier, HEX);
    Serial.println(F("If using the Elegoo 2.8\" TFT Arduino shield, the line:"));
    Serial.println(F("  #define USE_Elegoo_SHIELD_PINOUT"));
    Serial.println(F("should appear in the library header (Elegoo_TFT.h)."));
    Serial.println(F("If using the breakout board, it should NOT be #defined!"));
    Serial.println(F("Also if using the breakout, double-check that all wiring"));
    Serial.println(F("matches the tutorial."));
    identifier = 0x9341;
  }


  tft.begin(identifier);
  tft.setRotation(1);
  screen_welcome();


  pinMode (Motor, OUTPUT);
  pinMode (posWelle, INPUT);
  pinMode (KartonEingelegt, INPUT);
  pinMode (motor1, OUTPUT);
  pinMode (WelleAusGrundstellung, INPUT);
  pinMode (KeineSchachtelEingelegt, INPUT);




 // spi_save = SPCR;


  //  logo = TFTscreen.loadImage("Logo_groß.bmp");

  //EsploraTFT.image (logo, 0, 0);
}

#define MINPRESSURE 5
#define MAXPRESSURE 1000




int x = 0;
int y = 0;


void loop()
{

if ((digitalRead(posWelle) == 1 && start == 0 && Startfreigabe == 0) || (WelleGedreht == 0 && Startfreigabe == 0 ) || (digitalRead(posWelle) == 1 && GrundstellungFahren == 0 && Startfreigabe == 0)) {
    analogWrite(Motor, LOW);
    digitalWrite (motor1, LOW);
   /* if(MotorGelaufen = 1){
      start = 0;
    }*/
    GrundstellungFahren = 0;
    //  WelleGedreht = 0;
  }



  karton_eingelegt = digitalRead(KartonEingelegt);


  grundstellung = digitalRead(posWelle);


  KeineGrundstellung = digitalRead(WelleAusGrundstellung);

  KeineSchachtel = digitalRead(KeineSchachtelEingelegt);
  Serial.println(karton_eingelegt);
  Serial.println(grundstellung);
  Serial.println(KeineGrundstellung);
  Serial.println(KeineSchachtel);

  if (grundstellung == 1) {
    WelleGedreht = 0;
  }
  if (grundstellung == 0) {
    WelleGedreht = 1;
  }

  TSPoint p = ts.getPoint();
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);


  if (grundstellung == 1) {
    turn = 0;
  }

  if (KeineGrundstellung == 0) {
    turn = 1;
  }






  //  Serial.println(taktWelle("Takt Welle"));

if(screen_id == 20){
  delay(6000);
  screen_main();
}

  if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {

    // scale from 0->1023 to tft.width
    p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
    p.y = (tft.height() - map(p.y, TS_MINY, TS_MAXY, tft.height(), 0));

    //Display received X an Y value from touch
    Serial.print("(x "); Serial.print(p.x);
    Serial.print(", y "); Serial.print(p.y);
    Serial.println(")");




if(screen_id == 30) {
  
if(p.x > 60 && p.x < 100 && p.y < 220 && p.y > 90){
  sprache = 3;
  screen_main();
}

if(p.x > 135 && p.x < 175  && p.y < 220 && p.y > 90){
  sprache = 1;
  screen_main();
}

if(p.x > 210 && p.x < 260  && p.y < 220 && p.y > 90){
  sprache = 2;
  screen_main();
}

if (p.x > 200 && p.x < 250  && p.y < 75 && p.y > 20) {
        screen_main();
        //Action on F1-Screen zurück
        }
}




    if (screen_id == 0) {//Get touch on Main-Screen

      if (p.x > 280 && p.y > 180) {
        fbutton_id = 1;
        screen_f1();                                                  //Grundstellung fahren
      }

      if (p.x > 280 && p.y < 180 && p.y > 60) {
        fbutton_id = 2;
        screen_f2();
      }

      if (p.x > 280  && p.y < 60 ) {
        fbutton_id = 3;
        screen_f3();                                                   //Motor Geschwindigkeit einstellen
      }

if(p.x < 50  && p.y < 120 ){
  screen_sprache();
}



    }


    else if (screen_id == 1) { //F1 screen action
      Serial.print(KeineGrundstellung);
      if (p.x > 200 && p.x < 250 && p.y < 220 && p.y > 130  && grundstellung == 0) {// && KeineGrundstellung == 1
        analogWrite(Motor, ((20 + 100) / 2) * 2.55);
     // if(GrundstellungFahren = 1 && digitalRead(WelleAusGrundstellung) == 1){
        digitalWrite (motor1, HIGH);
//}
        TasterF1 = 1;

        //Action on F1-Screen Grundstellung fahren
      }

//digitalWrite (motor1, HIGH);
      else {
        TasterF1 = 0;
      }

      if (p.x > 200 && p.x < 250  && p.y < 75 && p.y > 20) {
        screen_main();
        //Action on F1-Screen zurück
      }
    }


/*karton_eingelegt = digitalRead(KartonEingelegt);


  grundstellung = digitalRead(posWelle);


  KeineGrundstellung = digitalRead(WelleAusGrundstellung);

  KeineSchachtel = digitalRead(KeineSchachtelEingelegt);*/


    else if (screen_id == 2) { //F2 screen action
      //
      if (p.x > 200 && p.x < 250 && p.y < 220 && p.y > 160) {
        
        
        if (digitalRead(posWelle) == 0 && digitalRead(KeineSchachtelEingelegt) == 0 && MotorGelaufen == 0 && digitalRead(KartonEingelegt) == 1 && digitalRead(WelleAusGrundstellung) == 0) { //
          screen_f5();
          Bildschirm5 = 1;
          Startfreigabe = 1;
          TasterF2 = 1;
        }

        if (taktWelle == 0 && digitalRead(posWelle) == 0 && digitalRead(WelleAusGrundstellung) == 0) {
          taktWelle = 1;
        }
        //  }
        //Action on F2-Screen Start falten
      }

  

      if (p.x > 200 && p.x < 250  && p.y < 75 && p.y > 20) {
        screen_main();
        //Action on F2-Screen zurück
      }
    }

    else if (screen_id == 3) { //F3 screen action
      if (p.x > 200 && p.x < 250 && p.y < 155 && p.y > 95) {
        screen_main();
        //Action on F3-Screen ok
      }
      if (p.x > 130 && p.x < 170 && p.y < 225 && p.y > 13) {
        value_bar = (216 - p.y + 8) / 2.1;
        tft.fillRect(15 + value_bar * 2.9, 100, 290 - value_bar * 2.9, 30, WHITE);
        tft.fillRect(15, 100, value_bar * 2.9, 30, BLUE);
        tft.fillRect(120, 40, 120, 40 , WHITE);
        tft.setCursor(125, 50);
        tft.setTextColor(BLACK);
        tft.setTextSize(3);
        tft.print(value_bar);
        tft.println("%");
        //Action if value-bar changed
        n = value_bar;
      }
    }




    else if (screen_id == 6) {

      if (p.x > 200 && p.x < 250 && p.y < 150 && p.y > 95) {
        // if (taktWelle == 1 && grundstellung == 1) {
        screen_f2();
        // }
      }
    }




  }

  if (screen_id == 5 && TasterF2 == 1 && digitalRead(WelleAusGrundstellung) == 0 && MotorGelaufen == 1 && digitalRead(posWelle) == 1)  {//
    //  delay (3600);


  if (start == 1) {
    analogWrite(Motor, ((value_bar + 100) / 2) * 2.55);
    digitalWrite (motor1, HIGH);
    //}//  WelleGedreht = 1;

    if (digitalRead(posWelle) == 0 && MotorGelaufen == 1) {
      taktWelle = 0;
    }



    if (digitalRead(posWelle) == 1) {
      MotorGelaufen = 1;
    }
    delay(200);
  }

    
    if (digitalRead(WelleAusGrundstellung) == 0 || digitalRead(posWelle) == 0) { //&& digitalRead(WelleAusGrundstellung) == 0
      screen_f6();
      MotorGelaufen = 0;
      TasterF2 = 0;
      start = 0;
      taktWelle = 0;
      turn = 0;
      Startfreigabe = 0;
    }
  }

 





  if (digitalRead(posWelle) == 0 || TasterF1 == 0) {
    GrundstellungFahren = 0;
  }

  if (TasterF1 == 1 && digitalRead(posWelle) == 1) {
    GrundstellungFahren = 1;
    TasterF1 = 0;
  }

  if (TasterF2 == 1 && taktWelle == 1 && Startfreigabe == 1) {  //|| (TasterF2 && KeineGrundstellung == 0 && grundstellung == 1 && karton_eingelegt == 1 && KeineSchachtel == 0)) { //&& karton_eingelegt == 1 && taktWelle == 1 && KeineGrundstellung == 1) || (TasterF2 == 1 && karton_eingelegt == 1 && MotorGelaufen == 0 && KeineGrundstellung == 1) {
    start = 1;
    //TasterF2 = 0;

  }


/*karton_eingelegt = digitalRead(KartonEingelegt);


  grundstellung = digitalRead(posWelle);


  KeineGrundstellung = digitalRead(WelleAusGrundstellung);

  KeineSchachtel = digitalRead(KeineSchachtelEingelegt);*/
  // Motor ansteuerung





if ((digitalRead(posWelle) == 1 && start == 0 && Startfreigabe == 0) || (WelleGedreht == 0 && Startfreigabe == 0 ) || (digitalRead(posWelle) == 1 && GrundstellungFahren == 0 && Startfreigabe == 0)) {
    analogWrite(Motor, 0);
    digitalWrite (motor1, 0);
   // if(MotorGelaufen = 1){
   //   start = 0;
   // }
    GrundstellungFahren = 0;
    //  WelleGedreht = 0;
  }

  else if (start == 1) {

    digitalWrite (motor1, HIGH);
    //}//  WelleGedreht = 1;

    if (digitalRead(posWelle) == 1 && MotorGelaufen == 1) {
      taktWelle = 0;
    }



    if (digitalRead(WelleAusGrundstellung) == 1) {
      MotorGelaufen = 1;
    }
    delay(500);
  }


}






//Display Seiten




// Willkommen
void screen_welcome() {
  screen_id = 20;
 // bmpDraw("Sotec-Logo-groß.bmp", 0, 0);
  tft.fillScreen(BLACK);
  //  tft.fillScreen(screen_f1);
  tft.setCursor(10, 50);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.println(" Willkommen");
  tft.println(" ");
  tft.println("     Welcome"   );
   tft.println(" ");
  tft.println("        Bienvenue"   );

 
}


//Build MAIN screen
void screen_main() {
  screen_id = 0;
  tft.fillScreen(BLACK);





  //Show F3 box value
  tft.setCursor(195, 154);
  tft.setTextColor(BLUE);
  tft.setTextSize(1);
  tft.println("Gechwindigkeit:");
  tft.setCursor(200, 170);
  tft.setTextColor(GRAY);
  tft.setTextSize(3);
  tft.print(value_bar);
  tft.println(" %");


  //Build F1 button
  tft.fillRect(0, 200, 80, 40, GRAY);
  tft.setCursor(15, 210);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.println("Pos");

  //Build F2 button
  

  if(sprache == 1){
  
  tft.fillRect(80, 200, 140, 40, WHITE);
  tft.setCursor(108, 210);
  tft.setTextColor(GREEN);
  tft.setTextSize(3);
  tft.println("launch");
 }
 if(sprache == 2){
  
  tft.fillRect(80, 200, 140, 40, WHITE);
  tft.setCursor(82, 210);
  tft.setTextColor(GREEN);
  tft.setTextSize(3);
  tft.println("commencer");
 }
 if(sprache == 3){
  
  tft.fillRect(80, 200, 140, 40, WHITE);
  tft.setCursor(120, 210);
  tft.setTextColor(GREEN);
  tft.setTextSize(3);
  tft.println("Start");
 }


  //Build F3 button
  tft.fillRect(240, 200, 80, 40, GRAY);
  tft.setCursor(265, 210);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.println("SP");

  //Build Language button
  tft.fillRect(160, 0, 160, 40, GRAY);
  tft.setCursor(170, 10);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
 if(sprache == 1){
  tft.println("language");
 }
 if(sprache == 2){
  tft.println("Langue");
 }
 if(sprache == 3){
  tft.println("Sprache");
 }

}


void screen_sprache() {
  screen_id = 30;
  tft.fillScreen(BLACK);
  //  tft.fillScreen(screen_f1);



  //Generate YES button
tft.fillRect(20, 50, 170, 40, GRAY);
    
    tft.setCursor(25, 60);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.println("Deutsch");

  
    tft.fillRect(20, 100, 170, 40, GRAY);
    
    tft.setCursor(25, 110);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.println("English");

    tft.fillRect(20, 150, 170, 40, GRAY);
  
  tft.setCursor(25, 160);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.println("France");

  //Generate NO button
  tft.fillRect(215, 150, 85, 40, GRAY);
  tft.setCursor(223, 160);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.println("Menu");
  // screen_f1();
}


//Seite 01 F1
void screen_f1() {
  screen_id = 1;
  tft.fillScreen(BLACK);
  //  tft.fillScreen(screen_f1);
  tft.setCursor(20, 50);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);

if(sprache == 1){ //englisch
  tft.println("  Drive in");
  tft.println("basic position");
 }
 if(sprache == 2){ // französisch
  tft.println("  Conduire en");
  tft.println(" position de base");
 }
 if(sprache == 3){ // deutsch
  tft.println("In Grundstellung");
  tft.println("     fahren");
 }
  


  //Generate YES button
  if (KeineGrundstellung == 0 || grundstellung == 1) {
    tft.fillRect(20, 150, 130, 40, GRAY);
  }
  else {
    tft.fillRect(20, 150, 130, 40, BLUE);
  }
  tft.setCursor(33, 160);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);

if(sprache == 1){
  tft.println("drive");
 }
 if(sprache == 2){
  tft.println("conduire");
 }
 if(sprache == 3){
  tft.println("fahren");
 }
  

  //Menü
  tft.fillRect(215, 150, 85, 40, GRAY);
  tft.setCursor(223, 160);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.println("Menu");
  // screen_f1();
}

//Seite 02 F2
void screen_f2() {
  screen_id = 2;
  tft.fillScreen(BLACK);
  //  tft.fillScreen(screen_f2);
  tft.setCursor(20, 50);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);

  if(sprache == 1){ //englisch
  tft.println(" box fold");
  tft.println("  ready");
 }
 if(sprache == 2){ // französisch
  tft.println("boite pliee");
  tft.println("  prete");
 }
 if(sprache == 3){ // deutsch
  tft.println("Schachtel falten");
  tft.println("     bereit");
 }
 
  //Generate YES button


  if (grundstellung == 0 && karton_eingelegt == 1 && KeineGrundstellung == 0) {
    tft.fillRect(20, 150, 115, 40, GREEN);
  }
  else {
    tft.fillRect(20, 150, 115, 40, GRAY);
  }
  tft.setCursor(33, 160);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  
  if(sprache == 1){
  tft.println("launch");
 }
 if(sprache == 2){
  tft.println("commencer");
 }
 if(sprache == 3){
  tft.println("Start");
 }

  //Generate NO button
  tft.fillRect(215, 150, 85, 40, GRAY);
  tft.setCursor(223, 160);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.println("Menu");
  //screen_f2();
}

//Seite 03 F3
void screen_f3() {
  screen_id = 3;
  tft.fillScreen(BLACK);
  tft.setCursor(25, 50);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  // tft.print("Geschwindigkeit");
  tft.print(value_bar);
  tft.println("%");
  tft.fillRect(15 + value_bar * 2.9, 100, 290 - value_bar * 2.9, 30, GRAY);
  tft.fillRect(15, 100, value_bar * 2.9, 30, BLUE);
  //Generate OK button
  tft.fillRect(115, 150, 80, 40, GRAY);
  tft.setCursor(138, 160);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.println("OK");
}

//Seite 05 F5
void screen_f5() {
  screen_id = 5;
  tft.fillScreen(BLACK);
  tft.setCursor(35, 50);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);

  if(sprache == 1){ //englisch
  tft.println("  box is");
  tft.println("  folded");
 }
 if(sprache == 2){ // französisch
  tft.println("La boite");
  tft.println("est pliee");
 }
 if(sprache == 3){ // deutsch
  tft.println("Schachtel wird");
  tft.println("     gefalten");
 }
 
}

//Seite 06 F6
void screen_f6() {
  screen_id = 6;
  tft.fillScreen(BLACK);
  tft.setCursor(15, 50);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);

  if(sprache == 1){ //englisch
  tft.println("  Box ready");
  tft.println("please remove");
 }
 if(sprache == 2){ // französisch
  tft.println("     boite prete");
  tft.println("S'il vous plait retirer");
 }
 if(sprache == 3){ // deutsch
  tft.println("Schachtel fertig");
  tft.println("  bitte entnehmen"   );
 }
 

  //Generate NO button
  tft.fillRect(100, 150, 120, 40, GREEN);
  tft.setCursor(105, 160);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);

  if(sprache == 1){
  tft.println("done");
 }
 if(sprache == 2){
  tft.println("fini");
 }
 if(sprache == 3){
  tft.println("fertig");
 }


}

Gerne macht das einer. Da bin ich sicher.
Kannst Du bitte Deinen Code von den unzähligen Leerzeilen befreien und nochmal einstellen? Danke.

Ok habe ich gemacht, ich muss noch dazu sagen das der Code nicht von mir ist und ich mich nicht sooo gut damit auskenne. Der den ihn geschrieben hat kann ich leider nicht mehr fragen.

#include <Elegoo_GFX.h>    // Core graphics library
#include <Elegoo_TFTLCD.h> // Hardware-specific library
#include <TouchScreen.h>
#include <SPI.h>
#include <SD.h>

#define YP A3  // must be an analog pin, use "An" notation!
#define XM A2  // must be an analog pin, use "An" notation!
#define YM 9   // can be a digital pin
#define XP 8   // can be a digital pin

//Touch For New ILI9341 TP
#define TS_MINX 120
#define TS_MAXX 900
#define TS_MINY 70
#define TS_MAXY 920

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
//PImage logo;
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
// optional
#define LCD_RESET A4
// Assign human-readable names to some common 16-bit color values:
#define	BLACK   0x0000
#define	BLUE    0x001F
#define	RED     0xF800
#define	GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF
#define GRAY    0xDEFB

//#define SD_CS 10  //SD card pin on your shield
Elegoo_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

int Motor = 44;
int motor1 = 40;
int posWelle = 22;
int KartonEingelegt = 32;
int WelleAusGrundstellung = 26;
int KeineSchachtelEingelegt = 34;

int j = 0;
int n = 0;
//int motor = 0;
int grundstellung = 0;
int start = 0;
int GrundstellungFahren = 0;
int taktWelle = 0;
int karton_eingelegt = 0;
int WelleGedreht = 0;
int MotorGelaufen = 0;
int TasterF1 = 0;
int TasterF2 = 0;
int turn = 0;
int KeineGrundstellung = 0;
int KeineSchachtel = 0;
int Startfreigabe = 0;
int Bildschirm5 = 0;

//long crontimer = millis();
//String runtime = "00:00:00";
//int t_sec = 0;
//int t_min = 0;
//int t_hour = 0;
int fbutton_id = 0;
//int clickcounter = 0;
int screen_id = 20;
int value_bar = 30;
//boolean runstop = true;
int sprache = 3;

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

  tft.reset();

 uint16_t identifier = tft.readID();

  if (identifier == 0x9325) {
    Serial.println(F("Found ILI9325 LCD driver"));
  } else if (identifier == 0x9328) {
    Serial.println(F("Found ILI9328 LCD driver"));
  } else if (identifier == 0x4535) {
    Serial.println(F("Found LGDP4535 LCD driver"));
  } else if (identifier == 0x7575) {
    Serial.println(F("Found HX8347G LCD driver"));
  } else if (identifier == 0x9341) {
    Serial.println(F("Found ILI9341 LCD driver"));
  } else if (identifier == 0x8357) {
    Serial.println(F("Found HX8357D LCD driver"));
  } else if (identifier == 0x0101)
  {
    identifier = 0x9341;
    Serial.println(F("Found 0x9341 LCD driver"));
  } else {
    Serial.print(F("Unknown LCD driver chip: "));
    Serial.println(identifier, HEX);
    Serial.println(F("If using the Elegoo 2.8\" TFT Arduino shield, the line:"));
    Serial.println(F("  #define USE_Elegoo_SHIELD_PINOUT"));
    Serial.println(F("should appear in the library header (Elegoo_TFT.h)."));
    Serial.println(F("If using the breakout board, it should NOT be #defined!"));
    Serial.println(F("Also if using the breakout, double-check that all wiring"));
    Serial.println(F("matches the tutorial."));
    identifier = 0x9341;
  }

  tft.begin(identifier);
  tft.setRotation(1);
  screen_welcome();

  pinMode (Motor, OUTPUT);
  pinMode (posWelle, INPUT);
  pinMode (KartonEingelegt, INPUT);
  pinMode (motor1, OUTPUT);
  pinMode (WelleAusGrundstellung, INPUT);
  pinMode (KeineSchachtelEingelegt, INPUT);

 // spi_save = SPCR;

  //  logo = TFTscreen.loadImage("Logo_groß.bmp");

  //EsploraTFT.image (logo, 0, 0);
}

#define MINPRESSURE 5
#define MAXPRESSURE 1000

int x = 0;
int y = 0;

void loop()
{
if ((digitalRead(posWelle) == 1 && start == 0 && Startfreigabe == 0) || (WelleGedreht == 0 && Startfreigabe == 0 ) || (digitalRead(posWelle) == 1 && GrundstellungFahren == 0 && Startfreigabe == 0)) {
    analogWrite(Motor, LOW);
    digitalWrite (motor1, LOW);
   /* if(MotorGelaufen = 1){
      start = 0;
    }*/
    GrundstellungFahren = 0;
    //  WelleGedreht = 0;
  }

  karton_eingelegt = digitalRead(KartonEingelegt);
  grundstellung = digitalRead(posWelle);
  KeineGrundstellung = digitalRead(WelleAusGrundstellung);
  KeineSchachtel = digitalRead(KeineSchachtelEingelegt);
  
  Serial.println(karton_eingelegt);
  Serial.println(grundstellung);
  Serial.println(KeineGrundstellung);
  Serial.println(KeineSchachtel);

  if (grundstellung == 1) {
    WelleGedreht = 0;
  }
  if (grundstellung == 0) {
    WelleGedreht = 1;
  }
  TSPoint p = ts.getPoint();
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);

  if (grundstellung == 1) {
    turn = 0;
  }

  if (KeineGrundstellung == 0) {
    turn = 1;
  }
  
  //  Serial.println(taktWelle("Takt Welle"));

if(screen_id == 20){
  delay(6000);
  screen_main();
}

  if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {

    // scale from 0->1023 to tft.width
    p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
    p.y = (tft.height() - map(p.y, TS_MINY, TS_MAXY, tft.height(), 0));

    //Display received X an Y value from touch
    Serial.print("(x "); Serial.print(p.x);
    Serial.print(", y "); Serial.print(p.y);
    Serial.println(")");

if(screen_id == 30) {
  
if(p.x > 60 && p.x < 100 && p.y < 220 && p.y > 90){
  sprache = 3;
  screen_main();
}
if(p.x > 135 && p.x < 175  && p.y < 220 && p.y > 90){
  sprache = 1;
  screen_main();
}
if(p.x > 210 && p.x < 260  && p.y < 220 && p.y > 90){
  sprache = 2;
  screen_main();
}
if (p.x > 200 && p.x < 250  && p.y < 75 && p.y > 20) {
        screen_main();
        //Action on F1-Screen zurück
        }
}
    if (screen_id == 0) {//Get touch on Main-Screen

      if (p.x > 280 && p.y > 180) {
        fbutton_id = 1;
        screen_f1();                                                  //Grundstellung fahren
      }
      if (p.x > 280 && p.y < 180 && p.y > 60) {
        fbutton_id = 2;
        screen_f2();
      }
      if (p.x > 280  && p.y < 60 ) {
        fbutton_id = 3;
        screen_f3();                                                   //Motor Geschwindigkeit einstellen
      }
if(p.x < 50  && p.y < 120 ){
  screen_sprache();
}
    }
    else if (screen_id == 1) { //F1 screen action
      Serial.print(KeineGrundstellung);
      if (p.x > 200 && p.x < 250 && p.y < 220 && p.y > 130  && grundstellung == 0) {// && KeineGrundstellung == 1
        analogWrite(Motor, ((20 + 100) / 2) * 2.55);
     // if(GrundstellungFahren = 1 && digitalRead(WelleAusGrundstellung) == 1){
        digitalWrite (motor1, HIGH);
//}
        TasterF1 = 1;
        //Action on F1-Screen Grundstellung fahren
      }
//digitalWrite (motor1, HIGH);
      else {
        TasterF1 = 0;
      }
      if (p.x > 200 && p.x < 250  && p.y < 75 && p.y > 20) {
        screen_main();
        //Action on F1-Screen zurück
      }
    }
/*karton_eingelegt = digitalRead(KartonEingelegt);

  grundstellung = digitalRead(posWelle);
  KeineGrundstellung = digitalRead(WelleAusGrundstellung);
  KeineSchachtel = digitalRead(KeineSchachtelEingelegt);*/

    else if (screen_id == 2) { //F2 screen action
      //
      if (p.x > 200 && p.x < 250 && p.y < 220 && p.y > 160) {
                
        if (digitalRead(posWelle) == 0 && digitalRead(KeineSchachtelEingelegt) == 0 && MotorGelaufen == 0 && digitalRead(KartonEingelegt) == 1 && digitalRead(WelleAusGrundstellung) == 0) { //
          screen_f5();
          Bildschirm5 = 1;
          Startfreigabe = 1;
          TasterF2 = 1;
        }
        if (taktWelle == 0 && digitalRead(posWelle) == 0 && digitalRead(WelleAusGrundstellung) == 0) {
          taktWelle = 1;
        }
        //  }
        //Action on F2-Screen Start falten
      }
      if (p.x > 200 && p.x < 250  && p.y < 75 && p.y > 20) {
        screen_main();
        //Action on F2-Screen zurück
      }
    }
    else if (screen_id == 3) { //F3 screen action
      if (p.x > 200 && p.x < 250 && p.y < 155 && p.y > 95) {
        screen_main();
        //Action on F3-Screen ok
      }
      if (p.x > 130 && p.x < 170 && p.y < 225 && p.y > 13) {
        value_bar = (216 - p.y + 8) / 2.1;
        tft.fillRect(15 + value_bar * 2.9, 100, 290 - value_bar * 2.9, 30, WHITE);
        tft.fillRect(15, 100, value_bar * 2.9, 30, BLUE);
        tft.fillRect(120, 40, 120, 40 , WHITE);
        tft.setCursor(125, 50);
        tft.setTextColor(BLACK);
        tft.setTextSize(3);
        tft.print(value_bar);
        tft.println("%");
        //Action if value-bar changed
        n = value_bar;
      }
    }
    else if (screen_id == 6) {

      if (p.x > 200 && p.x < 250 && p.y < 150 && p.y > 95) {
        // if (taktWelle == 1 && grundstellung == 1) {
        screen_f2();
        // }
      }
    }
  }
  if (screen_id == 5 && TasterF2 == 1 && digitalRead(WelleAusGrundstellung) == 0 && MotorGelaufen == 1 && digitalRead(posWelle) == 1)  {//
    //  delay (3600);
  if (start == 1) {
    analogWrite(Motor, ((value_bar + 100) / 2) * 2.55);
    digitalWrite (motor1, HIGH);
    //}//  WelleGedreht = 1;
    if (digitalRead(posWelle) == 0 && MotorGelaufen == 1) {
      taktWelle = 0;
    }
    if (digitalRead(posWelle) == 1) {
      MotorGelaufen = 1;
    }
    delay(200);
  }
    if (digitalRead(WelleAusGrundstellung) == 0 || digitalRead(posWelle) == 0) { //&& digitalRead(WelleAusGrundstellung) == 0
      screen_f6();
      MotorGelaufen = 0;
      TasterF2 = 0;
      start = 0;
      taktWelle = 0;
      turn = 0;
      Startfreigabe = 0;
    }
  }
  if (digitalRead(posWelle) == 0 || TasterF1 == 0) {
    GrundstellungFahren = 0;
  }
  if (TasterF1 == 1 && digitalRead(posWelle) == 1) {
    GrundstellungFahren = 1;
    TasterF1 = 0;
  }
  if (TasterF2 == 1 && taktWelle == 1 && Startfreigabe == 1) {  //|| (TasterF2 && KeineGrundstellung == 0 && grundstellung == 1 && karton_eingelegt == 1 && KeineSchachtel == 0)) { //&& karton_eingelegt == 1 && taktWelle == 1 && KeineGrundstellung == 1) || (TasterF2 == 1 && karton_eingelegt == 1 && MotorGelaufen == 0 && KeineGrundstellung == 1) {
    start = 1;
    //TasterF2 = 0;
  }
/*karton_eingelegt = digitalRead(KartonEingelegt);

  grundstellung = digitalRead(posWelle);
  KeineGrundstellung = digitalRead(WelleAusGrundstellung);
  KeineSchachtel = digitalRead(KeineSchachtelEingelegt);*/
  
  // Motor ansteuerung
if ((digitalRead(posWelle) == 1 && start == 0 && Startfreigabe == 0) || (WelleGedreht == 0 && Startfreigabe == 0 ) || (digitalRead(posWelle) == 1 && GrundstellungFahren == 0 && Startfreigabe == 0)) {
    analogWrite(Motor, 0);
    digitalWrite (motor1, 0);
   // if(MotorGelaufen = 1){
   //   start = 0;
   // }
    GrundstellungFahren = 0;
    //  WelleGedreht = 0;
  }
  else if (start == 1) {
    digitalWrite (motor1, HIGH);
    //}//  WelleGedreht = 1;
    if (digitalRead(posWelle) == 1 && MotorGelaufen == 1) {
      taktWelle = 0;
    }
    if (digitalRead(WelleAusGrundstellung) == 1) {
      MotorGelaufen = 1;
    }
    delay(500);
  }
}
//Display Seiten
// Willkommen
void screen_welcome() {
  screen_id = 20;
 // bmpDraw("Sotec-Logo-groß.bmp", 0, 0);
  tft.fillScreen(BLACK);
  //  tft.fillScreen(screen_f1);
  tft.setCursor(10, 50);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.println(" Willkommen");
  tft.println(" ");
  tft.println("     Welcome"   );
   tft.println(" ");
  tft.println("        Bienvenue"   );
}
//Build MAIN screen
void screen_main() {
  screen_id = 0;
  tft.fillScreen(BLACK);

  //Show F3 box value
  tft.setCursor(195, 154);
  tft.setTextColor(BLUE);
  tft.setTextSize(1);
  tft.println("Gechwindigkeit:");
  tft.setCursor(200, 170);
  tft.setTextColor(GRAY);
  tft.setTextSize(3);
  tft.print(value_bar);
  tft.println(" %");

  //Build F1 button
  tft.fillRect(0, 200, 80, 40, GRAY);
  tft.setCursor(15, 210);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.println("Pos");

  //Build F2 button
 
  if(sprache == 1){
  
  tft.fillRect(80, 200, 140, 40, WHITE);
  tft.setCursor(108, 210);
  tft.setTextColor(GREEN);
  tft.setTextSize(3);
  tft.println("launch");
 }
 if(sprache == 2){
  
  tft.fillRect(80, 200, 140, 40, WHITE);
  tft.setCursor(82, 210);
  tft.setTextColor(GREEN);
  tft.setTextSize(3);
  tft.println("commencer");
 }
 if(sprache == 3){
  
  tft.fillRect(80, 200, 140, 40, WHITE);
  tft.setCursor(120, 210);
  tft.setTextColor(GREEN);
  tft.setTextSize(3);
  tft.println("Start");
 }
 
  //Build F3 button
  tft.fillRect(240, 200, 80, 40, GRAY);
  tft.setCursor(265, 210);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.println("SP");

  //Build Language button
  tft.fillRect(160, 0, 160, 40, GRAY);
  tft.setCursor(170, 10);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
 if(sprache == 1){
  tft.println("language");
 }
 if(sprache == 2){
  tft.println("Langue");
 }
 if(sprache == 3){
  tft.println("Sprache");
 }
}

void screen_sprache() {
  screen_id = 30;
  tft.fillScreen(BLACK);
  //  tft.fillScreen(screen_f1);
  //Generate YES button
  tft.fillRect(20, 50, 170, 40, GRAY);
  tft.setCursor(25, 60);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.println("Deutsch");  
  tft.fillRect(20, 100, 170, 40, GRAY);  
  tft.setCursor(25, 110);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.println("English");
  tft.fillRect(20, 150, 170, 40, GRAY);
  tft.setCursor(25, 160);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.println("France");

  //Generate NO button
  tft.fillRect(215, 150, 85, 40, GRAY);
  tft.setCursor(223, 160);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.println("Menu");
  // screen_f1();
}
//Seite 01 F1
void screen_f1() {
  screen_id = 1;
  tft.fillScreen(BLACK);
  //  tft.fillScreen(screen_f1);
  tft.setCursor(20, 50);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);

if(sprache == 1){ //englisch
  tft.println("  Drive in");
  tft.println("basic position");
 }
 if(sprache == 2){ // französisch
  tft.println("  Conduire en");
  tft.println(" position de base");
 }
 if(sprache == 3){ // deutsch
  tft.println("In Grundstellung");
  tft.println("     fahren");
 }
  //Generate YES button
  if (KeineGrundstellung == 0 || grundstellung == 1) {
    tft.fillRect(20, 150, 130, 40, GRAY);
  }
  else {
    tft.fillRect(20, 150, 130, 40, BLUE);
  }
  tft.setCursor(33, 160);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);

if(sprache == 1){
  tft.println("drive");
 }
 if(sprache == 2){
  tft.println("conduire");
 }
 if(sprache == 3){
  tft.println("fahren");
 }
 
  //Menü
  tft.fillRect(215, 150, 85, 40, GRAY);
  tft.setCursor(223, 160);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.println("Menu");
  // screen_f1();
}

//Seite 02 F2
void screen_f2() {
  screen_id = 2;
  tft.fillScreen(BLACK);
  //  tft.fillScreen(screen_f2);
  tft.setCursor(20, 50);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);

  if(sprache == 1){ //englisch
  tft.println(" box fold");
  tft.println("  ready");
 }
 if(sprache == 2){ // französisch
  tft.println("boite pliee");
  tft.println("  prete");
 }
 if(sprache == 3){ // deutsch
  tft.println("Schachtel falten");
  tft.println("     bereit");
 }
  //Generate YES button
  if (grundstellung == 0 && karton_eingelegt == 1 && KeineGrundstellung == 0) {
    tft.fillRect(20, 150, 115, 40, GREEN);
  }
  else {
    tft.fillRect(20, 150, 115, 40, GRAY);
  }
  tft.setCursor(33, 160);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  
  if(sprache == 1){
  tft.println("launch");
 }
 if(sprache == 2){
  tft.println("commencer");
 }
 if(sprache == 3){
  tft.println("Start");
 }
  //Generate NO button
  tft.fillRect(215, 150, 85, 40, GRAY);
  tft.setCursor(223, 160);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.println("Menu");
  //screen_f2();
}

//Seite 03 F3
void screen_f3() {
  screen_id = 3;
  tft.fillScreen(BLACK);
  tft.setCursor(25, 50);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  // tft.print("Geschwindigkeit");
  tft.print(value_bar);
  tft.println("%");
  tft.fillRect(15 + value_bar * 2.9, 100, 290 - value_bar * 2.9, 30, GRAY);
  tft.fillRect(15, 100, value_bar * 2.9, 30, BLUE);
  //Generate OK button
  tft.fillRect(115, 150, 80, 40, GRAY);
  tft.setCursor(138, 160);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.println("OK");
}

//Seite 05 F5
void screen_f5() {
  screen_id = 5;
  tft.fillScreen(BLACK);
  tft.setCursor(35, 50);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);

  if(sprache == 1){ //englisch
  tft.println("  box is");
  tft.println("  folded");
 }
 if(sprache == 2){ // französisch
  tft.println("La boite");
  tft.println("est pliee");
 }
 if(sprache == 3){ // deutsch
  tft.println("Schachtel wird");
  tft.println("     gefalten");
 } 
}

//Seite 06 F6
void screen_f6() {
  screen_id = 6;
  tft.fillScreen(BLACK);
  tft.setCursor(15, 50);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);

  if(sprache == 1){ //englisch
  tft.println("  Box ready");
  tft.println("please remove");
 }
 if(sprache == 2){ // französisch
  tft.println("     boite prete");
  tft.println("S'il vous plait retirer");
 }
 if(sprache == 3){ // deutsch
  tft.println("Schachtel fertig");
  tft.println("  bitte entnehmen"   );
 }
 
  //Generate NO button
  tft.fillRect(100, 150, 120, 40, GREEN);
  tft.setCursor(105, 160);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);

  if(sprache == 1){
  tft.println("done");
 }
 if(sprache == 2){
  tft.println("fini");
 }
 if(sprache == 3){
  tft.println("fertig");
 }
}

Könntest du mir evtl. Helfen?

Ich seh Ducrch den Code nicht ganz durch, da es weder Kommentare noch irgendwas beschreibendes gibt.
Aber kann es sein, das Du hier hängst:

Wenn Du Taster F1 erkennst, stellst GrundstellFahren = 1 und TasterF1=0.
Im nächsten Umlauf ist der TasterF1 0 und damit Grundstellungfahren auch 0.

Die dann folgende if-Konstruktion erschliesst sich mir nicht, aber ich vermute, das Du da was machen willst und gleichzeitig die Variablen, auf die Du prüfst, zurücksetzt bevor ein Ergebnis da ist.

Tipp:
Breche das runter, das Du den TFT nicht brauchst und das mit manuellen Tastern machen kannst.
Mache daraus eine Schrittkette.
Lagere Funktionen aus.
Und dann:
Was sich nicht ändern kann, mach Konstanten draus.
Pin Nummern als byte ist ausreichend.
(Pin)Zustände mach bool draus
Und kommentiere den Code....

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.