Go Down

Topic: 2.8" TFT Touch Shield [V1] - Printing Data (Read 2 times) previous topic - next topic

milesmiles902


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

milesmiles902

Code: [Select]


#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);
 }
}

GunnerCAF

#7
Jul 24, 2013, 07:43 am Last Edit: Jul 24, 2013, 08:05 am by GunnerCAF Reason: 1
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:

Code: [Select]

/* 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

milesmiles902


Go Up