BMP images on TFT display

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?

Maybe it's some memory leak or smth,

Put you phone away. We don't do text-speak here.

You may, on the micro, be able to have a couple of pictures. You don't have enough memory to have 8 pictures.

PaulS:

Maybe it's some memory leak or smth,

Put you phone away. We don't do text-speak here.

You may, on the micro, be able to have a couple of pictures. You don't have enough memory to have 8 pictures.

I'm sorry for it.

Maybe there is possibility to load image only when it need to display and then somehow close it, when other picture appears (and loads to same variable)?

Maybe there is possibility to load image only when it need to display and then somehow close it, when other picture appears (and loads to same variable)?

Maybe. Get rid of the global instances one, two three, etc. Create local variables when needed. Let us know what happens.

First two or three photos works fine, but other do not display, on the screen appears only lines.

Check that the other shots are in the right form of bmp file. Do this by renaming the two files that work, the names of the other files. This checks if it is the files or your code that is wrong. My guess is on the files.

PaulS:

Maybe there is possibility to load image only when it need to display and then somehow close it, when other picture appears (and loads to same variable)?

Maybe. Get rid of the global instances one, two three, etc. Create local variables when needed. Let us know what happens.

I create local variables, and now for each button code looks like this:

  • if (digitalRead(2) == HIGH){*
  • PImage img;*
  • img = screen.loadImage("one.bmp");*
  • screen.image(img, 0, 0);*
  • while (digitalRead(2) == HIGH){};*

At the begining, images displays fine, but after 12 times screen goes like this:
https://dl.dropboxusercontent.com/u/55828149/IMAG0098.jpg
and displaying stops.

Any ideas?

Any ideas?

Check whether you are running out of memory.
http://playground.arduino.cc/Code/AvailableMemory

Post your new code (using the # icon).

PaulS:

Any ideas?

Check whether you are running out of memory.
http://playground.arduino.cc/Code/AvailableMemory

Post your new code (using the # icon).

Now I made it with procedures and its local variables for images. Still same problem…

#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);

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);

}

void loop() {
    if (digitalRead(2) == HIGH){
      int i = 2;
      char name[] = "one.bmp";
      images(name,i);
}
    if (digitalRead(3) == HIGH){
      int i = 3;
      char name[] = "two.bmp";
      images(name,i);
}
    if (digitalRead(4) == HIGH){
      int i = 4;
      char name[] = "three.bmp";
      images(name,i);
}
    if (digitalRead(5) == HIGH){
      int i = 5;
      char name[] = "four.bmp";
      images(name,i);
}
    if (digitalRead(6) == HIGH){
      int i = 6;
      char name[] = "five.bmp";
      images(name,i);
} 
    if (digitalRead(9) == HIGH){
      int i = 9;
      char name[] = "six.bmp";
      images(name,i);
}
    if (digitalRead(10) == HIGH){
      int i = 10;
      char name[] = "seven.bmp";
      images(name,i);
}
    if (digitalRead(11) == HIGH){
      int i = 11;
      char name[] = "eight.bmp";
      images(name,i);
}

}

void images(char name[], int i){
  PImage img;
  img = screen.loadImage(name);
  screen.image(img, 0, 0);
  while (digitalRead(i) == HIGH){};
}

Still same problem..

How much memory do you have after each button press? It may be that the image memory is not being freed. A link to the library you are using would be useful.

PaulS:

Still same problem..

How much memory do you have after each button press? It may be that the image memory is not being freed. A link to the library you are using would be useful.

I tried that FreeMemory library:

freeMemory()=912
freeMemory()=881
freeMemory()=850
freeMemory()=819
freeMemory()=788
freeMemory()=757
freeMemory()=726
freeMemory()=695
freeMemory()=664
freeMemory()=633

This is the log after each photo load

I use this library for display:

Maybe debounce the buttons.
Try consecutively loading the images through software in a loop.