The TFTscreen.background command was removed completely and was 'simulated' by drawing two lines of different colours on the same column instead, as I stated on my previous comment.
The new code is shown below. It has been successfully uploaded into Uno R3. Drawing is still not as fast as I was expecting (about 5-6 samples/sec - 'measured' with my eye(!), with each sample 1px wide), but it is much faster than before and there is no flickering whatsoever! It now almost fulfils my requirements.
As it is indicated within a comment at the end of the code, it is possible to replace lines with points, but I noticed that during fast sensor fluctuations (i.e. fast voltage changes) the line appears annoyingly 'dotted'..so better draw solid lines to get better aesthetics.
I really do not know if it is possible to code this sketch in a manner that can result in a faster drawing responses, otherwise I will keep on updating this post and share my results here.
/*
This program reads the voltage applied on the sensor analogRead(A0)
and draws a graph on a TFT screen proportional to the sensor voltage
(0-5V). The graph propagates (scrolls) from left
to right as the sensor reading is updated.
The sketch was written and tested around a TFT display sized 160x128 px
(Arduino LDC module with an Arduino Uno R3).
It can however be scaled into different TFT displays with
the appropriate amendments within the code (not difficult to do!).
I noticed that the scrolling rate of the graph is not
fast, about 4-5 pixels per second.
So, for faster applications this sketch won't yield graphs with accurate
results (fast flactuations appearing at analogRead(A0)
won't be captured). But, the sketch can provide the basis
for further development and improvement around graph drawing.
*/
#include <TFT.h> // Arduino LCD library
#include <SPI.h>
// pin definition for the Uno
#define cs 10
#define dc 9
#define rst 8
TFT TFTscreen = TFT(cs, dc, rst);
//I define TFTscreen.width() as wTFT
int wTFT = TFTscreen.width(); // or wTFT=160 for my TFT screen
//I define TFTscreen.height() as hTFT
int hTFT = TFTscreen.height(); // or hTFT=120 for my TFT screen
int count;
int sensorArray[161];
void setup(){
// initialize the serial port
Serial.begin(9600);
// initialize the display
TFTscreen.begin();
// set TFT background colour to yellow
TFTscreen.background(255, 255, 0);
//initialize the array by zeroing all its elements
for (int count=0; count<=159; count++) //counts up from 0 to 159
{
sensorArray[count] = 0; //this 'zeros' all array elements(?)
}
}
void loop() {
//read the current value of analog sensor A0 and
//map it to TFT screen height (e.g.128px)
int drawHeight = map(analogRead(A0), 0, 1023, 0, hTFT );
//store this current sensor value within the array for future retrieval
//at the 1st element of the array (note: arrays are zero indexed)
sensorArray[0] = drawHeight; //this stores current drawHeight() value on the 1st element of array
//----routine to start the drawing of 160 vertical lines across TFT display----
//reminder: wTFT = 160
//'read' all sensor values from the array and draw them
//at the appropriate x-locations using the values stored in the array
//draw 160 lines!
for ( int count = 1; count <= wTFT; count++ ) //this counts up from 1 to 160
{
//set colour of lines to be drawn (red)
TFTscreen.stroke( 255, 0, 0 ); //draw red colour first
TFTscreen.line(count, hTFT - sensorArray[count-1], count, hTFT );
TFTscreen.stroke( 255, 255, 0 ); //draw yellow colour lines to simulate the erase process
TFTscreen.line(count, 1, count, hTFT-sensorArray[count-1] );
} //end of 'for' loop
//----routine for Shift Process within the array----
//within the array, shift elements to the right by one step, by copying the adjacent
//values found on the left of each element. Start the copying process
// from the right(last) element of the array!!! Leave the first [0] element intact!!!
for (count=wTFT; count>=2; count--) // count down from 160 to 2
{
sensorArray[count-1] = sensorArray[count-2]; //count=160, count=159 , ..., ..., count=3 , count=2
//159<-158 , 158<-157 , ..., ..., 2<-1 , 1<-0
//explanation: value of element 158 is written to element 159, and so on...
}
//'freeze' graph for a while for the viewer to enjoy!
//delay(100);
//erase(clear) ALL lines, (alternatively you can define a rect box
//which 'clears' the graph only, if other text has to remain on screen)
//TFTscreen.background( 255, 255, 0);
//other idea: draw a points instead of lines to accelerate drawing process???
}