2.8" TFT Touch Shield [V1] - Printing Data

Howdy,

I posted this on the Seeeduino forums...but have gotten literally no response. I thought it would be best to post on the makers forum, but nobody is ever on there. The issue lies with Seeeduino's products are much different than arduino's, adafruit's or even sparkfun's. They require different libraries and what makes it worse is this is version 1, not version 2. I found tutorials on how to do it for v2, but I don't know how to translate the code over because of the reference to different libraries.

Here what I posted on their forum:

Hello,

I have Version 1 of the Seeeduino 2.8" TFT Touch Screen from Radioshack. Currently, I am making a reef controller for my aquarium. I want the TFT to print data (hopefully live data) on the screen, such as temperature and pH. I am able to gather data from the probes and print them in the serial monitor. Although, I have no clue on how to transmit recorded data to the TFT.

Would someone be willing to help me out on a tutorial for V1 or help me out with the code.

I will need to use both analog and digital pins.

Thank you.

-Miles

This is the tutorial for version 2, if that helps any:

http://www.seeedstudio.com/forum/viewtopic.php?f=23&t=4627&hilit=tft

Here is the wiki for v1 of the TFT, with libraries at the bottom of the page:

http://www.seeedstudio.com/wiki/2.8''_TFT_Touch_Shield_V1.0

I appreciate anyone's help.

-Miles

What's wrong exactly? Code doesn't compile? Errors? What are those errors?

Do you need help with writing to the screen or getting back touches?

I have written a sketch for this TFT I bought at Radio Shack. Use the information from here:

http://www.seeedstudio.com/wiki/2.8''_TFT_Touch_Shield_V1.0

There is an example sketch at the bottom of the page. Start your sketch out by using the correct libraries for your TFT like in the example. I would start with the example, then start making modifications to it. The library includes several examples, there is one called "text", this may be a good one to start with. If you just want to print data, use the Tft.drawString or Tft.drawCar commands. Modify the coordinates where you want your text to start printing.

One tip, draw all the text that will not change. The values that do change, you will need to clear the screen before you print the new value. To do this, I used Tft.fillRectange, and use the color of your background so the old information is erased. You only need make the rectangle big enough to erase only the numbers that change. This way you won't need to re-print the entire screen each time a number changes. This will speed up your screen refreshes.

I just did one line at a time, loaded the sketch, tested it, when it looked right, load the next one. It took some time, but it's easer then trying to troubleshoot the entire screen. Also, get some graph paper and draw the screen as you want it. Then write down all your coordinates for text, lines or boxes for reference when you write your code.

What Arduino are you using for this project? I used a Mega since this shield uses all but 4 pins of the Uno, and the shield covers all the pins of the Uno.

Good luck,
Dave

I need help writing live sensor data on the TFT, and for it to refresh by itself. I have made all the menus and they seem to work. I just do not know how to write sensor data on the screen.

The code in the tutorial references TFTv2.h, which used the v2 libraries and they don't entirely work for v1. If I attempt to change TFTv2.h to the TFT.h (version 1), I get this compiling error:

I think this is due to the fact TFTv2.cpp declares TFT, while TFT.cpp (version 1) does not.

GunnerCAF:
One tip, draw all the text that will not change. The values that do change, you will need to clear the screen before you print the new value. To do this, I used Tft.fillRectange, and use the color of your background so the old information is erased. You only need make the rectangle big enough to erase only the numbers that change. This way you won't need to re-print the entire screen each time a number changes. This will speed up your screen refreshes.

I like this idea. I will rewrite the code to try to achieve this.

GunnerCAF:
What Arduino are you using for this project? I used a Mega since this shield uses all but 4 pins of the Uno, and the shield covers all the pins of the Uno.

I am using the Mega2650. I have a Uno and the TFT took up all but 2 digital pins, which is not enough with two temp probes and a pH probe.

Sorry if I did not make it concise enough. If you need more info, let me know.

Sincerely,
Miles

P.S. Here are the libraries:

