Decrementing a value using tft button

I'm using Arduino Mega and 3.5" TFT Display.
I want to increment and decrement a value by 10, using on-screen buttons.
But it misbehaves after reaching 100. Does somebody know why? I think the problem is in the decrement formula.

Code:

int volt = 0;
int diff = 10;

void loop(){

if (sub_btn.justPressed()) {
sub_btn.drawButton(true);
volt = volt - diff;
}

if (add_btn.justPressed()) {
add_btn.drawButton(true);
volt = volt + diff;
}
}

But it misbehaves after reaching 100.

I've been misbehaving all my life and I am no where near 100!!!

Please explain what 'misbehaving' means.

HAHA..

It increments from 0 to 255.
It decrements also properly upto 100.
But after further decrementing from 100 it becomes 900.

White decrementing for example: 130, 120, 110, 100, 900, 800, 700, 600, 500, 400, 300, 200, 100, 000

The code you posted is obviously not complete. Please post complete code and please remember to format it correctly and use code tags </> as per the forum instructions.

Thanks

This is the entire code.

#include "MCUFRIEND_kbv.h"
#include "TouchScreen.h"
#include <Adafruit_GFX.h>
MCUFRIEND_kbv tft;

#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4

#define MINPRESSURE 200
#define MAXPRESSURE 1000

const int XP = 8, XM = A2, YP = A3, YM = 9;
const int TS_LEFT = 128, TS_RT = 908, TS_TOP = 950, TS_BOT = 100;

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
TSPoint tp;
Adafruit_GFX_Button sub_btn, add_btn;

int pixel_x, pixel_y;
bool Touch_getXY()
{
  TSPoint p = ts.getPoint();
  pinMode(YP, OUTPUT);
  pinMode(XM, OUTPUT);
  digitalWrite(YP, HIGH);
  digitalWrite(XM, HIGH);
  bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
  if (pressed) {
    pixel_x = map(p.x, TS_LEFT, TS_RT, 0, tft.width());
    pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
  }
  return pressed;
}

#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

int motor = 45;
int volt = 0;
int diff = 10;
void setup() {

  Serial.begin(9600);
  uint16_t ID = tft.readID();
  tft.reset();
  tft.begin(0x9486);
  tft.setRotation(0);

  tft.setCursor(20, 10);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("PRECISION PRODUCTS");

  tft.setCursor(100, 40);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("PVT. LTD.");

  tft.setCursor(100, 80);
  tft.setTextColor(WHITE, RED);
  tft.setTextSize(3);
  tft.print("Oxygen");

  tft.setCursor(50, 120);
  tft.setTextColor(WHITE, RED);
  tft.setTextSize(3);
  tft.print("Concentrator");

  sub_btn.initButton(&tft,  60, 250, 100, 40, WHITE, CYAN, BLACK, "-", 3);
  add_btn.initButton(&tft, 252, 250, 100, 40, WHITE, CYAN, BLACK, "+", 3);
  sub_btn.drawButton(false);
  add_btn.drawButton(false);

  pinMode(motor, OUTPUT);

}

void loop() {
  bool down = Touch_getXY();
  sub_btn.press(down && sub_btn.contains(pixel_x, pixel_y));
  add_btn.press(down && add_btn.contains(pixel_x, pixel_y));

  if (sub_btn.justReleased())
    sub_btn.drawButton();
  if (add_btn.justReleased())
    add_btn.drawButton();
  if (sub_btn.justPressed()) {
    sub_btn.drawButton(true);
    volt = volt - diff;
  }
  if (add_btn.justPressed()) {
    add_btn.drawButton(true);
    volt = volt + diff;
  }

  tft.setCursor(120, 235);
  tft.setTextColor(TFT_RED, TFT_BLACK);
  tft.setTextSize(4);
  tft.print(volt);

}
int pressureValue = analogRead(46);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float pressure = pressureValue * (5.0 / 1023.0);

tft.setCursor(100, 400);
tft.setTextColor(TFT_RED, TFT_BLACK);
tft.setTextSize(4);
tft.print(pressure);


analogWrite(motor, volt);

}

OK, thanks.

I don't have the right experience to help, sorry, maybe someone else can.

bonyl:
It increments from 0 to 255.

If you do not want it it increment above 100 add an “if” statement…

if (volt < 100) volt = volt + diff;

…also similarly for decreasing.

It decrements also properly upto 100.
But after further decrementing from 100 it becomes 900.

It’s displaying 90 correctly. The extra “0” on the end is what was left over from displaying “100”.
Add…

tft.print(" ");

…to overwrite the last digit.

Posted in error...

It worked. Thanks for explaining.
Really appreciate it.