Hello,
I am attempting to create a project with a 2.8inch ELEGOO TFT Shield hooked up to a prototype shield on Arduino Uno with a MiCS5524 sensor.
What I'm trying to do is have the arduino print a random word from an array of words I created to the TFT screen, when it reads a certain value or range of values from the MiCS5524 sensor. So far, everything is working great - the sensor is reading, the TFT shield is communicating between it and the arduino, and the conditional statements function in that it does the "else" function too when it's called.
In my serial monitor, the words are continuously being printed as it reads from the sensor, yet my TFT shield stops printing them after the fourth word. It continues doing the other "else" function (fillScreen with green), but the words just don't keep printing.
Here is where my code is at currently:
// IMPORTANT: Adafruit_TFTLCD LIBRARY MUST BE SPECIFICALLY
// CONFIGURED FOR EITHER THE TFT SHIELD OR THE BREAKOUT BOARD.
// SEE RELEVANT COMMENTS IN Adafruit_TFTLCD.h FOR SETUP.
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
// The control pins for the LCD can be assigned to any digital or
// analog pins...but we'll use the analog pins as this allows us to
// double up the pins with the touch screen (see the TFT paint example).
#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_RS A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0
#define LCD_RESET 11 // Can alternately just connect to Arduino's reset pin
// When using the BREAKOUT BOARD only, use these 8 data lines to the LCD:
// For the Arduino Uno, Duemilanove, Diecimila, etc.:
// D0 connects to digital pin 8 (Notice these are
// D1 connects to digital pin 9 NOT in order!)
// D2 connects to digital pin 2
// D3 connects to digital pin 3
// D4 connects to digital pin 4
// D5 connects to digital pin 5
// D6 connects to digital pin 6
// D7 connects to digital pin 7
// Assign human-readable names to some common 16-bit color values:
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
//#define USE_ADAFRUIT_SHIELD_PINOUT
//TFT.clearDisplay();
Adafruit_TFTLCD tft(LCD_CS, LCD_RS, LCD_WR, LCD_RD, LCD_RESET);
int wormdata = 0;
const char *worms[] = {"The", "roots", "the", "of", "trees", "tangle", "and", "And", "squirm", "together", "creaking", "to", "To", "melody", "of", "Of", "A", "a", "long", "winter", "where", "Where", "What", "what", "bears", "dream", "wet", "warm", "flesh", "to", "To", "boil", "Their", "their", "insides", "back", "normalcy", "For", "every", "every", "Spring", "tree", "is", "Is", "accomplice", "murder", "birth", "Plants", "plants", "Me", "we", "me", "We", "may", "being", "food", "mammals", "or", "mere", "lover" "bees", "fuzzy", "clandesetine", "legs", "that", "That", "trick", "goo", "intimacy", "life", "out", "pores", "flower", "perhaps", "fulfill", "They", "they", "be", "cloned", "mass-gardened", "sibling", "siblings", "thousands", "enlisting", "into", "An", "an", "unproclaimed", "state", "war", "War", "on", "its", "it's", "ancestral", "wild", "family", "ungoverned", "roots", "pollen-bearers", "root-bearing", "heavyweights", "suffocate", "as", "there", "their", "fabricated", "siblings", "suck", "all", "sun", "from", "air", "until", "all", "All", "left", "is", "fakes", "If", "only", "fake", "remains", "does", "Does", "become", "new", "original", "We", "I", "Me", "Our", "our", "records", "in", "DNA", "positionality", "components", "Physical", "Being", "place", "can", "store", "memories", "parents", "children", "ancestors", "natural", "Without", "without", "my", "My", "vines", "insect", "lovers", "companions", "death", "loss", "forage", "pillage", "mush", "eating", "eat", "eaten", "chewed", "absorbed", "sponge", "spores", "fade", "away", "black", "hole", "nothing", "darkness", "dark", "light", "bright", "internet", "things", "who", "Who", "Why", "why"};
unsigned long worm1;
void setup(void) {
Serial.begin(9600);
Serial.println(F("TFT LCD test"));
#ifdef USE_ADAFRUIT_SHIELD_PINOUT
Serial.println(F("Using Adafruit 2.8\" TFT Arduino Shield Pinout"));
#else
Serial.println(F("Using Adafruit 2.8\" TFT Breakout Board Pinout"));
#endif
Serial.print("TFT size is "); Serial.print(tft.width()); Serial.print("x"); Serial.println(tft.height());
tft.reset();
uint16_t identifier = tft.readID();
if(identifier == 0x9325) {
Serial.println(F("Found ILI9325 LCD driver"));
} else if(identifier == 0x9328) {
Serial.println(F("Found ILI9328 LCD driver"));
} else if(identifier == 0x7575) {
Serial.println(F("Found HX8347G LCD driver"));
} else if(identifier == 0x9341) {
Serial.println(F("Found ILI9341 LCD driver"));
} else if(identifier == 0x8357) {
Serial.println(F("Found HX8357D LCD driver"));
}
tft.begin(identifier);
Serial.println(F("Done!"));
Serial.println("MiCS-5524 Data Below!");
randomSeed(494);
}
void loop(void) {
tft.fillScreen(BLACK);
// unsigned long start = micros();
wormdata = analogRead(A4);
// Serial.println(wormdata);
Serial.println(worms[worm1]);
delay(3000);
if (wormdata > 5) {
tft.fillScreen(BLACK);
tft.println();
tft.setTextColor(BLUE);
tft.setTextSize(5);
worm1 = random(sizeof(worms)/sizeof(char*));
tft.println(worms[worm1]);
delay(3000);
}
else
{
tft.fillScreen(GREEN);
delay(3000);
}
// return micros() - start;
}
I suspected it could be a memory issue, since my code uses 53% of storage space and 90% of dynamic memory. I tried combatting this by converting everything to case switch - calling multiple arrays (same words, just broken up and redistributed) at random rather than the word itself. Like this:
void loop(void) {
tft.fillScreen(BLACK);
wormdata = analogRead(A4);
tft.fillScreen(BLACK);
tft.println();
tft.setTextColor(BLUE);
tft.setTextSize(5);
worm2 = random(sizeof(worms2)/sizeof(char*));
worm3 = random(sizeof(worms3)/sizeof(char*));
worm4 = random(sizeof(worms4)/sizeof(char*));
worm5 = random(sizeof(worms5)/sizeof(char*));
if (wormdata > 0) {
int worm_random = random (0,4);
switch (worm_random) {
case '0':
tft.fillScreen(BLACK);
tft.println();
tft.setTextColor(BLUE);
tft.setTextSize(5);
tft.println(worms2[worm2]);
break;
case '1':
tft.fillScreen(BLACK);
tft.println();
tft.setTextColor(BLUE);
tft.setTextSize(5);
tft.println(worms3[worm3]);
break;
case '2':
tft.fillScreen(BLACK);
tft.println();
tft.setTextColor(BLUE);
tft.setTextSize(5);
tft.println(worms4[worm4]);
break;
case '3':
tft.fillScreen(BLACK);
tft.println();
tft.setTextColor(BLUE);
tft.setTextSize(5);
tft.println(worms5[worm5]);
break;
}
}
else
{
tft.fillScreen(GREEN);
Serial.println("Values incorrect or unreadable. Double check pins.");
delay(1);
}
}
I also tried storing the array with PROGMEM, and I don't think I understand that fully because it lowered my storage down to 70%, but my TFT was still only printing four words. I also don't even remember how I did that, to be frank.
Something like
const char *worms[183] PROGMEM = { *insert words for array*};
I am at a loss on how I could keep going at this. The randomness of the word generation is pretty important, so I don't want to compromise that if possible, nor the number of words... Right now I have 183 words. Any help would be super greatly appreciated! I am pretty new to Arduino and programming in general so I am not the best at optimization nor accuracy. Thanks again!