#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 8
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
int up = 7;
int ok = 6;
int down = 5;
int sum = 0;
String actualItems[][2] = {
{"Item 1", "100"},
{"Item 2", "200"},
{"Item 3", "300"},
{"Item 4", "400"}
};
int itemCount = 0;
int cursorItem = 0;
String currentItems[20][3];
bool isChanged = true;
void setup() {
Serial.begin(9600);
tft.begin();
Serial.println("~~ AutoCart Presents ~~ ");
Serial.println("Height :- " + String(tft.height()) + " , Width :- " + String(tft.width()));
tft.setRotation(1);
tft.fillScreen(ILI9341_WHITE);
tft.fillRect(0, 0, tft.width(), 30, ILI9341_RED);
tft.setTextSize(3);
tft.setCursor(90, 5);
tft.setTextColor(ILI9341_WHITE);
tft.println("AutoCart");
tft.fillRect(0, 30, tft.width(), 30, ILI9341_BLUE);
tft.setTextSize(2);
tft.setCursor(40, 40);
tft.setTextColor(ILI9341_WHITE);
tft.println("Total amount :- ");
tft.setCursor(tft.width() - 70, 40);
tft.println(sum);
tft.fillRect(0, 60, tft.width(), 35, ILI9341_BLACK);
tft.drawRect(0, 60, tft.width(), 35, ILI9341_DARKGREY);
tft.setTextSize(2);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(20, 70);
tft.println("Name");
tft.setCursor(170, 70);
tft.println("Qty.");
tft.setCursor(250, 70);
tft.println("Price");
pinMode(up, INPUT_PULLUP);
pinMode(ok, INPUT_PULLUP);
pinMode(down, INPUT_PULLUP);
}
void loop() {
checkButtons(currentItems, actualItems, isChanged);
printList(currentItems, isChanged);
}
void checkButtons(String currentItems[20][3], String actualItems[][2], bool& isChanged) {
if (digitalRead(down) == LOW) {
if (cursorItem < itemCount-1) {
cursorItem++;
isChanged = true;
}
delay(100);
}
if (digitalRead(up) == LOW) {
if (cursorItem > 0) {
cursorItem--;
isChanged = true;
}
delay(100);
}
if (digitalRead(ok) == LOW) {
int index = random(0, 4);
String newItem[2] = actualItems[index];
bool isNewAdded = true;
for (int i = 0; i < itemCount; i++) {
if (currentItems[i][0] == newItem[0]) {
int lastQty = currentItems[i][1].toInt() + 1;
currentItems[i][1] = String(lastQty);
isNewAdded = false;
break;
}
}
if (isNewAdded) {
currentItems[itemCount][0] = newItem[0];
currentItems[itemCount][1] = "1";
currentItems[itemCount][2] = newItem[1];
itemCount++;
}
isChanged = true;
delay(200);
}
}
void printList(String currentItems[20][3], bool& isChanged) {
if (isChanged) {
sum = 0;
tft.setTextColor(ILI9341_BLACK);
for (int i = 0; i < itemCount ; i++) {
if ( i == cursorItem) {
tft.fillRect(0, 100 + i * 23, tft.width(), 23, 0xef7d);
tft.fillCircle(10, 110 + i * 23, 5, ILI9341_BLACK);
} else {
tft.fillRect(0, 100 + i * 23, tft.width(), 23, ILI9341_WHITE);
}
tft.setCursor(30, 105 + i * 23);
tft.println(currentItems[i][0]);
tft.setCursor(180, 105 + i * 23);
tft.println(currentItems[i][1]);
tft.setCursor(250, 105 + i * 23);
tft.println(currentItems[i][2]);
sum += (currentItems[i][1].toInt() * currentItems[i][2].toInt());
}
tft.fillRect(tft.width() - 70, 30, tft.width(), 30, ILI9341_BLUE);
tft.setCursor(tft.width() - 70, 40);
tft.setTextColor(ILI9341_WHITE);
tft.println(String(sum));
isChanged = false;
}
}