Hello! I am fairly new to Arduino, and have been playing around with the Arduino Uno Rev3 and a 3.5 inch TFT LCD Display compatible with Arduino Uno Rev3, and have a question about coding loops. As I have the code now, the goal was to use the touchscreen to move from one screen the next, until you get to the end the 5 pages, then the loop starts at the first page, and only moves to the next when the screen is touched.
My issue is that once I upload the code to the Arduino, the 5 pages loop continuously, and do not stop. I do not know what I did wrong or what I am missing, and am posting the entirety of my code below.
Thank you very much for your help with this!
#include <Adafruit_GFX.h> // Core graphics library
#include <MCUFRIEND_kbv.h>
#include <TouchScreen.h>
#include <SPI.h>
#include <SD.h>
int nextMenu = 0;
#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD 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 YP A2
#define XM A3
#define YM 8
#define XP 9
int X, Y;
#define SD_CS 10 //SD card pin on your shield
#define TS_LEFT 126
#define TS_RT 876
#define TS_TOP 88
#define TS_BOT 875
#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 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
#define TS_MINX 100
#define TS_MINY 120
#define TS_MAXX 940
#define TS_MAXY 920
#define TS_LEFT 126
#define TS_RT 907
#define TS_TOP 78
#define TS_BOT 889
#define MINPRESSURE 10
#define MAXPRESSURE 1000
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
MCUFRIEND_kbv tft;
TSPoint waitTouch() {
TSPoint p;
do {
p = ts.getPoint();
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
} while ((p.z < MINPRESSURE ) || (p.z > MAXPRESSURE));
p.x = map(p.x, TS_LEFT, TS_RT, 0, 240);
p.y = map(p.y,TS_TOP, TS_BOT, 0, 320);
return p;
}
void setup(){
Serial.begin(9600);
uint16_t identifier = tft.readID();
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
tft.begin(identifier);
if (!SD.begin(SD_CS)) {
progmemPrintln(PSTR("failed!"));
return;
}
tft.reset();
tft.begin(identifier);
tft.setRotation(3);
tft.invertDisplay(0);
tft.fillScreen(BLACK);
tft.setTextSize(2);
tft.setTextColor(GREEN);
tft.setCursor(150, 145);
tft.print("Initializing...");
delay(500);
tft.fillScreen(BLACK);
tft.setTextSize(2);
tft.setTextColor(GREEN);
tft.setCursor(150, 145);
tft.print("Initializing...");
delay(500);
tft.fillScreen(BLACK);
tft.setTextSize(2);
tft.setTextColor(GREEN);
tft.setCursor(150, 145);
tft.print("Initializing...");
delay(500);
tft.fillScreen(BLACK);
tft.setTextSize(1);
for (int i = 0; i < 15; i += 7) {
tft.setCursor(0, i * 16);
tft.println("DLP Vault Pip Boy initializing… \n\nCode:01100100000_101110_1110100011_001001100 _ Code complete \n\nStarting the main screen_DLP's Vault \n\nWelcome to DLP's Vault \n\nThe date is 05/07/2289, 12:22, Have a LOVELY day!");
delay(500);
}
tft.fillScreen(BLACK); // Clear the screen before drawing image
bmpDraw("stat.bmp", 0, 0);
//Calling the bmpDraw function ("Name_of_your_image.bmp",x,y) (x,y) is the starting position of the picture drawing
}
void loop(){
//bmpDraw("mone.bmp", 0, 0); //Calling the bmpDraw function ("Name_of_your_image.bmp",x,y) (x,y) is the starting position of the picture drawing
TSPoint p = waitTouch();
X = p.x; Y = p.y;
Serial.println(X);
Serial.println(Y);
if(X <= 240 || Y <= 320){
nextMenu++;
}
if(nextMenu == 0){
bmpDraw("stat.bmp", 0, 0);
}
if(nextMenu == 1){
bmpDraw("inv.bmp", 0, 0);
}
if(nextMenu == 2){
bmpDraw("data.bmp", 0, 0);
}
if(nextMenu == 3){
bmpDraw("map.bmp", 0, 0);
}
if(nextMenu == 4){
bmpDraw("radio.bmp", 0, 0);
}
if(nextMenu == 5){
nextMenu = -1;
}
}
#define BUFFPIXEL 20 //Drawing speed, 20 is meant to be the best but you can use 60 altough it takes a lot of uno's RAM
//Drawing function, reads the file from the SD card and do the
//conversion and drawing, also it shows messages on the Serial monitor in case of a problem
//No touchy to this function :D
void bmpDraw(String filename, int x, int y) {
File bmpFile;
int bmpWidth, bmpHeight; // W+H in pixels
uint8_t bmpDepth; // Bit depth (currently must be 24)
uint32_t bmpImageoffset; // Start of image data in file
uint32_t rowSize; // Not always = bmpWidth; may have padding
uint8_t sdbuffer[3*BUFFPIXEL]; // pixel in buffer (R+G+B per pixel)
uint16_t lcdbuffer[BUFFPIXEL]; // pixel out buffer (16-bit per pixel)
uint8_t buffidx = sizeof(sdbuffer); // Current position in sdbuffer
boolean goodBmp = false; // Set to true on valid header parse
boolean flip = true; // BMP is stored bottom-to-top
int w, h, row, col;
uint8_t r, g, b;
uint32_t pos = 0, startTime = millis();
uint8_t lcdidx = 0;
boolean first = true;
if((x >= tft.width()) || (y >= tft.height())) return;
Serial.println();
progmemPrint(PSTR("Loading image '"));
Serial.print(filename);
Serial.println('\'');
// Open requested file on SD card
if ((bmpFile = SD.open(filename)) == NULL) {
progmemPrintln(PSTR("File not found"));
return;
}
// Parse BMP header
if(read16(bmpFile) == 0x4D42) { // BMP signature
progmemPrint(PSTR("File size: ")); Serial.println(read32(bmpFile));
(void)read32(bmpFile); // Read & ignore creator bytes
bmpImageoffset = read32(bmpFile); // Start of image data
progmemPrint(PSTR("Image Offset: ")); Serial.println(bmpImageoffset, DEC);
// Read DIB header
progmemPrint(PSTR("Header size: ")); Serial.println(read32(bmpFile));
bmpWidth = read32(bmpFile);
bmpHeight = read32(bmpFile);
if(read16(bmpFile) == 1) { // # planes -- must be '1'
bmpDepth = read16(bmpFile); // bits per pixel
progmemPrint(PSTR("Bit Depth: ")); //Serial.println(bmpDepth);
if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed
goodBmp = true; // Supported BMP format -- proceed!
progmemPrint(PSTR("Image size: "));
Serial.print(bmpWidth);
Serial.print('x');
Serial.println(bmpHeight);
// BMP rows are padded (if needed) to 4-byte boundary
rowSize = (bmpWidth * 3 + 3) & ~3;
// If bmpHeight is negative, image is in top-down order.
// This is not canon but has been observed in the wild.
if(bmpHeight < 0) {
bmpHeight = -bmpHeight;
flip = false;
}
// Crop area to be loaded
w = bmpWidth;
h = bmpHeight;
if((x+w-1) >= tft.width()) w = tft.width() - x;
if((y+h-1) >= tft.height()) h = tft.height() - y;
// Set TFT address window to clipped image bounds
tft.setAddrWindow(x, y, x+w-1, y+h-1);
for (row=0; row<h; row++) { // For each scanline...
// Seek to start of scan line. It might seem labor-
// intensive to be doing this on every line, but this
// method covers a lot of gritty details like cropping
// and scanline padding. Also, the seek only takes
// place if the file position actually needs to change
// (avoids a lot of cluster math in SD library).
if(flip) // Bitmap is stored bottom-to-top order (normal BMP)
pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;
else // Bitmap is stored top-to-bottom
pos = bmpImageoffset + row * rowSize;
if(bmpFile.position() != pos) { // Need seek?
bmpFile.seek(pos);
buffidx = sizeof(sdbuffer); // Force buffer reload
}
for (col=0; col<w; col++) { // For each column...
// Time to read more pixel data?
if (buffidx >= sizeof(sdbuffer)) { // Indeed
// Push LCD buffer to the display first
if(lcdidx > 0) {
tft.pushColors(lcdbuffer, lcdidx, first);
lcdidx = 0;
first = false;
}
bmpFile.read(sdbuffer, sizeof(sdbuffer));
buffidx = 0; // Set index to beginning
}
// Convert pixel from BMP to TFT format
b = sdbuffer[buffidx++];
g = sdbuffer[buffidx++];
r = sdbuffer[buffidx++];
lcdbuffer[lcdidx++] = tft.color565(r,g,b);
} // end pixel
} // end scanline
// Write any remaining data to LCD
if(lcdidx > 0) {
tft.pushColors(lcdbuffer, lcdidx, first);
}
progmemPrint(PSTR("Loaded in "));
Serial.print(millis() - startTime);
Serial.println(" ms");
} // end goodBmp
}
}
bmpFile.close();
if(!goodBmp) progmemPrintln(PSTR("BMP format not recognized."));
}
// These read 16- and 32-bit types from the SD card file.
// BMP data is stored little-endian, Arduino is little-endian too.
// May need to reverse subscript order if porting elsewhere.
uint16_t read16(File f) {
uint16_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read(); // MSB
return result;
}
uint32_t read32(File f) {
uint32_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read();
((uint8_t *)&result)[2] = f.read();
((uint8_t *)&result)[3] = f.read(); // MSB
return result;
}
// Copy string from flash to serial port
// Source string MUST be inside a PSTR() declaration!
void progmemPrint(const char *str) {
char c;
while(c = pgm_read_byte(str++)) Serial.print(c);
}
// Same as above, with trailing newline
void progmemPrintln(const char *str) {
progmemPrint(str);
Serial.println();
}