V1
http://www.seeedstudio.com/wiki/File:TFT_Touch_Shield_libraries.zip

V2:
http://www.seeedstudio.com/wiki/File:Seeed_TFT_v2.0.zip

The code I compiled is from this tutorial:
http://www.seeedstudio.com/forum/viewtopic.php?f=23&t=4627&hilit=tft

First, you need to only use the Version 1 libraries for the TFT Shield you have. From what I read, the Version 2 shield has completely different hardware, so V2 libraries will not work.

Use these 3 libraries, plus any library you need to read your sensors:

#include <stdint.h>
#include <TouchScreen.h> 
#include <TFT.h>

For the Mega, you need to define these:

#define YP A2   // must be an analog pin, use "An" notation!
#define XM A1   // must be an analog pin, use "An" notation!
#define YM 54   // can be a digital pin, this is A0
#define XP 57   // can be a digital pin, this is A3

The background light is not controllable on the V1 shield, so don't use the background light statement.

When you start the TFT library, you used an extra "TFT" in the statement, this may be your "TFT not declared" error. It should look like this:

Tft.init();  //init TFT library

I suggest you start with an example code, load it and make sure it works. Now you know you have all the right libraries and setup code needed. Next, modify the example code, then test it. If it works, save it then do another modification. Do only small modifications and repeat the modify/testing process. If you do this, you can easily find these errors. If not, you can always go back to your last good saved sketch to try again.

Good luck,
Dave

GunnerCAF:
I suggest you start with an example code, load it and make sure it works. Now you know you have all the right libraries and setup code needed. Next, modify the example code, then test it. If it works, save it then do another modification. Do only small modifications and repeat the modify/testing process. If you do this, you can easily find these errors. If not, you can always go back to your last good saved sketch to try again.

This is what I did. I opened the TFTpaint example and reworked it for my menu design. The code I am posting is mainly what I have come up with, except with the new addition of the SensorPrintout.

The parts I would like you to look at is the areas I have "************". They show my additions to the code to try to achieve a live sensor reading.

The main menu tabs that are on all screens are Home, Temperature, pH and Relays. There are other things such as Pump1, Pump2, Actinics, Metal Halides etc. that are just buttons for now, and will be further changed to turn relays on and off. Just one step at a time. Plus, I am waiting for the Relays in the mail.

Any pointers on the code would be great. In reality I am not a programmer, just modify code to my benefit.

Reconditely,
Miles

#include <TouchScreenMenu.h>
#include <TouchScreen.h>
#include <TFT.h>
#include <cstddef.h>
//*******************************The script was working fine without the defines. Do I need them, I think in the TFT.h it was changed for a Mega.***********************************
#define YP A2   // must be an analog pin, use "An" notation!
#define XM A1   // must be an analog pin, use "An" notation!
#define YM 54   // can be a digital pin, this is A0
#define XP 57   // can be a digital pin, this is A3 

int tempPin = 15;//Added for temperature probe
char sensorPrintout[4];//********************Think it is needed for the string value********************

// create the array of items for the first Home menu
TouchScreenMenuItem HomeMenuItems[] = {
  TouchScreenMenuItem("Home"),
  TouchScreenMenuItem("Temperature"),
  TouchScreenMenuItem("pH"),
  TouchScreenMenuItem(sensorPrintout),//***************Tried just adding it to the Touch screen to see if it printed out, but got an empty box.*****************
  TouchScreenMenuItem("Relays"),
  TouchScreenMenuItem("Pump 1"),
  TouchScreenMenuItem("Pump 2"),
  TouchScreenMenuItem("ENDOFMENU")
};

// create the array of items for the sub menu
TouchScreenMenuItem RelaysSubMenuItems[] = {
  TouchScreenMenuItem("Home"),
  TouchScreenMenuItem("Temperature"),
  TouchScreenMenuItem("pH"),
  TouchScreenMenuItem("Relays"),
  TouchScreenMenuItem("Pump 1"),
  TouchScreenMenuItem("Pump 2"),
  TouchScreenMenuItem("Skimmer"),
  TouchScreenMenuItem("Return Pump"),
  TouchScreenMenuItem("Halides"),
  TouchScreenMenuItem("Actinics"),
  TouchScreenMenuItem("ENDOFMENU")
};

