Examples > TFT
This sketch for the TFT screen with an Arduino Esplora reads the value of the onboard accelerometer, and draws a line across the screen that stays parallel to the horizon.
As you tilt the Esplora, the accelerometer reports its orientation to the center of the earth. Mapping these values allows you to keel the line aligned with the horizon.
Attach the TFT screen to the socket on your Esplora, with the label "SD Card" facing up.
To use the screen you must first include the SPI and TFT libraries. Don't forget to include the Esplora library as well.
Create variables for the start and end points of the line, as well as variables to hold the locations for the previous time through the
loop(). You'll use these values to erase lines when it changes position.
setup(), initialize the display and clear the screen's background.
loop(), read the value from the accelerometer's x-axis. This is the axis oriented to the horizon when the Esplora is held in front of your face, perpendicular to the earth. Map the values from the accelerometer so that the line's starting point is between 0 and the screen's height. To find the end point, invert the output values
Compare the current beginning and end points to the values from the previous time through
loop(). If they are different, erase the line drawn previously. Then, draw the line in its new location, based on the mapped values.
Save the current start and end values as the previous values to compare next time through
The complete sketch is below :