TFT display multiple images

Hello,

I have Arduino Micro and Arduino TFT display. I want to display one of 8 bmp images on the screen by the simple 8 buttons.

I write the code like this:

#include <SPI.h>
#include <SD.h>
#include <TFT.h>
#define sd_cs 8
#define lcd_cs 7
#define dc 0
*#define rst 1 *
TFT screen = TFT(lcd_cs, dc, rst);

  • PImage one, two, three, four, five, six, seven, eight;*
    void setup() {

  • pinMode(2, INPUT);*

  • pinMode(3, INPUT);*

  • pinMode(4, INPUT);*

  • pinMode(5, INPUT);*

  • pinMode(6, INPUT);*

  • pinMode(9, INPUT);*

  • pinMode(10, INPUT);*

  • pinMode(11, INPUT);*

  • screen.begin();*

  • SD.begin(sd_cs);*

  • screen.background(255, 255, 255);*

  • one = screen.loadImage("one.bmp");*

  • two = screen.loadImage("two.bmp");*

  • three = screen.loadImage("three.bmp");*

  • four = screen.loadImage("four.bmp");*

  • five = screen.loadImage("five.bmp");*

  • six = screen.loadImage("six.bmp");*

  • seven = screen.loadImage("seven.bmp");*

  • eight = screen.loadImage("eight.bmp");*

}
void loop() {

  • if (digitalRead(2) == HIGH){*
  • screen.image(one, 0, 0);*
  • while (digitalRead(2) == HIGH){};*
    }
  • if (digitalRead(3) == HIGH){*
  • screen.image(two, 0, 0);*
  • while (digitalRead(3) == HIGH){};*
    }
  • if (digitalRead(4) == HIGH){*
  • screen.image(three, 0, 0);*
  • while (digitalRead(4) == HIGH){};*
    }
  • if (digitalRead(5) == HIGH){*
  • screen.image(four, 0, 0);*
  • while (digitalRead(5) == HIGH){};*
    }
  • if (digitalRead(6) == HIGH){*
  • screen.image(five, 0, 0);*
  • while (digitalRead(6) == HIGH){};*
    }
  • if (digitalRead(9) == HIGH){*
  • screen.image(six, 0, 0);*
  • while (digitalRead(9) == HIGH){};*
    }
  • if (digitalRead(10) == HIGH){*
  • screen.image(seven, 0, 0);*
  • while (digitalRead(10) == HIGH){};*
    }
  • if (digitalRead(11) == HIGH){*
  • screen.image(eight, 0, 0);*
  • while (digitalRead(11) == HIGH){};*
    }
    }

First two or three photos works fine, but other do not display, on the screen appears only lines.
Maybe it's some memory leak or smth,

Can you help me solve this problem?

You do not say how the buttons are connected.
I would do this:
change these like so:
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);

and the read lines to match:
if (digitalRead(2) == LOW){
screen.image(one, 0, 0);
while (digitalRead(2) == LOW){};
}

And wire the buttons to connect the pins to Gnd. The internal pullup resistors will hold the pins High when the buttons are not pressed.

If that doesn't solve it, try changing this line:

screen.image(one, 0, 0);

to call out the files that are not working (two thru eight), and confirm all the files can display.

My guess is you ran out of memory.

Try only loading them when you're about to display them, then freeing the memory afterwards.

Also, you could add the image checks after the loads:

if (image.isValid() != true) {
Serial.println("error while loading arduino.bmp");
}

Attempting to cache images on a microcontroller with 2k of RAM is just going to
overflow memory, just load the image when needed and use only one PImage variable??
How large are these images?

I have the same problem

that cannot get more than 2 pictures shown on the tft .

Have you found a solution??

Thanks, its working!