TFT touch Screen+ Serial communication

Hi guys,
i habe 2 Arduinos Mega (one for TFT Touch and the other one for Sensor).
The funktion Werte() is used for reding the serial Communication received from arduino1 and write
it on arduino 2. the TFT works good without the function Wert().
but when i use the function Wert() the touch doesnt work .
So what can i do ?

void loop{
if (currentPage == '0') {
Werte();
if (myTouch.dataAvailable()) {

  myTouch.read();
  x=myTouch.getX();  
  y=myTouch.getY();   
  if ((x>=110) && (x<=410) && (y>=350) && (y<=450)) {
    drawFrame(110, 350,410, 450); 
    currentPage = '1'; 
    myGLCD.clrScr(); 
    Systemeinstellung(); 
  }

void Werte(){
while (Serial1.available() > 0)
{
// wait until the serial reading encounters character 'a'
// this marks the beginning of data
inChar = Serial1.read();
while (inChar != 'a')
{
inChar = Serial1.read();
}
// when character 'a' is encountered, read the floating data till the next character
sensor1 = Serial1.parseFloat();
inChar = Serial1.read();
// run if the next character is 'b' (it will always be 'b', unless someone messes with the sender code)
if (inChar == 'b')
{
// read floating value till next character
sensor2 = Serial1.parseFloat();
}
inChar = Serial1.read();
// break out of the loop once character 'c' is encountered
if (inChar == 'c')
{
break;
}
// for more sensor values, add more IF cases and keep the 'break' in the last one
// number them alphabetically for simplicity
}

Serial.println(sensor1);
Serial.println(sensor2);

myGLCD.printNumF(sensor1,2,190, 150);

 myGLCD.printNumF(sensor2,2,360, 150);

}

First read How to get the best out of this forum and post your entire sketch so it can be copied.

Second, why are you using two Arduinos? Is it because there is significant physical distance from the sensor?

You are missing the setup() function and all the code from before setup(). Post the whole code in code tags.

When I look at what you did post, the Werte() function definition is inside of the loop() function. That is illegal.

The Code is too long thats why i used points.
without the funktion Werte() the TFT worked well.
The funktion Werte worked also good but when i use it i couldnt use the TFT touch again imean the touch function doesnt work when i put Werte() funktion.
do you have any solutions guys?
thjis is the Code :

#include <UTouch.h>


//==== Creating Objects
UTFT    myGLCD(EHOUSE70,38,39,40,41); 
UTouch  myTouch( 6, 5, 4, 3, 2);
extern uint8_t SmallFont[];
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];

float sensor1 = 0;
char inChar;
int bg[] = {
  0, 0, 255};
#define Relai 8  
int fg[] = {
  255, 255, 255};
int button1on = 0;
char currentPage, selectedUnit;
int waterhour     = 16;//
int waterminute0   = 0; 
int waterminute1   = 30;//
int waterminute2   = 46;
int watersecond   = 0;  
int waterONminute = 1;
void setup() {
// Initial setup
  myGLCD.InitLCD();
  myGLCD.clrScr();
  myTouch.InitTouch();
  myTouch.setPrecision(PREC_MEDIUM);
    drawHomeScreen();  // Draws the Home Screen
    
  currentPage = '0'; // Indicates that we are at Home Screen
  selectedUnit = '0'; // Indicates the selected unit for the first example, cms or inches
 
  pinMode(Relai, OUTPUT);
  Serial.begin(9600);
Serial1.begin(9600);  
digitalWrite(Relai, HIGH);

}
void loop() { 

// Home Screen
  if (currentPage == '0') {
  Werte();
  
    if (myTouch.dataAvailable()) {
      
      myTouch.read();
      x=myTouch.getX(); 
      y=myTouch.getY(); 
      if ((x>=110) && (x<=410) && (y>=350) && (y<=450)) {
        drawFrame(110, 350,410, 450); 
        currentPage = '1'; 
        myGLCD.clrScr(); 
        Systemeinstellung(); 
      }}}
  if (currentPage == '1') {
      if (myTouch.dataAvailable()) {
        myTouch.read();
        x=myTouch.getX();
        y=myTouch.getY();// 
        if ((x>=10) && (x<=60) &&(y>=10) && (y<=36)) {
          drawFrame(10, 10, 60, 36);
          currentPage = '0'; // Indicates we are at home screen
          myGLCD.clrScr();
          drawHomeScreen(); // Draws the home screen
        }
        
      if ((x>=200) && (x<=600) &&(y>=130) && (y<=180)) {
          drawFrame(200, 130,600, 180);  
        currentPage = '3';
        myGLCD.clrScr();
        drawWasserpumpe();}
        }}.......}
void drawHomeScreen()
 {
  
   // Title
   myGLCD.setBackColor(0,0,0); // Sets the background color of the area where the text will be printed to black
   myGLCD.setColor(255, 255, 255); // Sets color to white
   myGLCD.setFont(BigFont); // Sets font to big
   myGLCD.print("Willkomen ", CENTER, 10); // Prints the string on the screen
   myGLCD.setFont(SmallFont); // Sets the font to small
   myGLCD.setColor(255, 0, 0); // Sets color to red
   myGLCD.drawLine(0,32,319,32); // Draws the red line
   myGLCD.setColor(255, 255, 255); // Sets color to white
   myGLCD.setFont(SmallFont); // Sets the font to small
 
   myGLCD.setFont(BigFont);
   myGLCD.print("Der Multifilter ist ", LEFT, 64);
    myGLCD.print("Temperatur ", CENTER, 90);
    myGLCD.setFont(SmallFont);
    myGLCD.print("TEMP1(TANK)      TEMP2(REAKTOR1)      TEMP3(DRAUSSEN)      Temp4(MULTIFILTER)      TEMP5(REAKTOR2)", 10, 120);
    myGLCD.setFont(BigFont);
    
    
    myGLCD.printNumI(x4,540, 150);
    myGLCD.printNumI(x5,720, 150);
    myGLCD.print("FEINSTAUB ", CENTER, 190);
    myGLCD.setFont(SmallFont);
    myGLCD.print("PM1.0 ", 25, 220);
    myGLCD.print("PM2.5 ", 360, 220);
    myGLCD.print("PM10 ", 695, 220);
    myGLCD.setFont(BigFont);
 
    myGLCD.printNumI(x1,35, 240);
    myGLCD.printNumI(x2,370, 240);
    myGLCD.printNumI(x3,705, 240);
    myGLCD.print("NOX ", 350, 270);
    myGLCD.setFont(SmallFont);
     myGLCD.print("Konzentration0 ", 150, 290);
     myGLCD.print("Konzentration1 ", 500, 290);
     myGLCD.setFont(BigFont);
     myGLCD.printNumI(x1,200, 310);
     myGLCD.printNumI(x2,550, 310);
   

   

   // Button - System einstellen
   
   myGLCD.setColor(16, 167, 103); // Sets green color
   myGLCD.fillRoundRect (110, 350,410, 450); // Draws filled rounded rectangle
   myGLCD.setColor(255, 255, 255); // Sets color to white
   myGLCD.drawRoundRect (110, 350,410, 450); // 
   myGLCD.setFont(BigFont); // Sets the font to big
   myGLCD.setBackColor(16, 167, 103); // 
   myGLCD.print("System einstellen", 130, 390);  // Prints the string

   // Button - Sensoren Messwerte
  myGLCD.setColor(16, 167, 103);
  myGLCD.fillRoundRect (430, 350, 730, 450);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (430, 350, 730, 450);
  myGLCD.setFont(BigFont);
  myGLCD.setBackColor(16, 167, 103);
  myGLCD.print("Sensoren Werte", 460, 390);
  drawButtons();
 
  }
void drawFrame(int x1, int y1, int x2, int y2) {
  myGLCD.setColor(255, 0, 0);
  myGLCD.drawRoundRect (x1, y1, x2, y2);
  while (myTouch.dataAvailable())
    myTouch.read();
    myGLCD.setColor(255, 255, 255);
    myGLCD.drawRoundRect (x1, y1, x2, y2);
}

 void Systemeinstellung() {
  
  myGLCD.setColor(100, 155, 203);
  myGLCD.fillRoundRect (10, 10, 60, 36);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (10, 10, 60, 36);
  myGLCD.setFont(BigFont);
  myGLCD.setBackColor(100, 155, 203);
  myGLCD.print("<-", 18, 15);
  myGLCD.setBackColor(0, 0, 0);
  myGLCD.setFont(SmallFont);
  myGLCD.print("zurueck", 70, 18);
  myGLCD.setFont(BigFont);
  myGLCD.print("Einstellung", CENTER, 50);
// Button - Wasserpumpe
  myGLCD.setColor(16, 167, 103); // Sets green color
   myGLCD.fillRoundRect (200, 130,600, 180); // Draws filled rounded rectangle
   myGLCD.setColor(255, 255, 255); // Sets color to white
   myGLCD.drawRoundRect (200, 130,600, 180); // Draws rounded rectangle without a fill, so the overall appearance of the button looks like it has a frame
   myGLCD.setFont(BigFont); // Sets the font to big
   myGLCD.setBackColor(16, 167, 103); // Sets the background color of the area where the text will be printed to green, same as the button
   myGLCD.print("Wasserpumpe", CENTER, 150); // Prints the string
// Button - Luftpumpe
   myGLCD.setColor(16, 167, 103); // Sets green color
   myGLCD.fillRoundRect (200, 200,600, 250); // Draws filled rounded rectangle
   myGLCD.setColor(255, 255, 255); // Sets color to white
   myGLCD.drawRoundRect (200, 200,600, 250); // Draws rounded rectangle without a fill, so the overall appearance of the button looks like it has a frame
   myGLCD.setFont(BigFont); // Sets the font to big
   myGLCD.setBackColor(16, 167, 103); // Sets the background color of the area where the text will be printed to green, same as the button
   myGLCD.print("Luftpumpe", CENTER, 220); // Prints the string
// Button - Licht 
   myGLCD.setColor(16, 167, 103); // Sets green color
   myGLCD.fillRoundRect (200, 270,600, 320); // Draws filled rounded rectangle
   myGLCD.setColor(255, 255, 255); // Sets color to white
   myGLCD.drawRoundRect (200, 270,600, 320); // Draws rounded rectangle without a fill, so the overall appearance of the button looks like it has a frame
   myGLCD.setFont(BigFont); // Sets the font to big
   myGLCD.setBackColor(16, 167, 103); // Sets the background color of the area where the text will be printed to green, same as the button
   myGLCD.print("Licht", CENTER, 290); // Prints the string
// Button - Durchlauferhitzer  
   myGLCD.setColor(16, 167, 103); // Sets green color
   myGLCD.fillRoundRect (200, 340,600, 390); // Draws filled rounded rectangle
   myGLCD.setColor(255, 255, 255); // Sets color to white
   myGLCD.drawRoundRect (200, 340,600, 390); // Draws rounded rectangle without a fill, so the overall appearance of the button looks like it has a frame
   myGLCD.setFont(BigFont); // Sets the font to big
   myGLCD.setBackColor(16, 167, 103); // Sets the background color of the area where the text will be printed to green, same as the button
   myGLCD.print("Durchlauferhitzer", CENTER, 360); // Prints the string
// Button - Kühlgeräte
   myGLCD.setColor(16, 167, 103); // Sets green color
   myGLCD.fillRoundRect (200, 410,600, 460); // Draws filled rounded rectangle
   myGLCD.setColor(255, 255, 255); // Sets color to white
   myGLCD.drawRoundRect (200, 410,600, 460); // Draws rounded rectangle without a fill, so the overall appearance of the button looks like it has a frame
   myGLCD.setFont(BigFont); // Sets the font to big
   myGLCD.setBackColor(16, 167, 103); // Sets the background color of the area where the text will be printed to green, same as the button
   myGLCD.print("Kuehlgeraet", CENTER, 430); // Prints the string   
.......
void Werte(){ 
  if (Serial1.available()){
  
    // wait until the serial reading encounters character 'a'
    // this marks the beginning of data
    inChar = Serial1.read();
    while (inChar != 'a')
    {
      inChar = Serial1.read();
    }
    // when character 'a' is encountered, read the floating data till the next character
    sensor1 = Serial1.parseFloat();
    inChar = Serial1.read();
    // run if the next character is 'b' (it will always be 'b', unless someone messes with the sender code)
    if (inChar == 'b')
    {
      // read floating value till next character
      sensor2 = Serial1.parseFloat();
    }
    inChar = Serial1.read();
    // break out of the loop once character 'c' is encountered
    if (inChar == 'c')
    {
      return;
    }
    // for more sensor values, add more IF cases and keep the 'break' in the last one
    // number them alphabetically for simplicity
  }
  Serial.println(sensor1);
    Serial.println(sensor2);
   
    myGLCD.printNumF(sensor1,2,190, 150);
    myGLCD.printNumF(sensor2,2,360, 150);
        
}

no i use 2 Arduinos because i really have many Sensors

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