// create the array of items for the sub menu
TouchScreenMenuItem pHSubMenuItems[] = {
  TouchScreenMenuItem("Home"),
  TouchScreenMenuItem("Temperature"),
  TouchScreenMenuItem("pH"),
  TouchScreenMenuItem("Relays"),
  TouchScreenMenuItem("High"),
  TouchScreenMenuItem("Low"),
  TouchScreenMenuItem("Current"),
  TouchScreenMenuItem("ENDOFMENU")
};

// create the array of items for the sub menu
TouchScreenMenuItem TemperatureSubMenuItems[] = {
  TouchScreenMenuItem("Home"),
  TouchScreenMenuItem("Temperature"),
  TouchScreenMenuItem("pH"),
  TouchScreenMenuItem("Relays"),
  TouchScreenMenuItem("High"),
  TouchScreenMenuItem("Low"),
  TouchScreenMenuItem("Current"),
  TouchScreenMenuItem("ENDOFMENU")
};


// create the various menus setting the items, font size, spacing, padding, justification and titles
TouchScreenMenu HomeMenu = TouchScreenMenu(HomeMenuItems, 2, 5, 3, LEFTJ, "Home");
TouchScreenMenu TemperatureSubMenu = TouchScreenMenu(TemperatureSubMenuItems, 2, 5, 3, LEFTJ, "Temperature");
TouchScreenMenu pHSubMenu= TouchScreenMenu(pHSubMenuItems, 2, 5, 3, LEFTJ, "pH");
TouchScreenMenu RelaysSubMenu = TouchScreenMenu(RelaysSubMenuItems, 2, 5, 3, LEFTJ, "Relays");


// keep track of which menu is the currently active one
TouchScreenMenu *curMenu = &HomeMenu;



void setup(void) {
  TSC.setBackColor(TSC.createColor(255, 255, 24)); // change the default background color
  TSC.init(); // make sure everything get initialized
  curMenu->draw(); // put up the main menu
  
  Tft.init();//**************Thought this was needed? Might not be.*************************************************************
}

void loop(void) {
  // handle the current menu
  if(curMenu!=NULL){
    // process the current menu
    TouchScreenMenuItem *item = curMenu->process(false);
    // check to see which, if any, menu item was pressed
    checkMenuSelection(item);
  }else{
    // if there isn't a current menu being displayed check all of the buttons
    // to see if any of them was pressed
   
  } 
//*****************************************Temperature Section. Standard TMP35 code.**************************************
 int reading = analogRead(tempPin);  
 
 // converting that reading to voltage, for 3.3v arduino use 3.3
 float voltage = reading * 5.0;
 voltage /= 1024.0; 
 
 // print out the voltage
 Serial.print(voltage); Serial.println(" volts");
 
 // now print out the temperature
 float temperatureC = (voltage - 0.5) * 100 ;  //converting from 10 mv per degree wit 500 mV offset
                                               //to degrees ((volatge - 500mV) times 100)
 Serial.print(temperatureC); Serial.println(" degrees C");
 
 // now convert to Fahrenheight
 float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
 Serial.print(temperatureF); Serial.println(" degrees F");
 //***********************************************Attempt at Live Sensor Data**********************************************
 String TemperatureF = String(analogRead(A15));
 
 TemperatureF.toCharArray(sensorPrintout, 4);
 
}



