Sorry Below is the full New updated code
In the DMCDetailsPage function I am displaying the data to a tft screen But having the problems that I said above, even if I had another tft.setCursor(0, 46); tft.println("Number Owned: "); It breaks
Its like the variables have no data in them.
////#include "Adafruit_GFX.h"
////#include <SPI.h>
#include <Adafruit_ILI9341.h> // With a bit of SC card
#include <Keypad.h>
#include <SD.h>
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 8
const int SD_CS = 2;
File myFile;
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
uint8_t colPins[COLS] = { A1, A2, A3, A4 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 7, 6, 5, 4 }; // Pins connected to R1, R2, R3, R4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
byte wScreen = 0;
String FileDMCCode = "00";
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC,TFT_RST);
#define TextSize 2
void setup(void) {
pinMode(TFT_CS, OUTPUT);
digitalWrite(TFT_CS, HIGH);
pinMode(SD_CS, OUTPUT);
digitalWrite(SD_CS, HIGH);
//keypad.addEventListener(keypadEvent); // Add an event listener for this keypad
////////////////////////////////////////////////////////////////
tft.begin();
tft.setRotation(0);
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(TextSize);
/////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////
Serial.begin(9600);
while (!Serial);
Serial.print("Initializing SD card...");
//////////// IN SETUP() NEED ADD ///////////////////////////////////////////////////////////////////////////
if (!SD.begin(SD_CS)) {
Serial.println("No SD Card Found!");
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(TextSize);
tft.setCursor(0, 0);
tft.println("No SD Card Found!");
while (1);
}
////////////////////////////////////////////////////////////////////////////////////////
Serial.println("initialization done.");
//////////////////////////////////////////////////////////////////////////////////////////
}
void ReadFile(String fDMCCode) {
char AllText[80]; // > 70
char DMColText[40];
byte DMColTextLen = 40;
//String DMColText = "";
char DMCCode[6];
byte DMCCodeLen = 6;
char CRGBR[4];
byte CRGBRLen = 4;
char CRGBG[4];
byte CRGBGLen = 4;
char CRGBB[4];
byte CRGBBLen = 4;
char Anchor[8];
byte AnchorLen = 6;
char NumOwned[3];
byte NumOwnedLen = 3;
char character;
int index = 0;
if (SD.exists(fDMCCode + ".txt")) {
myFile = SD.open(fDMCCode + ".txt", FILE_READ);
char character;
while( (character = myFile.read()) != -1) {
AllText[index] = (char)character;
index +=1;
}
myFile.close();
memset(character, 0, sizeof(character));
index = 0;
getValue(AllText, ',', 0).toCharArray(DMCCode, DMCCodeLen);
//DMColTextLen = getValue(AllText, ',', 1).length();
//DMColText = getValue(AllText, ',', 1);
getValue(AllText, ',', 1).toCharArray(DMColText, DMColTextLen);
getValue(AllText, ',', 2).toCharArray(CRGBR, CRGBRLen);
getValue(AllText, ',', 3).toCharArray(CRGBG, CRGBGLen);
getValue(AllText, ',', 4).toCharArray(CRGBB, CRGBBLen);
getValue(AllText, ',', 5).toCharArray(Anchor, AnchorLen);
getValue(AllText, ',', 6).toCharArray(NumOwned, NumOwnedLen);
DMCDetailsPage(DMCCode,NumOwned, DMColText,CRGBR, CRGBG, CRGBB,Anchor);
memset(character, 0, sizeof(character));
memset(AllText, 0, sizeof(AllText));
memset(DMColText, 0, sizeof(DMColText));
memset(DMCCode, 0, sizeof(DMCCode));
memset(CRGBR, 0, sizeof(CRGBR));
memset(CRGBG, 0, sizeof(CRGBG));
memset(CRGBB, 0, sizeof(CRGBB));
memset(Anchor, 0, sizeof(Anchor));
memset(NumOwned, 0, sizeof(NumOwned));
index = 0;
}else{
wScreen = 5;
Serial.println(fDMCCode + " Does Not Exists.");
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(TextSize);
tft.setCursor(0, 0);
tft.println(fDMCCode + " Does Not Exists.");
tft.setCursor(0, 50);
tft.println("Press B for Search");
tft.setCursor(0, 100);
tft.println("Press D for Menu");
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
char key = keypad.getKey();
if (key == 'A'){
ReadFile(FileDMCCode);
}
}
void DMCDetailsPage(char cDMCCode[6],char cNumOwned[3], char cDMColText[40],String R, char G[4], char B[4],char cAnchor[6]){
if (strstr(cDMCCode, "310"))
{
tft.fillScreen(0x484848);
Serial.print("True");
Serial.print(" ");
}else{
tft.fillScreen(ILI9341_BLACK);
Serial.print("False");
Serial.print(" ");
}
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(TextSize);
// X Y
tft.setCursor(0, 10);
tft.println("DMC Code: ");
tft.setCursor(110, 10);
tft.println(cDMCCode); // Works if variables are not changed
tft.setCursor(0, 46);
tft.println("Number Owned: "); // breaks if this is added
//tft.setCursor(150, 46);
//tft.println(cNumOwned);
Serial.print(cDMCCode);
Serial.print(" ");
Serial.print(cDMColText);
Serial.print(" ");
Serial.print(R);
Serial.print(" ");
Serial.print(G);
Serial.print(" ");
Serial.print(B);
Serial.print(" ");
Serial.print(cAnchor);
Serial.print(" ");
Serial.print(cNumOwned);
FileDMCCode = "01";
}
///////////////////////////////////////////////////////////////////////////////
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = { 0, -1 };
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if (data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}
////////////////////////////////////////////////////////////////////////////////////////