Note: this page refers to a product that is retired.

EsploraTFTBitmapLogo

Use the Esplora as a controller to play a kart racing game.

This example for the Esplora with an Arduino screen reads a bitmap file from a SD card and displays it on screen in a random location.

For this example to work, you need to save an image named "logo.bmp" to the root of the SD card. The SD card needs to be FAT16 and FAT32 formatted. See the SD library documentation for more information on working with SD cards.

Hardware Required

  • Arduino Esplora

  • Arduino TFT screen

  • micro-SD card

  • image file

Circuit

Esplora GLCDBitmapLogo

Attach the TFT screen to the socket on your Esplora, with the label "SD Card" facing up. Insert the SD card with the "logo.bmp" file into the slot.

Code

To use the screen you must first include the SPI and TFT libraries. Don't forget to include the Esplora library and the SD library to read the image from the card.

1#include <Esplora.h>
2#include <TFT.h>
3#include <SPI.h>
4#include <SD.h>

You need to define the pin for the SD reader's chip select (CS). On the Esplora, it is mapped to pin 8.

1#define SD_CS 8

There is a special datatype called PImage for holding image information. Create a named version of PImage

1PImage logo;

In

setup()
, you're going to initialize the serial port and wait for it to become active before starting up. This means that you will need to turn on the serial monitor once the sketch has loaded. If you want to ignore the status information, comment out the
while()
loop.

Once serial communication has started, initialize the SD library. If there is an error, turn the Esplora's LED red.

1void setup() {
2
3 Serial.begin(9600);
4
5 while (!Serial) {
6
7 }
8
9 Serial.print("Initializing SD card...");
10
11 if (!SD.begin(SD_CS)) {
12
13 Serial.println("failed!");
14
15 Esplora.writeRed(255);
16
17 return;
18
19 }
20
21 Serial.println("OK!");

Initialize and clear the screen

1EsploraTFT.begin();
2
3 EsploraTFT.background(255, 255, 255);

Try reading the image file into the PImage you named earlier with

loadimage()
. Check that it is a valid image, and turn the Esplora's LED green if everything is OK, red if there is an error.

1Esplora.writeRGB(0, 0, 0);
2
3 logo = EsploraTFT.loadImage("logo.bmp");
4
5 if (logo.isValid()) {
6
7 Esplora.writeGreen(255);
8
9 }
10
11 else
12
13 Esplora.writeRed(255);
14
15}

If the image wasn't loaded correctly, stop the sketch before going any further.

1void loop() {
2
3 if (logo.isValid() == false) {
4
5 return;
6
7 }

If the image information is valid, pick a random spot on the screen to display the image. To make sure all the image is drawn onscreen, take the dimensions of the image and subtract that from the screen's dimensions.

1int x = random(EsploraTFT.width() - logo.width());
2
3 int y = random(EsploraTFT.height() - logo.height());

Draw the image onscreen starting at the random coordinates from the previous step, and wait for a little bit before entering the next

loop()

1EsploraTFT.image(logo, x, y);
2
3 delay(1500);
4}

The complete sketch is below :

1/*
2
3 Esplora TFT Bitmap Logos
4
5 This example for the Arduino TFT screen is for use
6
7 with an Arduino Esplora.
8
9 This example reads an image file from a micro-SD card
10
11 and draws it on the screen, at random locations.
12
13 There is a .bmp file included with this sketch.
14
15 - open the sketch folder (Ctrl-K or Cmd-K)
16
17 - copy the "arduino.bmp" file to a micro-SD
18
19 - put the SD into the SD slot of the Arduino LCD module.
20
21 This example code is in the public domain.
22
23 Created 19 April 2013 by Enrico Gueli
24
25 http://www.arduino.cc/en/Tutorial/EsploraTFTBitmapLogo
26
27 */
28
29// include the necessary libraries
30#include <Esplora.h>
31#include <SPI.h>
32#include <SD.h>
33#include <TFT.h> // Arduino LCD library
34
35// the Esplora pin connected to the chip select line for SD card
36#define SD_CS 8
37
38// this variable represents the image to be drawn on screen
39
40PImage logo;
41
42void setup() {
43
44 // initialize the GLCD and show a message
45
46 // asking the user to open the serial line
47
48 EsploraTFT.begin();
49
50 EsploraTFT.background(255, 255, 255);
51
52 EsploraTFT.stroke(0, 0, 255);
53
54 EsploraTFT.println();
55
56 EsploraTFT.println(F("Arduino LCD Bitmap Example"));
57
58 EsploraTFT.stroke(0, 0, 0);
59
60 EsploraTFT.println(F("Open serial monitor"));
61
62 EsploraTFT.println(F("to run the sketch"));
63
64 // initialize the serial port: it will be used to
65
66 // print some diagnostic info
67
68 Serial.begin(9600);
69
70 while (!Serial) {
71
72 // wait for serial port to connect. Needed for native USB port only
73
74 }
75
76 // try to access the SD card. If that fails (e.g.
77
78 // no card present), the Esplora's LED will turn red.
79
80 Serial.print(F("Initializing SD card..."));
81
82 if (!SD.begin(SD_CS)) {
83
84 Serial.println(F("failed!"));
85
86 Esplora.writeRed(255);
87
88 return;
89
90 }
91
92 Serial.println("OK!");
93
94 // clear the GLCD screen before starting
95
96 EsploraTFT.background(255, 255, 255);
97
98 // now that the SD card can be access, try to load the
99
100 // image file. The Esplora LED will turn green or red if
101
102 // the loading went OK or not.
103
104 Esplora.writeRGB(0, 0, 0);
105
106 logo = EsploraTFT.loadImage("arduino.bmp");
107
108 if (logo.isValid()) {
109
110 Esplora.writeGreen(255);
111
112 } else {
113
114 Esplora.writeRed(255);
115
116 }
117
118}
119
120void loop() {
121
122 // don't do anything if the image wasn't loaded correctly.
123
124 if (logo.isValid() == false) {
125
126 return;
127
128 }
129
130 Serial.println(F("drawing image"));
131
132 // get a random location where to draw the image.
133
134 // To avoid the image to be draw outside the screen,
135
136 // take into account the image size.
137
138 int x = random(EsploraTFT.width() - logo.width());
139
140 int y = random(EsploraTFT.height() - logo.height());
141
142 // draw the image to the screen
143
144 EsploraTFT.image(logo, x, y);
145
146 // wait a little bit before drawing again
147
148 delay(1500);
149}

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.