// check to see if any menu item was pressed and do something
void checkMenuSelection(TouchScreenMenuItem *item) {
  boolean handled = false;
  if(item != NULL){
    
    if(curMenu == &HomeMenu){
      if(!strcmp(item->getText(),"Home")){
        curMenu = &HomeMenu;
        TSC.clearScreen();
        curMenu->draw();
        handled = true;
      }
       else if(!strcmp(item->getText(),"Home")){
        curMenu = &HomeMenu;
        TSC.clearScreen();
        curMenu->draw();
       
     }
      else if(!strcmp(item->getText(),"Temperature")){
        curMenu = &TemperatureSubMenu;
        TSC.clearScreen();
        curMenu->draw();
   
      }
      else if(!strcmp(item->getText(),"pH")){
        curMenu = &pHSubMenu;
        TSC.clearScreen();
        curMenu->draw();
      
      }
      else if(!strcmp(item->getText(),"Relays")){
        curMenu = &RelaysSubMenu;
        TSC.clearScreen();
        curMenu->draw();;
      
      }
    }
    
      else if(curMenu == &TemperatureSubMenu){
      if(!strcmp(item->getText(),"Temperature")){
        curMenu = &TemperatureSubMenu;
        TSC.clearScreen();
        curMenu->draw();
        handled = true;
      }
       else if(!strcmp(item->getText(),"Home")){
        curMenu = &HomeMenu;
        TSC.clearScreen();
        curMenu->draw();
      
     }
      else if(!strcmp(item->getText(),"Temperature")){
        curMenu = &TemperatureSubMenu;
        TSC.clearScreen();
        curMenu->draw();
        
      }
      else if(!strcmp(item->getText(),"pH")){
        curMenu = &pHSubMenu;
        TSC.clearScreen();
        curMenu->draw();
        
      }
      else if(!strcmp(item->getText(),"Relays")){
        curMenu = &RelaysSubMenu;
        TSC.clearScreen();
        curMenu->draw();;
      
      }
    }
    
    else if(curMenu == &pHSubMenu){
      if(!strcmp(item->getText(),"pH")){
        curMenu = &pHSubMenu;
        TSC.clearScreen();
        curMenu->draw();
        handled = true;
      }  
      else if(!strcmp(item->getText(),"Home")){
        curMenu = &HomeMenu;
        TSC.clearScreen();
        curMenu->draw();
       
     }
      else if(!strcmp(item->getText(),"Temperature")){
        curMenu = &TemperatureSubMenu;
        TSC.clearScreen();
        curMenu->draw();
       
      }
      else if(!strcmp(item->getText(),"pH")){
        curMenu = &pHSubMenu;
        TSC.clearScreen();
        curMenu->draw();
        
      }
      else if(!strcmp(item->getText(),"Relays")){
        curMenu = &RelaysSubMenu;
        TSC.clearScreen();
        curMenu->draw();;
       
      }
    }
      
      else if(curMenu == &RelaysSubMenu){
      if(!strcmp(item->getText(),"Relays")){
        curMenu = &RelaysSubMenu;
        TSC.clearScreen();
        curMenu->draw();
        handled = true;
      }
       else if(!strcmp(item->getText(),"Home")){
        curMenu = &HomeMenu;
        TSC.clearScreen();
        curMenu->draw();
 
     }
      else if(!strcmp(item->getText(),"Temperature")){
        curMenu = &TemperatureSubMenu;
        TSC.clearScreen();
        curMenu->draw();
    
      }
      else if(!strcmp(item->getText(),"pH")){
        curMenu = &pHSubMenu;
        TSC.clearScreen();
        curMenu->draw();
       
      }
      else if(!strcmp(item->getText(),"Relays")){
        curMenu = &RelaysSubMenu;
        TSC.clearScreen();
        curMenu->draw();;
        
      }
    }
    // if the menu item didn't get handled redraw it unpressed
    if(handled==false)
        curMenu->drawItem(item,false);
  }
}

Miles,

I gave up on the TFT Menu library when I was working on my project, there isn't very good documentation except for the example and I only needed a single screen. Maybe I can answer a few questions from the code you posted:

The # defines are needed for the touch screen, the statements you have are for the MEGA.

I'm not sure about the string value.

You forgot the quotations on "sensorPrintout".

You will need the Tft.init statement.

I did a little experimenting with my TFT tonight. My current project has two joysticks attached to my Mega, so I just used these for test sensor values. If you just want to print out sensor values, here is an example sketch that displays time and pot values:

