Changing number displays on TFT (Canteen Ordering)

I've been working on a canteen ordering through Arduino and TFT Touch Screen module. One of the features that I plan to achieve is having the balance (as seen at the feature) decrease as a user press a button, which denotes a dish and it's price (which will be deducted to the balance). How can I achieve it? Is it possible to do it with "math.h" library?

Here is the interface of my TFT:

While here is the code:

#include <Adafruit_TFTLCD.h>
#include <TouchScreen.h>
#include <Adafruit_GFX.h>
#include <math.h>

// define the pins used to communicate with the display
#define LCD_CS   A3
#define LCD_CD   A2
#define LCD_WR   A1
#define LCD_RD   A0
#define LCD_RESET   A4

#define TS_MINX 122
#define TS_MINY 111
#define TS_MAXX 942
#define TS_MAXY 890

#define YP A3
#define XM A2
#define YM 9
#define XP 8

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

// create an instance of the TFT display object
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 364);

boolean buttonEnabled = true;

void setup() {
  Serial.begin(9600);
  tft.reset();
  uint16_t identifier = tft.readID();
  tft.begin(identifier);
  tft.setRotation(1);
  tft.fillScreen(BLUE);

  tft.setCursor(205,30);
  tft.setTextColor(YELLOW);
  tft.setTextSize(3);
  tft.print("MENU");

  tft.setCursor(20,40);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("PHP 500.00");

  tft.setCursor(20,30);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("YOUR BALANCE: ");

  // dishOne
  tft.fillRect(20,80, 80, 40, MAGENTA);
  tft.drawRect(20,80,80,40,BLACK);
  tft.setCursor(30,90);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("ADOBO");
  tft.setCursor(30,100);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("PHP 50.00");

  // dishTwo
  tft.fillRect(110,80, 80, 40, MAGENTA);
  tft.drawRect(110,80,80,40,BLACK);
  tft.setCursor(120,90);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("SINIGANG");
  tft.setCursor(120,100);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("PHP 45.00");

  // dishThree
  tft.fillRect(200,80, 80, 40, MAGENTA);
  tft.drawRect(200,80,80,40,BLACK);
  tft.setCursor(210,90);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("TULINGAN");
  tft.setCursor(210,100);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("PHP 55.00");

  // dishSeven
  tft.fillRect(20,180, 80, 40, MAGENTA);
  tft.drawRect(20,180,80,40,BLACK);
  tft.setCursor(30,190);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("SIOMAI");
  tft.setCursor(30,200);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("PHP 20.00");

  // dishEight
  tft.fillRect(110,180, 80, 40, MAGENTA);
  tft.drawRect(110,180,80,40,BLACK);
  tft.setCursor(120,190);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("MENUDO");
  tft.setCursor(120,200);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("PHP 50.00");

  // dishNine
  tft.fillRect(200,180, 80, 40, MAGENTA);
  tft.drawRect(200,180,80,40,BLACK);
  tft.setCursor(210,190);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("TINOLA");
  tft.setCursor(210,200);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("PHP 50.00");

  // dishFour
  tft.fillRect(20,130, 80, 40, MAGENTA);
  tft.drawRect(20,130,80,40,BLACK);
  tft.setCursor(30,140);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("CHICKEN");
  tft.setCursor(30,150);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("PHP 45.00");

  // dishFive
  tft.fillRect(110,130, 80, 40, MAGENTA);
  tft.drawRect(110,130,80,40,BLACK);
  tft.setCursor(120,140);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("AFRITADA");
  tft.setCursor(120,150);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("PHP 55.00");

  // dishSix
  tft.fillRect(200,130, 80, 40, MAGENTA);
  tft.drawRect(200,130,80,40,BLACK);
  tft.setCursor(210,140);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("KARE-KARE");
  tft.setCursor(210,150);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("PHP 55.00");
}

