My goal is to assign a function (Logging_Function()) to a start/stop button.
For some reason using "TS_Point p = ts.getPoint()" is stopping the "void loop" I think since the time and analog values are not refreshing.
I am pretty much a beginner so please try and keep it simple. Thank you.
#include <Adafruit_GFX.h> // LCD GUI library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
#include "RTClib.h" //RTC Library
#include <MCUFRIEND_kbv.h> //LCD Library
#include <SPI.h> //SPI Library
#include <SD.h> //SD Module Library
#include "TouchScreen.h"
#include <stdint.h>
#include <Wire.h> // this is needed even tho we aren't using it
#include <Adafruit_STMPE610.h>
#define YP A2 // must be an analog pin, use "An" notation!
#define XM A3 // must be an analog pin, use "An" notation!
#define YM 8 // can be a digital pin
#define XP 9 // can be a digital pin
#define TS_MINX 150
#define TS_MINY 150
#define TS_MAXX 3800
#define TS_MAXY 4000
const int chipSelect = 4;
char daysOfTheWeek[7][12] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
#define YP A3 // must be an analog pin, use "An" notation!
#define XM A2 // must be an analog pin, use "An" notation!
#define YM 9 // can be a digital pin
#define XP 8 // can be a digital pin
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4
#define STMPE_CS 8
Adafruit_STMPE610 ts = Adafruit_STMPE610(STMPE_CS);
// Assign human-readable names to some common 16-bit color values:
#define BLACK 0x0000 /* 0, 0, 0 */
#define NAVY 0x000F /* 0, 0, 128 */
#define DARKGREEN 0x03E0 /* 0, 128, 0 */
#define DARKCYAN 0x03EF /* 0, 128, 128 */
#define MAROON 0x7800 /* 128, 0, 0 */
#define PURPLE 0x780F /* 128, 0, 128 */
#define OLIVE 0x7BE0 /* 128, 128, 0 */
#define LIGHTGREY 0xC618 /* 192, 192, 192 */
#define DARKGREY 0x7BEF /* 128, 128, 128 */
#define BLUE 0x001F /* 0, 0, 255 */
#define GREEN 0x07E0 /* 0, 255, 0 */
#define CYAN 0x07FF /* 0, 255, 255 */
#define RED 0xF800 /* 255, 0, 0 */
#define MAGENTA 0xF81F /* 255, 0, 255 */
#define YELLOW 0xFFE0 /* 255, 255, 0 */
#define WHITE 0xFFFF /* 255, 255, 255 */
#define ORANGE 0xFDA0 /* 255, 180, 0 */
#define GREENYELLOW 0xB7E0 /* 180, 255, 0 */
#define PINK 0xFC9F
RTC_DS3231 rtc;
MCUFRIEND_kbv tft;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
tft.reset();
uint16_t identifier = tft.readID();
if(identifier==0x0101)
{
identifier=0x9341;
Serial.println(F("Found 0x9341 LCD driver"));
}
tft.begin(identifier);
tft.setRotation(1);
tft.fillScreen(BLACK);
tft.fillRect(0, 0, 320, 25, NAVY);
rtc.begin(); // Initialize the rtc object
tft.fillRect(100, 0, 200, 25, NAVY);
tft.setTextColor(ORANGE);
tft.setCursor(100, 0);
tft.print("Initializing RTC");
delay(500);
tft.fillRect(100, 0, 200, 25, NAVY);
if (! rtc.begin()) {
tft.fillRect(100, 0, 200, 25, NAVY);
tft.setTextColor(RED);
tft.setCursor(100, 0);
tft.print("RTC Error");
delay(500);
tft.fillRect(100, 0, 200, 25, NAVY);
}
else{
tft.fillRect(100, 0, 200, 25, NAVY);
tft.setTextColor(GREEN);
tft.setCursor(100, 0);
tft.print("RTC Initialized");
delay(500);
tft.fillRect(100, 0, 200, 25, NAVY);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, let's set the time!");
tft.fillRect(100, 0, 200, 25, NAVY);
tft.setTextColor(RED);
tft.setCursor(100, 0);
tft.print("Connect to PC to Sync Time");
delay(500);
tft.fillRect(100, 0, 200, 25, NAVY);
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
tft.fillRect(100, 0, 200, 25, NAVY);
tft.setTextColor(ORANGE);
tft.setCursor(100, 0);
tft.print("Initializing SD");
delay(500);
tft.fillRect(100, 0, 200, 25, NAVY);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
tft.fillRect(100, 0, 200, 25, NAVY);
tft.setTextColor(RED);
tft.setCursor(100, 0);
tft.print("SD Error");
delay(500);
tft.setCursor(100, 0);
tft.fillRect(100, 0, 200, 25, NAVY);
}
else{
tft.fillRect(100, 0, 200, 25, NAVY);
tft.setTextColor(GREEN);
tft.setCursor(100, 0);
tft.print("SD Initialized");
delay(500);
tft.setCursor(100, 0);
tft.fillRect(100, 0, 200, 25, NAVY);
}
/* if (!ts.begin()) {
tft.fillRect(100, 0, 200, 25, NAVY);
tft.setTextColor(RED);
tft.setCursor(100, 0);
tft.print("Touch Screen Error");
delay(500);
tft.setCursor(100, 0);
tft.fillRect(100, 0, 200, 25, NAVY);
while (1);
}
else{
tft.fillRect(100, 0, 200, 25, NAVY);
tft.setTextColor(GREEN);
tft.setCursor(100, 0);
tft.print("Touch Screen Initialized");
delay(500);
tft.setCursor(100, 0);
tft.fillRect(100, 0, 200, 25, NAVY);
}*/
}
void RTC_Display(){
tft.fillRect(250, 0, 75, 25, NAVY);
DateTime now = rtc.now();
tft.setTextColor(WHITE);
tft.setTextSize(1);
tft.setCursor(0, 0);
tft.print(now.day(), DEC);
tft.print("-");
tft.print(now.month(), DEC);
tft.print("-");
tft.print(now.year(), DEC);
tft.setCursor(0, 15);
tft.print(daysOfTheWeek[now.dayOfTheWeek()]);
tft.setCursor(270, 0);
tft.print(now.hour(), DEC);
tft.print(":");
tft.print(now.minute(), DEC);
tft.print(":");
tft.print(now.second(), DEC);
}
void Logging_Function(){
String dataString = "";
float ScalingFactor_Press = 0.5;
float ScalingFactor_Flow = 0.5;
float ScalingFactor_Temp = 0.5;
float Press = analogRead(A5);
Press = Press - 335;
Press = Press*ScalingFactor_Press/1000;
float Flow = analogRead(A6);
Flow = Flow - 360;
Flow = Flow*ScalingFactor_Flow/1000;
float Temp = analogRead(A7);
Temp = Temp - 320;
Temp = Temp*ScalingFactor_Temp/1000;
dataString += String(",Pressure:");
dataString += String(Press);
dataString += String(",Flow Rate:");
dataString += String(Flow);
dataString += String(",Temperature:");
dataString += String(Temp);
/*
if (!SD.begin(chipSelect)) {
tft.setTextColor(RED);
tft.setCursor(100, 0);
tft.fillRect(100, 0, 150, 25, NAVY);
tft.print("SD Error on Logging");
}
else{
*/
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
DateTime now = rtc.now();
dataFile.print("Logged ON: ");
dataFile.print(now.year(), DEC);
dataFile.print("-");
dataFile.print(now.month(), DEC);
dataFile.print("-");
dataFile.print(now.day(), DEC);
dataFile.print(" at :");
dataFile.print(now.hour(), DEC);
dataFile.print(":");
dataFile.print(now.minute(), DEC);
dataFile.print(":");
dataFile.print(now.second(), DEC);
dataFile.print(" -- ");
dataFile.println(dataString);
dataFile.close();
tft.setCursor(100, 0);
tft.fillRect(100, 0, 150, 25, NAVY);
tft.setTextColor(YELLOW);
tft.setCursor(100, 0);
tft.print("Log Value Saved");
}
else{
if (!SD.begin(chipSelect)) {
tft.setTextColor(RED);
tft.setCursor(100, 0);
tft.fillRect(100, 0, 150, 25, NAVY);
tft.print("SD Error on Logging");
}
}
}
void Logging_Display(){
float ScalingFactor_Press = 0.5;
float ScalingFactor_Flow = 0.5;
float ScalingFactor_Temp = 0.5;
float Press = analogRead(A5);
tft.setTextColor(GREEN);
tft.setCursor(20, 75);
tft.print("PRESSURE");
// tft.fillRect(90, 65, 150, 25, BLACK);
tft.fillRect(90, 65, 150, 25, GREEN);
tft.setTextColor(BLACK);
tft.setCursor(125, 75);
Press = Press - 335;
Press = Press*ScalingFactor_Press/1000;
tft.print(Press);
tft.print(" BAR");
float Flow = analogRead(A6);
tft.setTextColor(YELLOW);
tft.setCursor(20, 100);
tft.print("FLOW RATE");
// tft.fillRect(90, 90, 150, 25, BLACK);
tft.fillRect(90, 90, 150, 25, YELLOW);
tft.setTextColor(BLACK);
tft.setCursor(125, 100);
Flow = Flow - 360;
Flow = Flow*ScalingFactor_Flow/1000;
tft.print(Flow);
tft.print(" LPM");
float Temp = analogRead(A7);
tft.setTextColor(RED);
tft.setCursor(20, 125);
tft.print("TEMPERATURE");
// tft.fillRect(90, 115, 150, 25, BLACK);
tft.fillRect(90, 115, 150, 25, RED);
tft.setTextColor(BLACK);
tft.setCursor(125, 125);
Temp = Temp - 320;
Temp = Temp*ScalingFactor_Temp/1000;
tft.print(Temp);
tft.print(" C");
}
void Home_Buttons(){
tft.fillRect(120,200,120,40,CYAN);
tft.drawRect(120,200,120,40,WHITE);
}
void loop() {
RTC_Display();
Logging_Display();
Home_Buttons();
if (ts.bufferEmpty()){
return;}
TS_Point p = ts.getPoint();
// Serial.println(p.x);
// Serial.println(p.y);
// p.x=map(p.x, TS_MINX, TS_MAXX,0,tft.width());
// p.y=map(p.y, TS_MINY, TS_MAXY,0,tft.height());
// Serial.println(p.x);
// Serial.println(p.y);
// if(p.x>120 && p.x<240){
// if(p.y>200 && p.y<240){
// Logging_Function();
//
// }
// }
}
