Draw bargraph on TFT Shield to inicate analouge voltage

Hi,

I am new to the Arduino so please forgive me if this is easy to do...

I would like to indicate the level of my water tank on a N10DP TFT Shield using a bargraph. The voltage output from my water level sensor is 0 to 3.5V (max). I have got as far as laying out my screen with the graphic I wish to show the water level in as follows...

/* draw text's APP
drawChar(INT8U ascii,INT16U poX, INT16U poY,INT16U size, INT16U fgcolor);
drawString(char *string,INT16U poX, INT16U poY,INT16U size,INT16U fgcolor);
*/

#include <stdint.h>
#include <TFTv2.h>
#include <SPI.h>

void setup()
{
TFT_BL_ON; // turn on the background light
Tft.TFTinit(); // init TFT library

Tft.drawString("Water Tank Level",25,5,2,WHITE); // draw string: "Water Tank Level", (21, 5), size: 2, color: WHITE

Tft.drawLine(20,25,220,25,YELLOW); //start: (20, 25) end: (220, 25), color : YELLOW

Tft.drawLine(20,26,220,26,YELLOW);

Tft.drawString("NOTE: Pump will not operate at tank",10,35,1,GREEN);

Tft.drawString("level less than 5% total fill !",18,45,1,GREEN);

Tft.drawString("PUMP OUTPUT RELAY",13,65,2,RED);

Tft.drawString("IS",65,90,2,RED);

Tft.drawLine(60,120,60,300,CYAN);

Tft.drawLine(180,120,180,300,CYAN);

Tft.drawLine(60,300,180,300,CYAN);

Tft.drawLine(50,300,60,300,WHITE);

Tft.drawString("0 %",25,295,1,WHITE);

Tft.drawLine(50,282,60,282,WHITE);

Tft.drawString("10 %",18,277,1,WHITE);

Tft.drawLine(50,264,60,264,WHITE);

Tft.drawString("20 %",18,259,1,WHITE);

Tft.drawLine(50,246,60,246,WHITE);

Tft.drawString("30 %",18,240,1,WHITE);

Tft.drawLine(50,228,60,228,WHITE);

Tft.drawString("40 %",18,222,1,WHITE);

Tft.drawLine(50,210,60,210,WHITE);

Tft.drawString("50 %",18,205,1,WHITE);

Tft.drawLine(50,192,60,192,WHITE);

Tft.drawString("60 %",18,187,1,WHITE);

Tft.drawLine(50,174,60,174,WHITE);

Tft.drawString("70 %",18,169,1,WHITE);

Tft.drawLine(50,156,60,156,WHITE);

Tft.drawString("80 %",18,150,1,WHITE);

Tft.drawLine(50,138,60,138,WHITE);

Tft.drawString("90 %",18,133,1,WHITE);

Tft.drawLine(50,120,60,120,WHITE);

Tft.drawString("100 %",13,115,1,WHITE);

Tft.drawLine(180,120,190,120,WHITE);

Tft.drawString("FULL",195,115,1,WHITE);

Tft.drawLine(180,210,190,210,WHITE);

Tft.drawString("HALF",195,205,1,WHITE);

Tft.drawLine(180,300,190,300,WHITE);

Tft.drawString("EMPTY",195,295,1,WHITE);

}

void loop()
{

}

Now I need to add the code to read in an analogue voltage (just one - 0 to 3.5V max) and then put this into a variable, which I then use to draw filled boxes - according to the variable level (large value = a bigger box = a bigger bar graph).

Any help would be much appreciated and help me on my way to learning...

Cheers

Dan

Now I need to add the code to read in an analogue voltage (just one - 0 to 3.5V max) and then put this into a variable

int voltIndicator = analogRead(someAnalogPin);

Then, you need to convert the analogRead() value, in the range 0 to 1023, to voltage. How depends on exactly which Arduino you have (specifically, its nominal voltage and how it's powered).

i just draw two rectangles and change the height, opposed. On has the main color, and the other the background ( so you have one going up and one going down).

Its quite a headache, if you can avoid it, please do so :wink:

This is how it looks:

Here is a simple way to convert the analog range (0 - 1023) to 0 - 5 Volts

float voltage = float(analogRead(A0)) * 0.00488759;

For your bargraph, I would use two fill_Rectangle functions one to match the screens background color and the other, to whatever color you want.

(Empty)============|----- -> 70% filled

You will also need to use either two FOR loops(blocking) or some IF statements (non-blocking) to draw the rectangles.

You will also need to use either two FOR loops(blocking) or some IF statements (non-blocking) to draw the rectangles.

How is a for loop going to blocking, while an if statement isn't, if they produce the same results?

They are not actually needed, but they do make it look smoother when drawn. Also if coded correctly, it wont need to completely redraw the entire rectangle, just the difference from when it moved last.