Analog data plotting on 3.2 inch TFT using Arduino Mega

Hi all,

I am trying to plot two analog signal on a TFT screen. I am successfully able to display the numeric values but not able to plot them on graph.
I am weak in programming part and could not get much ahead. My code gives me a line for each input signal reading whereas I want to show all readings on a single screen.
Any help is appreciated.

#include <TFT_HX8357.h> // Hardware-specific library

TFT_HX8357 tft = TFT_HX8357();       // Invoke custom library

#define TFT_GREY 0x5AEB // New colour

float data;
int i=0;
int sensorArray[128];

void setup(void) {

void loop() {
  tft.setCursor(20, 50, 4);

  tft.drawLine(250, 15, 250, 304, TFT_BLUE);
  tft.drawLine(250, 159, 478, 159, TFT_BLUE);
  for (int i=250; i<470; i+=10)
    tft.drawLine(i, 157, i, 161, TFT_BLUE);
  for (int i=15; i<320; i+=10)
    tft.drawLine(248, i, 252, i, TFT_BLUE);
  data = analogRead(A0);
  int drawHeight = map(data,0,1023,0,32);
  sensorArray[0]= drawHeight;
  for (int i=250; i<470;i++)
    tft.drawPixel(i,32-sensorArray[i-1], TFT_BLUE);
  tft.setTextColor(TFT_BLUE);    tft.setTextFont(4);
  tft.print("Data1= "); tft.print(data*5.0/1023);tft.println(" dBm");
  tft.setCursor(20, 100, 4);
  tft.setTextColor(TFT_RED);    tft.setTextFont(4);
  tft.print("Data2= "); tft.print(data*5.0/1023);tft.print(" dBm");


Suppose you have an array of values that you want to plot on your display. For example: myData1[Dim1].
What you want to do is draw a line between all your values. You just need to make a for loop and draw each lines in this loop :

for (int i = 1; i<Dim1;i++) tft.drawLine(i-1, myData1[i-1], i, myData1[i], TFT_BLUE);

To add another curve, for say myData2[Dim2], just do the same:

for (int i = 1; i<Dim2;i++) tft.drawLine(i-1, myData2[i-1], i, myData2[i], TFT_RED);

This will draw the curves from the left side of your display. If you want to leave some space on the left side before drawing, just add that space :

byte Space = 30;
for (int i = 1; i<Dim1;i++) tft.drawLine(Space+i-1, myData1[i-1], Space+i, myData1[i], TFT_BLUE);

You can adapt this to draw the data values on arrival: in this case, each time a new data comes (analogRead), you just need to draw the line between the previous value and the current one. No need for the loop anymore.

And if there are more values than the width of the screen, you can shift the curve to the left on each new data arrival. In this case, you need to plot the curve in the for loop using the last N values of your array, N being the number of values you want to plot.

Hope this helps…