Hi, I have tried to improve a little my code adding a Processing Sketch that permits also to understand what's going on for users which does not have the LCD
The code for Arduino has little changes that permits to send a float through serial port using an union
/*
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;// declare an union to convert from float to an array of bytes
// to send them to the serial port
union FLOAT2BYTE {
** float myFloat;**
** byte byte_s[4];**
} S_FLOAT;// 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);
// send a byte to establish contact until receiver responds
**establishContact(); **
}// 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
sendFloat(horizontalAngle);
** sendFloat(verticalInclination);**// 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);
}void sendFloat(float floatToSend) {
** // we can send a float to serial port**
** S_FLOAT.myFloat = floatToSend;**
** for(int ii = 0; ii < 4; ii++) {**
** // we have to write bytes on serial port**
Serial.write(S_FLOAT.byte_s[ii]);
** }**
}
void establishContact() {
** // save the start time**
** unsigned long startTime = millis();**
** // until we have a response from the pc**
__ while (Serial.available() <= 0) {__
__ Serial.print('A'); // send a capital A__
** // if we are waiting from more than 10 seconds**
** if(millis() - startTime > 10000) {**
** // we will not comunicate with computer**
** break;**
** } **
** delay(300);**
** }**
}