/* Modified from Draw Texts - Demonstrate drawChar and drawString
/  Example to display sensor data on TFT screen
/  
/  For Arduino MEGA with Seeed V1 TFT Touch Shield
/  Circuit: Joystick, or Pot center pin connected to A8, A9, and A10.
/  pot outer pins connected to GND and 5V
/  
*/

#include <stdint.h>
#include <TouchScreen.h> 
#include <TFT.h>

#define YP A2   // must be an analog pin, use "An" notation!
#define XM A1   // must be an analog pin, use "An" notation!
#define YM 54   // can be a digital pin, this is A0
#define XP 57   // can be a digital pin, this is A3 


int sensor1Data;            // Data varable for JS1 pot 1
int sensor2Data;            // Data varable for JS1 pot 2
int sensor3Data;            // Data varable for JS2 pot 3
char sensor1Char[4];        // Charactor array for pot 1
char sensor2Char[4];        // Charactor array for pot 2
char sensor3Char[4];        // Charactor array for pot 3
long timerData;             // Data varable for timer
char timerChar[4];          // Charactor array for timer
void setup()
{

  Tft.init();  //init TFT library
  pinMode(A8, INPUT);         // JS1 input (pot 1 u/d)
  pinMode(A9, INPUT);         // JS1 input (pot 2 r/l)
  pinMode(A10, INPUT);        // JS2 input (pot 3 u/d)
  
  drawScreen();              // Draw info that does not change once
}

void loop()
{
  getSensor();                                  // Get Sensor Data 
  
      // Print Char to TFT //
  Tft.fillRectangle(140,40,50,15,BLACK);        // Clear old text
  Tft.drawString(timerChar,140,40,2,CYAN);      // Print time
  Tft.fillRectangle(140,70,60,15,BLACK);        // Clear old text
  Tft.drawString(sensor1Char,140,70,2,WHITE);   // Print Sensor 1
  Tft.fillRectangle(140,100,60,15,BLACK);       // Clear old text
  Tft.drawString(sensor2Char,140,100,2,WHITE);  // Print Sensor 2
  Tft.fillRectangle(140,130,60,15,BLACK);       // Clear old text
  Tft.drawString(sensor3Char,140,130,2,WHITE);  // Print Sensor 3
  
  delay(1000);                                  // delay to read screen

}

// Draw Screen Function //
// Draw all the text and objects that will not change //
void drawScreen(){                
  Tft.drawRectangle(0, 3, 238,30,BLUE);            // HEADER TITLE
  Tft.drawString("DISPLAY DATA",20,15,2,WHITE);    // header name
  Tft.drawString("Seconds:",2,40,2,CYAN);          
  Tft.drawString("Elevator:",2,70,2,WHITE);
  Tft.drawString("Rudder:",2,100,2,WHITE);
  Tft.drawString("Throttle:",2,130,2,WHITE);
}
  
// Get Sensor Function //
// Gets sensor data and converts to character //
void getSensor(){  
  timerData = millis()/1000;            // Read time
  int val= analogRead(A8);              // Read JS1 (pot 1)
  sensor1Data= map(val,0,1023,0,100);   // Map value from 0-100
  val= analogRead(A9);                  // Read JS1 (pot 2)
  sensor2Data= map(val,0,1023,0,100);   // Map value from 0-100
  val= analogRead(A10);                 // Read JS2 (pot 3)
  sensor3Data= map(val,0,1023,0,100);   // Map value from 0-100
      // Convert Data to Char //
  itoa (timerData, timerChar, 10);
  itoa(sensor1Data, sensor1Char, 10);  // convert sensor data to char
  itoa(sensor2Data, sensor2Char, 10);  // convert sensor data to char
  itoa(sensor3Data, sensor3Char, 10);  // convert sensor data to char
}

Tips: If your sensor data changes slowly, increase the delay to reduce flicker.
When clearing old text, clear just the area you need to reduce the slow screen refresh rate.

Dave

Thank you. Your code helped immensely.