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
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()1int yStart = EsploraTFT.height()/2;2int yEnd = EsploraTFT.height()/2;3
4int oldEndY;5int oldStartY;In
setup()1void setup() {2
3  EsploraTFT.begin();4
5  EsploraTFT.background(0,0,0);6}In
loop()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()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 Horizon4
5 This example for the Arduino TFT and Esplora draws6
7 a line on the screen that stays level with the ground8
9 as you tile the Esplora side to side10
11 This example code is in the public domain.12
13 Created 15 April 2013 by Scott Fitzgerald14
15 http://www.arduino.cc/en/Tutorial/EsploraTFTHorizon16
17 */18
19#include <Esplora.h>20#include <TFT.h>            // Arduino LCD library21#include <SPI.h>22
23// horizontal start and end positions24int yStart = EsploraTFT.height() / 2;25int yEnd = EsploraTFT.height() / 2;26
27// previous start and end positions28int oldEndY;29int oldStartY;30
31void setup() {32
33  // initialize the display34
35  EsploraTFT.begin();36
37  // make the background black38
39  EsploraTFT.background(0, 0, 0);40}41
42void loop() {43
44  // read the x-axis of the accelerometer45
46  int tilt = Esplora.readAccelerometer(X_AXIS);47
48  // the values are 100 when tilted to the left49
50  // and  -100 when tilted to the right51
52  // map these values to the start and end points53
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 values59
60  // erase the previous line61
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 magenta71
72  EsploraTFT.stroke(255, 0, 255);73
74  EsploraTFT.line(0, yStart, EsploraTFT.width(), yEnd);75
76  // save the current start and end points77
78  // to compare int he next loop79
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.