void loop() 
  {
  TSPoint p = ts.getPoint();
  
  if (p.z > ts.pressureThreshhold)

  {  
   p.x = map(p.x, TS_MAXX, TS_MINX, 0, 320);
   p.y = map(p.y, TS_MAXY, TS_MINY, 0, 480);
       
   if(p.x > 20 && p.x < 60 && p.y > 80 && p.y < 160 && buttonEnabled)
   {    
    buttonEnabled = false;
    
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);

    dishNine(); // draw the ninth dish 
   }

   if(p.x > 110 && p.x < 150 && p.y > 80 && p.y < 160 && buttonEnabled)
   {    
    buttonEnabled = false;
    
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);

    dishSix(); // draw the sixth dish 
   }

    if(p.x > 200 && p.x < 240 && p.y > 80 && p.y < 160 && buttonEnabled)
   {    
    buttonEnabled = false;
    
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);

    dishThree(); // draw the third dish 
   }

   if(p.x > 20 && p.x < 60 && p.y > 180 && p.y < 280 && buttonEnabled)
   {    
    buttonEnabled = false;
    
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);

    dishEight(); // draw the eight dish 
   }

   if(p.x > 110 && p.x < 150 && p.y > 180 && p.y < 280 && buttonEnabled)
   {    
    buttonEnabled = false;
    
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);

    dishFive(); // draw the fifth dish 
   }

   if(p.x > 200 && p.x < 240 && p.y > 180 && p.y < 280 && buttonEnabled)
   {    
    buttonEnabled = false;
    
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);

    dishTwo(); // draw the second dish 
   }

   if(p.x > 20 && p.x < 60 && p.y > 300 && p.y < 460 && buttonEnabled)
   {    
    buttonEnabled = false;
    
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);

    dishSeven(); // draw the seventh dish 
   }

   if(p.x > 110 && p.x < 150 && p.y > 300 && p.y < 460 && buttonEnabled)
   {    
    buttonEnabled = false;
    
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);

    dishFour(); // draw the fourth dish 
   }

   if(p.x > 200 && p.x < 240 && p.y > 300 && p.y < 460 && buttonEnabled)
   {    
    buttonEnabled = false;
    
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);

    dishOne(); // draw the first dish 
   }
  }
}

void landingPage() {
  tft.fillScreen(BLUE);
    
  tft.setCursor(40,30);
  tft.setTextColor(YELLOW);
  tft.setTextSize(2);
  tft.print("Your order has been");
    
  tft.setCursor(100,50);
  tft.setTextColor(YELLOW);
  tft.setTextSize(2);
  tft.print("received.");    

  tft.setCursor(20,100);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("If you made a mistake,");
    
  tft.setCursor(50,120);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("please inform us");    
    
  tft.setCursor(80,140);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("immediately.");

  tft.setCursor(60,200);
  tft.setTextColor(YELLOW);
  tft.setTextSize(1);
  tft.print("(C) 2023 CALAMBA CITY SCIENCE");  

  tft.setCursor(90,210);
  tft.setTextColor(YELLOW);
  tft.setTextSize(1);
  tft.print("INTEGRATED SCHOOL"); 
}

void dishNine() {
  Serial.print("TINOLA  ");
  Serial.print("\t");
  delay(1000); 
  Serial.println("Php 50.00");
  
  landingPage();
}

void dishSix() {
  Serial.print("KARE-KARE");
  Serial.print("\t");
  delay(1000);
  Serial.println("Php 55.00");
  
  landingPage(); 
}

void dishThree() {
  Serial.print("TULINGAN");
  Serial.print("\t");
  delay(1000);
  Serial.println("Php 55.00");
  
  landingPage(); 
}

void dishEight() {
  Serial.print("MENUDO  ");
  Serial.print("\t");
  delay(1000);
  Serial.println("Php 50.00");
  
  landingPage(); 
}

void dishFive() {
  Serial.print("AFRITADA");
  Serial.print("\t");
  delay(1000);
  Serial.println("Php 55.00");
  
  landingPage(); 
}

void dishTwo() {
  Serial.print("SINIGANG");
  Serial.print("\t");
  delay(1000);
  Serial.println("Php 45.00");
  
  landingPage();
}

void dishSeven() {
  Serial.print("SIOMAI  ");
  Serial.print("\t");
  delay(1000);
  Serial.println("Php 20.00");
  
  landingPage();
}

void dishFour() {
  Serial.print("CHICKEN  ");
  Serial.print("\t");
  delay(1000);
  Serial.println("Php 45.00");
  
  landingPage();
}

void dishOne() {
  Serial.print("ADOBO  ");
  Serial.print("\t");
  delay(1000);
  Serial.println("Php 50.00");
  
  landingPage();
}

Note that I haven't utilize the <math.h> function as of now. Hoping for your immediate replies and suggestions. Thank you.

That looks like where you could place a variable to display a balance instead of a constant 500.00.

You don't need any library to add numbers up.

Add new costs to a variable called balance or whatever. Subtract the cost of any cancelled item.

a7

Can you give an example using the code? Thank you in advance.

You need to add a balance variable before void setup().

int balance = 0;

Then, for every button pressed (for example ADOBO: 50.00) subtract that amount from BALANCE

void dishOne() {
  Serial.print("ADOBO  ");
  Serial.print("\t");
  delay(1000);
  Serial.println("Php 50.00"); // cost
  balance = balance - 50; // subtract the cost from the balance
  landingPage();
}

And then you need to update balance:

  tft.setCursor(20,40);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print(balance);
  // tft.print("PHP 500.00"); // do not hard-code the balance

Gee, since it would be so easy, why not start the user off with a positive balance?

