Odd Variable Values

Hi All,

Arduino newbie of 1 week so please don’t bite! I am running a program on a mega that will display the value of 2 potentiometers on a 2.4" TFT. I know I’ve got the pots wired up correctly one is into A11 the other into A12. The TFT is plugged directly to the mega. I’m running 5v out and grounding via the digital I/O at the rear of the board. I have my set up running an opening screen with a message in various fonts and sizes, this works perfectly.

In the loop I call for the screen to print the pot values in certain defined areas of the screen, it does this perfectly. It starts at 0 and scrolls diligently up to 1023 on the clockwise turn. All good! It’s when I turn the pot anti clockwise it decides to countdown back to 1000 ok but then for some reason when it reaches 1000 it flips and counts down from 9999 back down to zero. I’m fairly sure this is my programming as on serial print it goes from 0-1023 and back down perfectly. Necron is just a custom colour I defined!

void loop() {

Serial.println(analogRead(A11));
tft.setFont();
tft.setCursor(240,30);
tft.setTextColor(NECRON,BLACK);
tft.setTextSize(3);
tft.print(analogRead(A11));
tft.setCursor(240,110);
tft.setTextColor(NECRON, BLACK);
tft.setTextSize(3);
tft.print(analogRead(A12));

Go easy on me I’m new! I don’t think I’m doing too bad for 1 week old! Cheers all, Kev

Does it really go to 9999 or does it go to 1999?

It starts at 0 on clockwise turn it goes up to 1023, all good. On the anticlockwise turn it starts from the 1023 it reached previously but when it hits 1000 it goes over to 9999. I know that the pot is ok because on serial print it goes from 0-1023 then back to 0 perfectly! It's confusing!

Oops, yes 9999 would be expected - sorry brain-fart.

Your print values are being left-justified.

Read the value from analogRead into a variable.
If the variable is less than 1000, print a space, then the variable.

Repeat for lower powers of ten.

Please remember to use code tags when posting code

Hi, thanks for the reply, excuse my ignorance but how would I express this on my print line? It's been frying my brain for 8 hours and I can't think any more! I learn quicker by messing with things but this one has me stumped. Apologies for not posting code correctly, didn't know how! Kev

int potval =analogRead (potPin);
if (potval < 1000) {
  tft.print (' ');
}
tft.print(potval);

Repeat for smaller powers of ten

Hi, it works perfectly just by doing the <1000 it seems. Many thanks, I may be back tomorrow if I can’t figure how to express it as a percentage now! I’ll get there! Kev