So, how to make "Back to menu"-button in those "Measure Celsius"- and "Measure Fahrenheit"-functions? We have McuFriend 2.8" touch screen in this project and we can´t make a function what turns back to main menu from "Measure Celsius"- or "Measure Fahrenheit"-"app"...
Here is the code.
#include <Adafruit_GFX.h> // Core graphics library
#include <MCUFRIEND_kbv.h> // Hardware-specific library
MCUFRIEND_kbv tft;
#include <TouchScreen.h>
#include <Fonts/Org_01.h>
#include <FreeDefaultFonts.h>
const int XP=7, XM=A1, YP=A2, YM=6; //240x320 ID=0x6809
const int TS_LEFT=91, TS_RT=904, TS_TOP=122, TS_BOT=922; //näytön kalibrointi
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300); //(data,data,data,data,sensitivity);
#define MINPRESSURE 200
#define MAXPRESSURE 1000
int currentpage;
#define BLACK 0x0000 // Värit
#define BLUE 0x001F
#define GREEN 0x07E0
#define RED 0xF800
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#include "RTClib.h" // Ensin määritetään komponentit
RTC_PCF8523 rtc;
#include <Adafruit_MLX90614.h> // MLX90614-sensorin määritys alkaa
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
void setup()
{
Serial.begin(9600);
while (!Serial);
if (!mlx.begin())
{
Serial.println("Yhdistys MLX sensoriin ei onnistunut. Tarkista kytkentä savun halvettya.");
while (1);
}
Serial.println("================================================"); // MLX90614-sensorin määritys loppuu
if (! rtc.begin()) // RTC-kellon määritys alkaa
{
Serial.println("RTC-kello hukassa, tarkista kytkentä!");
Serial.flush();
while (1) delay(10);
}
if (! rtc.initialized() || rtc.lostPower())
{
Serial.println("RTC ei ole asennettu, asetetaan seuraavaksi aika ja päivämäärä!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // Päivämäärän ja kellonajan määritys, jos RTC:stä on katkennut virta, tai patteri on irrotettu (muisti hävinnyt)
// Päivämäärä ja kellonaika asetetaan muodossa (2021,12,22, 14,53,32)
}
rtc.start(); // Tähän asti päästyä RTC on asentunut ja toimii oikein, joten voidaan aloittaa varsinainen ohjelma
float drift = 43; // Kalibroi sekunnit
float period_sec = (7 * 86400); // (86400 = sekunnit per päivä 1: 7 päivää = (7 * 86400) sekuntia )
float deviation_ppm = (drift / period_sec * 1000000); // Sekunnin poikkeama miljoonasosina
float drift_unit = 4.34; // OFFSET-toiminto PCF8523_TwoHours-käytössä
int offset = round(deviation_ppm / drift_unit);
Serial.print("Offset is "); Serial.println(offset); // Offsetin arvo
// RTC-kellon määritys loppuu
tft.reset();
uint16_t ID = tft.readID();
Serial.print("TFT nayton prosessori = 0x");
Serial.println(ID, HEX);
tft.begin(ID);
tft.setRotation(1); //näytön suunnan määritys, 0 = 0 astetta, 1 = 90 astetta 2 = 180 astetta, 3 = 270 astetta
currentpage = 0;
tft.fillScreen(BLACK); //määritetään minkä värinen näytön tausta on
drawHome();
}
void loop()
{
TSPoint p = ts.getPoint(); // Read touchscreen
digitalWrite(13, HIGH);
digitalWrite(13, LOW);
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
//DateTime now = rtc.now();
if (currentpage == 0)
{
if (p.z > 10 && p.z < 1000)
{
if (p.x > 303 && p.x < 425 && p.y > 180 && p.y < 750) //jos klikataan Measure celsius
{
Serial.println("Measure celsius");
currentpage = 1;
tft.fillRoundRect(30, 45, 240, 40, 8, WHITE);
delay(100);
tft.fillRoundRect(30, 45, 240, 40, 8, RED);
tft.drawRoundRect(30, 45, 240, 40, 8, WHITE); //Measure celsius
tft.setCursor(38, 57);
tft.println("Measure celsius");
delay(100);
startCelsius();
}
if (p.x > 440 && p.x < 563 && p.y > 180 && p.y < 800) //jos klikataan Measure fahrenheit
{
Serial.println("Measure fahrenheit");
currentpage = 2;
tft.fillRoundRect(30, 95, 240, 40, 8, WHITE);
delay(100);
tft.fillRoundRect(30, 95, 240, 40, 8, RED);
tft.drawRoundRect(30, 95, 240, 40, 8, WHITE); //Measure fahrenheit
tft.setCursor(38, 107);
tft.print("Measure fahrenheit");
delay(100);
startFahrenheit();
}
if (p.x > 603 && p.x < 703 && p.y > 180 && p.y < 750) //jos klikataan Calendar
{
Serial.println("Calendar");
currentpage = 3;
tft.fillRoundRect(30, 145, 240, 40, 8, WHITE);
delay(100);
tft.fillRoundRect(30, 145, 240, 40, 8, RED);
tft.drawRoundRect(30, 145, 240, 40, 8, WHITE); //Calendar
tft.setCursor(38, 157);
tft.print("Calendar");
delay(100);
startCalendar();
}
}
}
if (currentpage == 1) //celsius
{
if (p.z > MINPRESSURE && p.z < MAXPRESSURE)
if (p.x > 743 && p.x < 850 && p.y> 105 && p.y < 200) //Takaisin nuolen klikkauskohta
{
backMenu();
drawHome();
}
}
if (currentpage == 2) //fahrenheit
{
if (p.z > MINPRESSURE && p.z < MAXPRESSURE)
if (p.x > 743 && p.x < 850 && p.y> 105 && p.y < 200) //Takaisin nuolen klikkauskohta
{
backMenu();
drawHome();
}
}
if (currentpage == 3) //calendar
if (p.z > MINPRESSURE && p.z < MAXPRESSURE)
if (p.x > 743 && p.x < 850 && p.y> 105 && p.y < 200) //Takaisin nuolen klikkauskohta
{
backMenu();
drawHome();
}
}
void drawHome()
{
tft.fillScreen(BLACK);
tft.drawRoundRect(3, 0, 315, 240, 8, WHITE); //Page border
tft.fillRoundRect(30, 45, 240, 40, 8, RED);
tft.drawRoundRect(30, 45, 240, 40, 8, WHITE); //Measure celsius
tft.fillRoundRect(30, 95, 240, 40, 8, RED);
tft.drawRoundRect(30, 95, 240, 40, 8, WHITE); //Measure fahrenheit
tft.fillRoundRect(30, 145, 240, 40, 8, RED);
tft.drawRoundRect(30, 145, 240, 40, 8, WHITE); //Calendar
tft.setCursor(32, 20);
tft.setTextSize(2);
tft.setTextColor(WHITE);
tft.print("MENU");
tft.setCursor(200, 20);
tft.setTextSize(1);
tft.setTextColor(WHITE);
tft.print("Pvm");
tft.setCursor(200, 30);
tft.setTextSize(1);
tft.setTextColor(WHITE);
tft.print("Klo");
tft.setTextColor(BLACK);
tft.setCursor(38, 57);
tft.setTextSize(2);
tft.print("Measure celsius");
tft.setTextColor(BLACK);
tft.setCursor(38, 107);
tft.setTextSize(2);
tft.print("Measure fahrenheit");
tft.setTextColor(BLACK);
tft.setCursor(38, 157);
tft.setTextSize(2);
tft.print("Calendar");
}
void startCelsius()
{
tft.fillScreen(BLACK);
tft.setCursor(32, 20);
tft.setTextSize(2);
tft.setTextColor(WHITE);
tft.print("Temperature in C");
tft.fillRect(30, 60, 240, 70, RED);
tft.drawRect(30, 60, 240, 70, WHITE);
tft.setCursor(38, 72);
tft.setTextColor(BLACK);
tft.print("Environment:");
tft.setCursor(185, 72);
tft.setTextColor(BLACK, RED);
tft.print(mlx.readAmbientTempC());
tft.setCursor(250, 72);
tft.setTextColor(BLACK);
tft.print("C");
tft.setCursor(38, 92);
tft.setTextColor(BLACK);
tft.print("Target:");
tft.setCursor(125, 92);
tft.setTextColor(BLACK, RED);
tft.print(mlx.readObjectTempC());
tft.setCursor(190, 92);
tft.setTextColor(BLACK);
tft.print("C");
backMenuIcon();
while(mlx.readAmbientTempC(), mlx.readObjectTempC())
{
tft.setCursor(185, 72);
tft.setTextColor(BLACK, RED);
tft.print(mlx.readAmbientTempC());
tft.setCursor(125, 92);
tft.setTextColor(BLACK, RED);
tft.setCursor(125, 92);
tft.setTextColor(BLACK, RED);
tft.print(mlx.readObjectTempC());
if(mlx.readObjectTempC() > -20){
tone(24, 200, 300);
}
if(mlx.readObjectTempC() > -10){
tone(24, 200, 300);
}
if(mlx.readObjectTempC() > 0){
tone(24, 200, 300);
}
if(mlx.readObjectTempC() > 10){
tone(24, 230, 300);
}
if(mlx.readObjectTempC() > 25){
tone(24, 262, 300);
}
if(mlx.readObjectTempC() > 26){
tone(24, 294, 300);
}
if(mlx.readObjectTempC() > 27){
tone(24, 330, 300);
}
if(mlx.readObjectTempC() > 28){
tone(24, 349, 300);
}
if(mlx.readObjectTempC() > 29){
tone(24, 379, 300);
}
if(mlx.readObjectTempC() > 30){
tone(24, 409, 300);
}
if(mlx.readObjectTempC() > 40){
tone(24, 502, 300);
}
if(mlx.readObjectTempC() > 70){
tone(24, 200, 300);
}
}
}
void startFahrenheit()
{
tft.fillScreen(BLACK);
tft.setCursor(32, 20);
tft.setTextSize(2);
tft.setTextColor(WHITE);
tft.print("Temperature in F");
tft.fillRect(30, 60, 240, 70, RED);
tft.drawRect(30, 60, 240, 70, WHITE);
tft.setCursor(38, 72);
tft.setTextColor(BLACK);
tft.print("Environment:");
tft.setCursor(185, 72);
tft.setTextColor(BLACK);
tft.print(mlx.readAmbientTempF());
tft.setCursor(255, 72);
tft.setTextColor(BLACK);
tft.print("F");
tft.setCursor(38, 92);
tft.setTextColor(BLACK);
tft.print("Target:");
tft.setCursor(125, 92);
tft.setTextColor(BLACK);
tft.print(mlx.readObjectTempF());
tft.setCursor(200, 92);
tft.setTextColor(BLACK);
tft.print("F");
if(mlx.readObjectTempF() > -20){
tone(24, 200, 300);
}
if(mlx.readObjectTempF() > -10){
tone(24, 200, 300);
}
if(mlx.readObjectTempF() > 0){
tone(24, 200, 300);
}
if(mlx.readObjectTempF() > 10){
tone(24, 230, 300);
}
if(mlx.readObjectTempF() > 25){
tone(24, 262, 300);
}
if(mlx.readObjectTempF() > 26){
tone(24, 294, 300);
}
if(mlx.readObjectTempF() > 27){
tone(24, 330, 300);
}
if(mlx.readObjectTempF() > 28){
tone(24, 349, 300);
}
if(mlx.readObjectTempF() > 60){
tone(24, 392, 300);
}
if(mlx.readObjectTempF() > 70){
tone(24, 200, 300);
}
backMenuIcon();
}
void startCalendar()
{
tft.fillScreen(BLACK);
tft.setCursor(32, 20);
tft.setTextSize(2);
tft.setTextColor(WHITE);
tft.print("Calendar");
backMenuIcon();
}
void backMenu() //Takaisin napin toiminto
{
tft.fillRoundRect(5, 195, 50, 30, 8, WHITE);
delay(70);
tft.fillRoundRect(5, 195, 50, 30, 8, BLUE);
tft.drawRoundRect(5, 195, 50, 30, 8, WHITE);
tft.setCursor(15, 204);
tft.print("<-");
delay(70);
tft.fillRoundRect(5, 195, 50, 30, 8, BLACK);
currentpage = 0;
}
void backMenuIcon() //Takaisin napin kuva
{
tft.fillRoundRect(5, 195, 50, 30, 8, BLUE);
tft.drawRoundRect(5, 195, 50, 30, 8, WHITE);
tft.setCursor(15, 204);
tft.setTextColor(BLACK);
tft.print("<-");
}