int balance = 32767;

Which makes me realize that the balance variable might need to be a long integer or even a floating point number, viz:

float balance = 10000.0;

a7

Whoops. Sorry.

That is a nice project!

Is it just to learn programming or shall it be used in a real environment?

I applied it to my code, and somehow it didn't deduct to the positive balance, what could be the problem?

See post #6 @alto777 ... you need:

  1. Create a starting balance.
  2. Deduct price of choice from balance
  3. Display balance.

Show each step by showing your full sketch.

So far I did all of the steps, but the balance still didn't subtract on the TFT screen

#include <Adafruit_TFTLCD.h>
#include <TouchScreen.h>
#include <Adafruit_GFX.h>

// define the pins used to communicate with the display
#define LCD_CS   A3
#define LCD_CD   A2
#define LCD_WR   A1
#define LCD_RD   A0
#define LCD_RESET   A4

#define TS_MINX 122
#define TS_MINY 111
#define TS_MAXX 942
#define TS_MAXY 890

#define YP A3
#define XM A2
#define YM 9
#define XP 8

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

// create an instance of the TFT display object
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 364);

boolean buttonEnabled = true;

float balance = 10000.0;

void setup() {
  Serial.begin(9600);
  tft.reset();
  uint16_t identifier = tft.readID();
  tft.begin(identifier);
  tft.setRotation(1);
  tft.fillScreen(BLUE);

  tft.setCursor(205,30);
  tft.setTextColor(YELLOW);
  tft.setTextSize(3);
  tft.print("MENU");

  tft.setCursor(20,40);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print(balance);
  // tft.print("PHP 500.00");
  // do not hard-code the balance

  tft.setCursor(20,30);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("YOUR BALANCE: ");

  // dishOne
  tft.fillRect(20,80, 80, 40, MAGENTA);
  tft.drawRect(20,80,80,40,BLACK);
  tft.setCursor(30,90);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("ADOBO");
  tft.setCursor(30,100);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("PHP 50.00");

  // dishTwo
  tft.fillRect(110,80, 80, 40, MAGENTA);
  tft.drawRect(110,80,80,40,BLACK);
  tft.setCursor(120,90);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("SINIGANG");
  tft.setCursor(120,100);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("PHP 45.00");

  // dishThree
  tft.fillRect(200,80, 80, 40, MAGENTA);
  tft.drawRect(200,80,80,40,BLACK);
  tft.setCursor(210,90);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("TULINGAN");
  tft.setCursor(210,100);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("PHP 55.00");

  // dishSeven
  tft.fillRect(20,180, 80, 40, MAGENTA);
  tft.drawRect(20,180,80,40,BLACK);
  tft.setCursor(30,190);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("SIOMAI");
  tft.setCursor(30,200);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("PHP 20.00");

  // dishEight
  tft.fillRect(110,180, 80, 40, MAGENTA);
  tft.drawRect(110,180,80,40,BLACK);
  tft.setCursor(120,190);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("MENUDO");
  tft.setCursor(120,200);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("PHP 50.00");

  // dishNine
  tft.fillRect(200,180, 80, 40, MAGENTA);
  tft.drawRect(200,180,80,40,BLACK);
  tft.setCursor(210,190);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("TINOLA");
  tft.setCursor(210,200);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("PHP 50.00");

  // dishFour
  tft.fillRect(20,130, 80, 40, MAGENTA);
  tft.drawRect(20,130,80,40,BLACK);
  tft.setCursor(30,140);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("CHICKEN");
  tft.setCursor(30,150);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("PHP 45.00");

  // dishFive
  tft.fillRect(110,130, 80, 40, MAGENTA);
  tft.drawRect(110,130,80,40,BLACK);
  tft.setCursor(120,140);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("AFRITADA");
  tft.setCursor(120,150);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("PHP 55.00");

  // dishSix
  tft.fillRect(200,130, 80, 40, MAGENTA);
  tft.drawRect(200,130,80,40,BLACK);
  tft.setCursor(210,140);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("KARE-KARE");
  tft.setCursor(210,150);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("PHP 55.00");
}

