Hi, I have tried to improve the Esplora TFT Horizon making it a little more sensitive to high inclination and adding also a line to represent vertical inclination. The angles are also printed to the serial port.
/*
Created 25 August 2013 by Corrado Possieri
*/// first of all we need to include the libraries
#include <Esplora.h>
#include <TFT.h> // Arduino LCD library
#include <SPI.h>// some variables that the Esplora need to work correctly
// we have to define the center of the screen
const float X0 = EsploraTFT.width()/2.0;
const float Y0 = EsploraTFT.height()/2.0;// and the limit values for the accelerometer
// good values not considering the acceleration impressed
// to Esplora are those (you can try also the values after comment)
int xAccMin = -180; // -150;
int xAccMax = 180; //170;
int yAccMin = -180; //-147;
int yAccMax = 180; //190;
int zAccMin = -180; //-150;
int zAccMax = 180; //170;// we need also to save in a global variable the last angle
float lastTanAngle;
float lastZIncl;// this function will be executed once at the beginning
void setup() {
// we need to initialize the display
EsploraTFT.begin();// and to select the color of the background
EsploraTFT.background(0,0,0); // black for an higher contrast// begin the serial comunication
Serial.begin(9600);
}// this will run over and over
void loop() {
// we need to read the Accelerometer values...
int xAxis = Esplora.readAccelerometer(X_AXIS);
int yAxis = Esplora.readAccelerometer(Y_AXIS);
int zAxis = Esplora.readAccelerometer(Z_AXIS);// ...and to remap them in a fixed range scale
const int scale = 1000;
float xIncl = map(xAxis, xAccMin, xAccMax, -scale, scale);
float yIncl = map(yAxis, yAccMin, yAccMax, -scale, scale);
float zIncl = map(zAxis, zAccMin, zAccMax, scale, -scale);// horizontal inclination
float horizontalAngle = atan2(xIncl, yIncl);
// vertical inclination
float verticalInclination = atan2(yIncl, -zIncl);// print these values to the serial port
Serial.print("horizontal angle: ");
Serial.println(horizontalAngle/PI180);
Serial.print("verical angle: ");
Serial.println(verticalInclination/PI180);
Serial.println();// no we can graph the values in the tft screen
// this value represent the offset of the line
// which represent the ground
float zOffset = map(zAxis, zAccMin, zAccMax, Y0, -Y0);
// this represent the inclination respect the horizon
float tanAngle = xIncl/yIncl;// first of all we have to delete the previous lines
// drawing black lines over them
// horizontal inclination
EsploraTFT.stroke(0,0,0); // we draw black over the previous lines
EsploraTFT.line(0, -X0lastTanAngle+Y0, 2X0, X0lastTanAngle+Y0);
// vertical inclination
EsploraTFT.line(0, -X0lastTanAngle+lastZIncl+Y0,
2X0, X0lastTanAngle+lastZIncl+Y0);// now we can write the new lines
// one for horizontal inclination
EsploraTFT.stroke(0,255,0); // this line would be green
EsploraTFT.line(0, -X0tanAngle+Y0, 2X0, X0tanAngle+Y0);
// this line would be red
// and one for vertical
EsploraTFT.stroke(255,0,0);
EsploraTFT.line(0, -X0tanAngle+zOffset+Y0, 2X0, X0tanAngle+zOffset+Y0);// we will save the values for the next loop iteration
lastTanAngle = tanAngle;
lastZIncl = zOffset;// a little delay for stability
delay(100);
}
I hope this will help.
Happy sketching