1 | /*
|
---|
2 | Arduino TFT text example
|
---|
3 |
|
---|
4 | This example demonstrates how to draw text on the
|
---|
5 | TFT with an Arduino. The Arduino reads the value
|
---|
6 | of an analog sensor attached to pin A0, and writes
|
---|
7 | the value to the LCD screen, updating every
|
---|
8 | quarter second.
|
---|
9 |
|
---|
10 | This example code is in the public domain
|
---|
11 |
|
---|
12 | Created 15 April 2013 by Scott Fitzgerald
|
---|
13 |
|
---|
14 | http://arduino.cc/en/Tutorial/TFTDisplayText
|
---|
15 |
|
---|
16 | */
|
---|
17 |
|
---|
18 | #include <TFT.h> // Arduino LCD library
|
---|
19 | #include <SPI.h>
|
---|
20 |
|
---|
21 | // pin definition for the Uno
|
---|
22 | #define cs 10
|
---|
23 | #define dc 9
|
---|
24 | #define rst 8
|
---|
25 |
|
---|
26 | // pin definition for the Leonardo
|
---|
27 | // #define cs 7
|
---|
28 | // #define dc 0
|
---|
29 | // #define rst 1
|
---|
30 |
|
---|
31 | // create an instance of the library
|
---|
32 | TFT TFTscreen = TFT(cs, dc, rst);
|
---|
33 |
|
---|
34 | // char array to print to the screen
|
---|
35 | char sensorPrintout[4];
|
---|
36 |
|
---|
37 | void setup() {
|
---|
38 |
|
---|
39 | // Put this line at the beginning of every sketch that uses the GLCD:
|
---|
40 | TFTscreen.begin();
|
---|
41 |
|
---|
42 | // clear the screen with a black background
|
---|
43 | TFTscreen.background(0, 0, 0);
|
---|
44 |
|
---|
45 | // write the static text to the screen
|
---|
46 | // set the font color to white
|
---|
47 | TFTscreen.stroke(255, 255, 255);
|
---|
48 | // set the font size
|
---|
49 | TFTscreen.setTextSize(2);
|
---|
50 | // write the text to the top left corner of the screen
|
---|
51 | TFTscreen.text("Sensor Value :\n ", 0, 0);
|
---|
52 | // ste the font size very large for the loop
|
---|
53 | TFTscreen.setTextSize(5);
|
---|
54 | }
|
---|
55 |
|
---|
56 | void loop() {
|
---|
57 |
|
---|
58 | // Read the value of the sensor on A0
|
---|
59 | String sensorVal = String(analogRead(A0));
|
---|
60 |
|
---|
61 | // convert the reading to a char array
|
---|
62 | sensorVal.toCharArray(sensorPrintout, 4);
|
---|
63 |
|
---|
64 | // set the font color
|
---|
65 | TFTscreen.stroke(255, 255, 255);
|
---|
66 | // print the sensor value
|
---|
67 | TFTscreen.text(sensorPrintout, 0, 20);
|
---|
68 | // wait for a moment
|
---|
69 | delay(250);
|
---|
70 | // erase the text you just wrote
|
---|
71 | TFTscreen.stroke(0, 0, 0);
|
---|
72 | TFTscreen.text(sensorPrintout, 0, 20);
|
---|
73 | }
|
---|
74 |
|
---|