void loop() 
  {
  TSPoint p = ts.getPoint();
  
  if (p.z > ts.pressureThreshhold)

  {  
   p.x = map(p.x, TS_MAXX, TS_MINX, 0, 320);
   p.y = map(p.y, TS_MAXY, TS_MINY, 0, 480);
       
   if(p.x > 20 && p.x < 60 && p.y > 80 && p.y < 160 && buttonEnabled)
   {    
    buttonEnabled = false;
    
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);

    dishNine(); // draw the ninth dish 
   }

   if(p.x > 110 && p.x < 150 && p.y > 80 && p.y < 160 && buttonEnabled)
   {    
    buttonEnabled = false;
    
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);

    dishSix(); // draw the sixth dish 
   }

    if(p.x > 200 && p.x < 240 && p.y > 80 && p.y < 160 && buttonEnabled)
   {    
    buttonEnabled = false;
    
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);

    dishThree(); // draw the third dish 
   }

   if(p.x > 20 && p.x < 60 && p.y > 180 && p.y < 280 && buttonEnabled)
   {    
    buttonEnabled = false;
    
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);

    dishEight(); // draw the eight dish 
   }

   if(p.x > 110 && p.x < 150 && p.y > 180 && p.y < 280 && buttonEnabled)
   {    
    buttonEnabled = false;
    
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);

    dishFive(); // draw the fifth dish 
   }

   if(p.x > 200 && p.x < 240 && p.y > 180 && p.y < 280 && buttonEnabled)
   {    
    buttonEnabled = false;
    
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);

    dishTwo(); // draw the second dish 
   }

   if(p.x > 20 && p.x < 60 && p.y > 300 && p.y < 460 && buttonEnabled)
   {    
    buttonEnabled = false;
    
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);

    dishSeven(); // draw the seventh dish 
   }

   if(p.x > 110 && p.x < 150 && p.y > 300 && p.y < 460 && buttonEnabled)
   {    
    buttonEnabled = false;
    
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);

    dishFour(); // draw the fourth dish 
   }

   if(p.x > 200 && p.x < 240 && p.y > 300 && p.y < 460 && buttonEnabled)
   {    
    buttonEnabled = false;
    
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);

    dishOne(); // draw the first dish 
   }
  }
}

void landingPage() {
  tft.fillScreen(BLUE);
    
  tft.setCursor(40,30);
  tft.setTextColor(YELLOW);
  tft.setTextSize(2);
  tft.print("Your order has been");
    
  tft.setCursor(100,50);
  tft.setTextColor(YELLOW);
  tft.setTextSize(2);
  tft.print("received.");    

  tft.setCursor(20,100);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("If you made a mistake,");
    
  tft.setCursor(50,120);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("please inform us");    
    
  tft.setCursor(80,140);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("immediately.");

  tft.setCursor(60,200);
  tft.setTextColor(YELLOW);
  tft.setTextSize(1);
  tft.print("(C) 2023 CALAMBA CITY SCIENCE");  

  tft.setCursor(90,210);
  tft.setTextColor(YELLOW);
  tft.setTextSize(1);
  tft.print("INTEGRATED SCHOOL"); 
}

void dishNine() {
  Serial.print("TINOLA  ");
  Serial.print("\t");
  delay(1000); 
  Serial.println("Php 50.00"); // cost
  balance = balance - 50; //subtract the cost from the balance
  
  landingPage();
}

void dishSix() {
  Serial.print("KARE-KARE");
  Serial.print("\t");
  delay(1000);
  Serial.println("Php 55.00"); // cost
  balance = balance - 55; //subtract the cost from the balance
  
  landingPage(); 
}

void dishThree() {
  Serial.print("TULINGAN");
  Serial.print("\t");
  delay(1000);
  Serial.println("Php 55.00"); // cost
  balance = balance - 55; //subtract the cost from the balance
  
  landingPage(); 
}

void dishEight() {
  Serial.print("MENUDO  ");
  Serial.print("\t");
  delay(1000);
  Serial.println("Php 50.00"); // cost
  balance = balance - 50; //subtract the cost from the balance

  landingPage(); 
}

void dishFive() {
  Serial.print("AFRITADA");
  Serial.print("\t");
  delay(1000);
  Serial.println("Php 55.00"); // cost
  balance = balance - 55; //subtract the cost from the balance
  
  landingPage(); 
}

void dishTwo() {
  Serial.print("SINIGANG");
  Serial.print("\t");
  delay(1000);
  Serial.println("Php 45.00"); // cost
  balance = balance - 45; //subtract the cost from the balance
  
  landingPage();
}

void dishSeven() {
  Serial.print("SIOMAI  ");
  Serial.print("\t");
  delay(1000);
  Serial.println("Php 20.00"); // cost
  balance = balance - 20; //subtract the cost from the balance
  
  landingPage();
}

void dishFour() {
  Serial.print("CHICKEN  ");
  Serial.print("\t");
  delay(1000);
  Serial.println("Php 45.00"); // cost
  balance = balance - 45; //subtract the cost from the balance
  
  landingPage();
}

void dishOne() {
  Serial.print("ADOBO  ");
  Serial.print("\t");
  delay(1000);
  Serial.println("Php 50.00"); // cost
  balance = balance - 50; //subtract the cost from the balance
  
  landingPage();
}

Very good. You can see that the following is in void setup() which prints your balance at the start, but you need to have it in void loop() to update all the time.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.