Hello,
I have a Mega2560 r3 an adafruit 2.8" TFT an LM35 temp sensor and an Ph sensor.
my code looks as follows.
/* Evan S Gray
*
*/
//
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
// For the Adafruit shield, these are the default.
#define TFT_DC 9
#define TFT_CS 10
// Color definitions
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
// If using the breakout, change pins as desired
//Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
int value=0; //initializing variables
float volts=0.0;
float temp=0.0;
float tempF=0.0;
int LM35 = A9;
void setup() {
// Begin Serial Comm
Serial.begin(9600);
Serial.println("ILI9341 Test!");
//Begin TFT
tft.begin();
// Read diagnostics (optional but can help debug problems)
uint8_t x = tft.readcommand8(ILI9341_RDMODE);
Serial.print("Display Power Mode: 0x"); Serial.println(x, HEX);
x = tft.readcommand8(ILI9341_RDMADCTL);
Serial.print("MADCTL Mode: 0x"); Serial.println(x, HEX);
x = tft.readcommand8(ILI9341_RDPIXFMT);
Serial.print("Pixel Format: 0x"); Serial.println(x, HEX);
x = tft.readcommand8(ILI9341_RDIMGFMT);
Serial.print("Image Format: 0x"); Serial.println(x, HEX);
x = tft.readcommand8(ILI9341_RDSELFDIAG);
Serial.print("Self Diagnostic: 0x"); Serial.println(x, HEX);
//set screen rotation
tft.setRotation(1);
//fill screen black
tft.fillScreen(BLACK);
//set text wrap
tft.setTextWrap(true);
// Display text
tft.setCursor(0, 0);
tft.setTextColor(WHITE);
tft.setTextSize(1);
tft.println("Hello World!");
tft.setTextColor(RED);
tft.setTextSize(2);
tft.println("Welcome to the first");
tft.setTextColor(GREEN);
tft.setTextSize(3);
tft.println("Version of our Watering");
tft.setTextColor(BLUE);
tft.setTextSize(4);
tft.println("System");
delay(4000);
tft.fillScreen(BLACK);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 10
float sensorValue = analogRead(A10); //get current sensorValue between 0-1000
float Voltage = sensorValue*(5.0/1024.0); //convert sensorValue to voltage 0-5v
float pH = (Voltage*3.56)-1.889; //convert voltage to pH
value=analogRead(LM35); //read from A9
volts=(value/1024.0)*5.0; //conversion to volts
temp= volts*100.0; //conversion to temp Celsius
tempF=temp*9/5+32; //conversion to temp Fahrenheit
Serial.print("temperature= ");
Serial.println(tempF); //print the temperature in fahrenheit
// print out the value for pH
Serial.println("SensorValue");
Serial.println(sensorValue);
Serial.println("Voltage");
Serial.println(Voltage);
Serial.println("pH");
Serial.println(pH);
// print out value for temp
//Serial.println("SensorValue1");
//Serial.println(sensorValue1);
//Serial.println("Voltage1");
//Serial.println(Voltage1);
//Serial.println("temp");
//Serial.println(tempF);
// print header
tft.setCursor(1,0);
tft.setTextSize(3);
tft.setTextColor(WHITE);
tft.println("GrayMatter");
tft.setCursor(25,25);
tft.println("Growers");
tft.drawRoundRect(210,10,90,45,25,WHITE);
tft.setCursor(230,15);
tft.setTextSize(2);
tft.setTextColor(WHITE);
tft.println("pH =");
tft.setCursor(230,33);
tft.println(pH); // print pH value 0-14
tft.drawRoundRect(210,65,90,45,25,WHITE);
tft.setCursor(230,70);
tft.setTextSize(2);
tft.setTextColor(WHITE);
tft.println("Temp");
tft.setCursor(230,88);
tft.println(tempF); // print temp value F
tft.drawRoundRect(210,120,90,45,25,WHITE);
tft.setCursor(230,125);
tft.setTextSize(2);
tft.setTextColor(WHITE);
tft.println("Hum =");
tft.setCursor(230,143);
tft.println("75%"); // print Humidity value %
tft.drawRoundRect(210,175,90,45,25,WHITE);
tft.setCursor(230,180);
tft.setTextSize(2);
tft.setTextColor(WHITE);
tft.println("cO2 =");
tft.setCursor(230,198);
tft.println(pH); // print cO2 value ppm
delay(10000); // delay in between reads for stability
tft.fillScreen(BLACK);
}