TFT Plotting

Hi,

I use this code to plot readings on a TFT Display (480x800). It works great, but I have a problem. When it plot the first reading, the drawLine starts from y 0 (x 51, y 0) on display, and then it plots the next readings within the screensize I set.

Anybody knows how to fix this?

Thanks,

The code:

void loop() {

{

int a1, a2, b1, b2, c1, c2;

int color1 = c1; // GREEN
int color2 = c2; // YELLOW
int color3 = c3; // RED
int color4 = c4; // BLUE
int color5 = c5; // CYAN
int color6 = c6; // PURPLE

for (int x = 51; x < 800; x++) {

a2 = map(analogRead(A0), 0, 1023, 149, 40); // map max analog read (1023) to max screensize (240)
b2 = map(analogRead(A1), 0, 1023, 299, 190); // map max analog read (1023) to max screensize (240)
c2 = map(analogRead(A2), 0, 1023, 449, 340); // map max analog read (1023) to max screensize (240)

tft.drawLine(x, a1, x + 1, a2, color6); // draw line between previous reading and current
tft.drawLine(x, b1, x + 1, b2, color4); // draw line between previous reading and current
tft.drawLine(x, c1, x + 1, c2, color3); // draw line between previous reading and current

a1 = a2; // Make current reading previous
b1 = b2;
c1 = c2;

}
}
}

tft.drawLine(x, a1, x + 1, a2, color6); // draw line between previous reading and current

Yes, that’s exactly what you told it to do. Start at x (which is 51) and a1 (which you never gave a value and it just happened to end up with a 0). Try giving a1 the initial value you think it should have. The compiler isn’t going to guess it.

You should also note that since your a1, a2, b1, etc variables are defined INSIDE the loop function and are not declared to be static, they will be destroyed every time loop exits and when it starts again they’ll reset to whatever values you give them at the top of loop. If you want those variables to maintain their values between iterations of the loop function, then they need to either be static or global.

Great, it works.

Thanks for the advice.