Can someone help find the problem.

Hi there,

I am trying to build a tilt switch using the MMA8452Q accelerometer and the Arduino TFT screen (https://www.arduino.cc/en/Guide/TFTtoBoards). I'm experiencing some problems with my setup as it will work normally for a period of time, but then suddenly freeze up. After resetting the Arduino Leonardo board, it will start to function as per normal again until the next freeze up. The functional period is irregular.

Can someone please put me in the right direction in diagnosing the problem? Thanks in advance.

I've attached the code here:

#include <Wire.h> // Must include Wire library for I2C
#include <SFE_MMA8452Q.h> // Includes the SFE_MMA8452Q library
#include <TFT.h>
#include <SPI.h>
#include <math.h>

MMA8452Q angle;

// pin definition for Leonardo
#define cs    7
#define dc    0
#define rst   1

// create instance of the library
TFT LCDscreen = TFT(cs, dc, rst);

// define values for int to char conversion
char PrintoutX[5];

// define calculated angle in degrees
float CalcAngleDeg;
float PrevAngle;

void setup() {

  LCDscreen.begin(); //initialize LCD screen
  LCDscreen.background(0, 0, 0); //set background colour
  LCDscreen.setTextSize (5); //set font size
  
  angle.init(SCALE_2G, ODR_50); //Initialised the accelerometer to have a scale of +/- 2G with DataRate of 50Hz

  //Initiate Previous Angle to be zero.
  //Initiate Calculated Angle to be zero.
  PrevAngle = 0;
  CalcAngleDeg = 0;
}

void loop() {

float test;
  
  digitalWrite(12, LOW);
  
  //Use the accel.available() function to wait for new data from the accelerometer
  if (angle.available())
  {
    digitalWrite(13, HIGH);
    angle.read(); //Use accel.read() to read the new variables:

    ConvertAngle(); // convert angle.y and angle.z into angles in degrees

   // Only update the LCD if there is an angle change greater than 1 degree
   test = PrevAngle - CalcAngleDeg; 
   if (test <= -1 | test >= 1)
   {
      LCDLoop();
   }
    
  }

}

void ConvertAngle()
{
  PrevAngle = CalcAngleDeg;
  float test_y = angle.y; //Convert read in angle.y to a floating point 
  float test_z = angle.z; //Convert read in angle.z to a floating point
  float theta = ((test_y) / (test_z)); 
  float CalcAngleRad = atan(theta); //finding the angle using trignometry inverse tan
  CalcAngleDeg = ((CalcAngleRad * 180) / M_PI); //by default the output value is in radiens, this step converts it to degrees
}

void LCDLoop()
{
      CleanLCD();
      CharConvert(); // convert int to char for LCD
      PrintLCD(); // print the calculated value onto LCD
}

void CharConvert()
{
  String AngleString = String(CalcAngleDeg);
  AngleString.toCharArray(PrintoutX, 5);
}

void PrintLCD()
{
  LCDscreen.stroke(255, 255, 255); //Set Font colour to white
  LCDscreen.text(PrintoutX, 0, 20);
}

void CleanLCD()
{
  LCDscreen.stroke(0, 0, 0); //Set Font colour to black
  LCDscreen.text(PrintoutX, 0, 20); //reprint the text in black to cover the wording again.
}

The pleasures of String (with capital S) in CharConvert()? Try to use sprintf / snprintf instead and see if it solves the issue.

  snprintf(PrintoutX, sizeof(PrintoutX), "%f", CalcAngleDeg);

I'm not 100% sure about the format specifier; see http://www.cplusplus.com/reference/cstdio/printf/.

Also, pay attention to the size; the above will truncate to 4 characters (e.g. 3.14 or 2345).

The LCDLoop() function doesn't loop. A better name is in order!