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

EsploraTFTHorizon

Draw an artificial horizon line based on the tilt from the accelerometer.

Esplora TFT Horizon

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.

Hardware Required

  • Arduino Esplora

  • Arduino TFT screen

Circuit

Esplora GLCDHorizon

Attach the TFT screen to the socket on your Esplora, with the label "SD Card" facing up.

Code

To use the screen you must first include the SPI and TFT libraries. Don't forget to include the Esplora library as well.

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

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.

1int yStart = EsploraTFT.height()/2;
2int yEnd = EsploraTFT.height()/2;
3
4int oldEndY;
5int oldStartY;

In

setup()
, initialize the display and clear the screen's background.

1void setup() {
2
3 EsploraTFT.begin();
4
5 EsploraTFT.background(0,0,0);
6}

In

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

1void loop()
2{
3
4 int tilt = Esplora.readAccelerometer(X_AXIS);
5
6 yStart = map(tilt,-100,100,EsploraTFT.height(),0);
7
8 yEnd = map(tilt,-100,100,0,EsploraTFT.height());

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.

1if (oldStartY != yStart || oldEndY != yEnd) {
2
3 EsploraTFT.stroke(0,0,0);
4
5 EsploraTFT.line(0, oldStartY, EsploraTFT.width(), oldEndY);
6
7 }
8
9 EsploraTFT.stroke(255,0,255);
10
11 EsploraTFT.line(0,yStart,EsploraTFT.width(),yEnd);

Save the current start and end values as the previous values to compare next time through

loop()
.

1oldStartY= yStart;
2
3 oldEndY = yEnd;
4
5 delay(10);
6}

The complete sketch is below :

1/*
2
3 Esplora TFT Horizon
4
5 This example for the Arduino TFT and Esplora draws
6
7 a line on the screen that stays level with the ground
8
9 as you tile the Esplora side to side
10
11 This example code is in the public domain.
12
13 Created 15 April 2013 by Scott Fitzgerald
14
15 http://www.arduino.cc/en/Tutorial/EsploraTFTHorizon
16
17 */
18
19#include <Esplora.h>
20#include <TFT.h> // Arduino LCD library
21#include <SPI.h>
22
23// horizontal start and end positions
24int yStart = EsploraTFT.height() / 2;
25int yEnd = EsploraTFT.height() / 2;
26
27// previous start and end positions
28int oldEndY;
29int oldStartY;
30
31void setup() {
32
33 // initialize the display
34
35 EsploraTFT.begin();
36
37 // make the background black
38
39 EsploraTFT.background(0, 0, 0);
40}
41
42void loop() {
43
44 // read the x-axis of the accelerometer
45
46 int tilt = Esplora.readAccelerometer(X_AXIS);
47
48 // the values are 100 when tilted to the left
49
50 // and -100 when tilted to the right
51
52 // map these values to the start and end points
53
54 yStart = map(tilt, -100, 100, EsploraTFT.height(), 0);
55
56 yEnd = map(tilt, -100, 100, 0, EsploraTFT.height());
57
58 // if the previous values are different than the current values
59
60 // erase the previous line
61
62 if (oldStartY != yStart || oldEndY != yEnd) {
63
64 EsploraTFT.stroke(0, 0, 0);
65
66 EsploraTFT.line(0, oldStartY, EsploraTFT.width(), oldEndY);
67
68 }
69
70 // draw the line in magenta
71
72 EsploraTFT.stroke(255, 0, 255);
73
74 EsploraTFT.line(0, yStart, EsploraTFT.width(), yEnd);
75
76 // save the current start and end points
77
78 // to compare int he next loop
79
80 oldStartY = yStart;
81
82 oldEndY = yEnd;
83
84 delay(10);
85}

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.