//Konversi format Hexa rgb 565 ke bahasa manusia :v
#define TFT_BLACK 0x0000 /* 0, 0, 0 */
#define TFT_NAVY 0x000F /* 0, 0, 128 */
#define TFT_DARKGREEN 0x03E0 /* 0, 128, 0 */
#define TFT_DARKCYAN 0x03EF /* 0, 128, 128 */
#define TFT_MAROON 0x7800 /* 128, 0, 0 */
#define TFT_PURPLE 0x780F /* 128, 0, 128 */
#define TFT_OLIVE 0x7BE0 /* 128, 128, 0 */
#define TFT_LIGHTGREY 0xC618 /* 192, 192, 192 */
#define TFT_DARKGREY 0x7BEF /* 128, 128, 128 */
#define TFT_BLUE 0x001F /* 0, 0, 255 */
#define TFT_GREEN 0x07E0 /* 0, 255, 0 */
#define TFT_CYAN 0x07FF /* 0, 255, 255 */
#define TFT_RED 0xF800 /* 255, 0, 0 */
#define TFT_MAGENTA 0xF81F /* 255, 0, 255 */
#define TFT_YELLOW 0xFFE0 /* 255, 255, 0 */
#define TFT_WHITE 0xFFFF /* 255, 255, 255 */
#define TFT_ORANGE 0xFDA0 /* 255, 180, 0 */
#define TFT_GREENYELLOW 0xB7E0 /* 180, 255, 0 */
//Menyertakan library dan inisialisasi
#include <Adafruit_GFX.h>
#include "Arduino.h"
#include "DFRobotDFPlayerMini.h"
#include "HX711.h"
HX711 scale;
DFRobotDFPlayerMini Speech;
//Pendefinisian variabel konstan===================================================================================================================================================
//metode analisis fat dan lbm
#define CALCULATION 0
//metode komunikasi microcontrol dengan panel lcd,,, SPI atau Parallel
#define SPI_DISPLAY //memberitahu compiler jika menggunakan lcd SPI
//#define PARALLEL_DISPLAY //memberitahu compiler jika menggunakan lcd parallel
//Definisi dan konstanta dibawah digunakan jika menggunakan LCD Parallel
#if defined(PARALLEL_DISPLAY)
#include <MCUFRIEND_kbv.h> //Memanggil library dan inisialisasi LCD
MCUFRIEND_kbv tft;
#include "TouchScreen.h" //Memanggil library dan inisialisasi Touchscreen
TouchScreen ts = TouchScreen(6, A1, A2, 7, 300);
#define HX711_SCK 10 //pin sck hx711
#define HX711_DT 11 //pin dt hx711
#define TRIG_PIN 12 //pin trigger ultrasonik
#define ECHO_PIN 13 //pin echo ultrasonik
#define BATTERY_PIN A4 //pin pembacaan level baterai
#define IMPEDANCE_PIN A5 //pin pembacaan BIA
#define HOWLAND_CURENT 0.28 //konstanta untuk arus pengukuran BIA (mA)
#define VOLTAGE_OFFSET 600 //konstanta tegangan offset
#define GAIN 80.0 //konstanta GAIN INSTRUMENTATION AMP
#define baseline 203.0 //nilai garis dasar pengukuran jarak
#define ID 0x9341 //alamat LCD
#else
//Definisi dan konstanta dibawah digunakan jika menggunakan LCD SPI
#include <Adafruit_GFX.h>
#include <SPI.h>
#include <Adafruit_ILI9341.h>
#include <Arduino.h>
#include <Adafruit_FT6206.h>
#define TFT_CS 10
#define TFT_DC 9
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
Adafruit_FT6206 ctp = Adafruit_FT6206();
#define HX711_SCK 5
#define HX711_DT 6
#define TRIG_PIN 8
#define ECHO_PIN 7
#define BATTERY_PIN -1
#define IMPEDANCE_PIN -1
#define HOWLAND_CURENT 0.28
#define VOLTAGE_OFFSET 0
#define GAIN 80.0
#define baseline 200.0 //cm
#endif
#include <Fonts/FreeSansBold9pt7b.h>
//#include <FreeDefaultFonts.h>
#define wanita 2
#define pria 1
//=================================================================================================================================================================================
//Kustomisasi keypad GUI===========================================================================================================================================================
//setting ukuran keypad
#define BUTT_X 16 //koordinat x awal
#define BUTT_Y 150 //koordinat y awal
#define BUTT_W 40 //lebar tbl keypad
#define BUTT_H 40 //tinggi tbl keyoad
#define BUTT_XSPACE 16 //spasi antar keypad
#define BUTT_YSPACE 16 //spasi antar keypad
//setting label keypad
#define LABEL_X 15 //awal koordinat label per tombol
#define LABEL_Y 26 //awal koordinat label per tombol
#define LABEL_SIZ 1
//=================================================================================================================================================================================
//warna mewarnai, format hex rgb565 (16-bit colour)================================================================================================================================
//setting warna ui, background ui, dan warna font ui
#define UI_FONT FreeSansBold9pt7b
#define UI_COLR 0x247b //warna tampilan UI
#define UI_FNT_COLR 0x247b //warna font UI
#define UI_BACKGROUND 0 //warna background UI
//setting warna keypad
#define BUTT_COLR 0x247b //warna tombol keypad
#define BUTT_C_COLR 0xf800 //warna tombol keypad C "clear"
#define BUTT_OK_COLR 0x07e0 //warna tombol keypad "OK"
#define BUTT_PRESSED_COLR 0X001F //warna tombol keypad saat ditekan
#define LABEL_COLR 0x247b //warna font label keypad
//=================================================================================================================================================================================
//pengalamatan file mp3============================================================================================================================================================
#define SYSTEM_READY 21
#define HARDWARE_ERROR 22
#define OPENING 23
#define TB_SPEECH 24
#define BB_SPEECH 25
#define BON_SPEECH 26
#define BMI_SPEECH 27
#define FM_SPEECH 28
#define LBM_SPEECH 29
#define REJECTION 31
#define CM_SPEECH 34
#define KG_SPEECH 35
#define KKAL_SPEECH 36
#define KGM2_SPEECH 37
#define PERSEN_SPEECH 38
#define IDEAL 41
#define KURANG_IDEAL 42
#define TIDAK_IDEAL 43
#define TBW_SPEECH 44
//=================================================================================================================================================================================
//inisialisasi variabel============================================================================================================================================================
unsigned int color_picker;
int keypad_content;
int keypad_monitor;
int gender = 0;
int Cor_X[12];
int Cor_Y[12];
bool State_register[12] = {
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
};
unsigned int But_Color[12] = {
BUTT_COLR,
//WARNA UI
BUTT_COLR,
BUTT_COLR,
BUTT_COLR,
BUTT_COLR,
BUTT_COLR,
BUTT_COLR,
BUTT_COLR,
BUTT_C_COLR,
BUTT_COLR,
BUTT_COLR,
BUTT_OK_COLR
};
String gndr[3] = {
"Null",
"Laki-laki",
"Perempuan"
};
char* label[12] = {
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"C",
"9",
"0",
"OK"
};
char data_keypad[12] = {
1,
2,
3,
4,
5,
6,
7,
8,
11,
9,
0,
12
};
bool pressed = 0;
int tsx, tsy, tsz;
//=================================================================================================================================================================================
//inisialisasi varabel dinamis dan library=========================================================================================================================================
void setup() {
delay(100);
Serial.begin(9600);
#if defined(SPI_DISPLAY)
tft.begin();
#else
tft.begin(ID);
#endif
tft.setRotation(0);
tft.fillScreen(UI_BACKGROUND);
tft.setFont(&UI_FONT);
tft.setTextSize(3);
tft.setCursor(8, 130);
tft.println("BCS - 02");
tft.setTextSize(1);
tft.setCursor(73, 160);
tft.println("BY: Husain");
tft.setTextSize(1);
tft.setCursor(0, 260);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
scale.begin(HX711_DT, HX711_SCK);
delay(1000);
scale.set_scale(22790);
int hcsr04state = ultrasonicscan();
int df_state = 1;
if (Speech.begin(Serial)) {
tft.println("Periksa koneksi DF_Player");
df_state = 0;
}
Speech.volume(15);
//Serial.println(F("DFPlayer Mini online."));
if (hcsr04state == false) tft.println("Periksa koneksi HCSR04");
if (hcsr04state != 0 && df_state != 0) {
self_adjustment();
Speech.playMp3Folder(SYSTEM_READY);
tft.fillScreen(UI_BACKGROUND);
battery_scan(10, 10, 210, 20, true);
tft.setCursor(0, 80);
tft.println("> Posisikan tubuh pada");
tft.println(" timbangan");
tft.println("> Letakkan tangan pada");
tft.println(" elektroda");
tft.println("> Sentuh layar untuk");
tft.println(" melanjutkan");
//mapp(0xffff); //kanvas koordinat tft
do {
ts_scan();
} while (pressed == 0);
tft.fillRect(0, 40, 240, 280, UI_BACKGROUND);
} else {
Speech.playMp3Folder(HARDWARE_ERROR);
while (1)
;
}
}
//=================================================================================================================================================================================
//Looping program utama beserta sub fungsi=========================================================================================================================================
void loop() {
tsx = 0;
tsy = 0;
tsz = 0;
ts_scan();
battery_scan(10, 10, 210, 20, false);
main_program_run();
}
//=================================================================================================================================================================================
//Subfungsi pemindai level baterai=================================================================================================================================================
void battery_scan(int x, int y, int w, int h, bool reset) {
static int adc,
percent,
oldpercent = 200,
rs = true;
reset = true ? rs = reset : 0;
static unsigned long oldmls;
if (rs == true) {
tft.drawRoundRect(x - 9, y - 9, 238, 38, 10, UI_COLR);
tft.setTextColor(UI_FNT_COLR);
tft.setCursor(x, y + h - 5);
tft.print("Baterai");
}
if (((millis() - oldmls) > 50) || reset) {
//update batre setiap 50ms
//non blocking delay dengan millis
oldmls = millis(); //reset timer
adc = analogRead(BATTERY_PIN);
if (adc < 655) adc = 655;
else if (adc > 860) adc = 860;
percent = map(adc, 655, 860, 0, 100);
if ((oldpercent != percent) || reset) {
int rev = map(adc, 655, 860, 100, 0);
rainbow(0, 100, rev);
int xb = map(percent, 0, 100, 70 + x + 2, x + w - 2);
tft.drawRect(70 + x, y, w - 70, h, UI_COLR);
tft.fillRect(70 + x + 1, y + 1, w - 2 - 70, h - 2, UI_BACKGROUND);
for (int wi = 70 + x + 2; wi < xb; wi++) {
tft.drawFastVLine(wi, y + 2, h - 4, rainbow(0, 100, rev));
}
tft.setCursor(130, y + h - 5);
tft.print(percent);
tft.print(" %");
oldpercent = percent;
rs = 0;
}
}
}
//================================================================================================================================================================================
//Penginputan data pasien > pengukuran > hingga penyajian data====================================================================================================================
void main_program_run() {
//Inisialisasi variabel dinamis lokal
static int steps = 0,
oldv,
umur;
static bool fk = 1,
fk1 = 1;
static float kg, cm;
switch (steps) {
case 0:
//input data gender
gender_selector(0, 70);
if (gender > 0) {
tft.fillScreen(UI_BACKGROUND);
steps = 1;
}
break;
case 1:
//input data umur
if (fk) {
keypad_init(BUTT_X, BUTT_Y, BUTT_W, BUTT_H, BUTT_XSPACE, BUTT_YSPACE, 4, 3);
tft.drawRoundRect(1, 50, 238, 60, 10, UI_COLR);
battery_scan(10, 10, 210, 20, true);
fk = 0;
}
keypad_run(200); //limit usia 200
tft.setTextColor(UI_FNT_COLR);
if (oldv != keypad_monitor) {
tft.setTextSize(2);
tft.fillRect(20, 60, 80, 40, UI_BACKGROUND);
tft.setCursor(20, 92);
tft.print(String(keypad_monitor));
tft.setCursor(100, 92);
tft.print("Tahun");
oldv = keypad_monitor;
tft.setTextSize(1);
}
//pengukuran
if (keypad_content != 0) {
steps = 2;
umur = keypad_content;
keypad_content = 0;
oldv = 1;
for (int i = 5; i > 0; i--) {
tft.fillScreen(UI_BACKGROUND);
tft.setTextSize(1);
tft.setCursor(15, 70);
tft.print("Berdirilah dengan Tegak");
tft.setTextSize(3);
tft.setCursor(100, 150);
tft.print(i);
delay(800);
}
tft.fillScreen(UI_BACKGROUND);
tft.setTextSize(1);
kg = scale.get_units(10);
cm = dist_scan();
cm = baseline - cm;
battery_scan(10, 10, 210, 20, true);
#if defined(SPI_DISPLAY)
kg = 40.4; //force simulation
cm = 164.5;
#endif
}
break;
case 2:
//Inisialisasi variabel dinamis lokal
if (fk1) {
float V_BMI,
V_BON,
V_FAT,
V_TBW,
V_LBM, V_IMPEDANCE;
int xo = 10;
int yo = 50;
unsigned int C_BMI,
C_BON,
C_FAT,
C_LBM,
C_TBW;
unsigned int S_BMI,
S_BON,
S_FAT,
S_LBM,
S_TBW;
//pemetaan hasil terhadap rentang nilai
tft.drawRoundRect(1, 50, 238, 270, 10, UI_COLR);
if (gender == wanita) {
V_TBW = TBW_P(cm, kg);
C_TBW = rangeToColor(42, 60, V_TBW);
S_TBW = valueToState(42, 60, V_TBW);
#if (CALCULATION == 0)
V_IMPEDANCE = impedanceRead(IMPEDANCE_PIN, HOWLAND_CURENT, GAIN);
V_LBM = LBM_P(cm, kg, umur, V_IMPEDANCE);
V_FAT = FAT_P(V_LBM, kg);
#else
V_FAT = FAT_P(cm, kg, umur);
V_LBM = LBM_P(cm, kg);
#endif
V_BON = BON_P(kg, V_FAT);
C_FAT = rangeToColor(25, 30, V_FAT);
S_FAT = valueToState(25, 30, V_FAT);
if (umur >= 18 && umur <= 35) {
C_LBM = rangeToColor(31, 33, V_LBM);
S_LBM = valueToState(31, 33, V_LBM);
} else if (umur >= 36 && umur <= 55) {
C_LBM = rangeToColor(29, 31, V_LBM);
S_LBM = valueToState(29, 31, V_LBM);
} else if (umur >= 56 && umur <= 75) {
C_LBM = rangeToColor(27, 30, V_LBM);
S_LBM = valueToState(27, 30, V_LBM);
} else if (umur >= 76) {
C_LBM = rangeToColor(25, 27, V_LBM);
S_LBM = valueToState(25, 30, V_LBM);
}
} else if (gender == pria) {
V_TBW = TBW_L(cm, kg);
C_TBW = rangeToColor(48, 67, V_TBW);
S_TBW = valueToState(48, 67, V_TBW);
#if (CALCULATION == 0)
V_IMPEDANCE = impedanceRead(IMPEDANCE_PIN, HOWLAND_CURENT, GAIN);
V_LBM = LBM_L(cm, kg, umur, V_IMPEDANCE);
V_FAT = FAT_L(V_LBM, kg);
#else
V_FAT = FAT_L(cm, kg, umur);
V_LBM = LBM_L(cm, kg);
#endif
V_BON = BON_L(kg, V_FAT);
C_FAT = rangeToColor(18, 25, V_FAT);
S_FAT = valueToState(18, 25, V_FAT);
if (umur >= 18 && umur <= 35) {
C_LBM = rangeToColor(40, 44, V_LBM);
S_LBM = valueToState(40, 44, V_LBM);
} else if (umur >= 36 && umur <= 55) {
C_LBM = rangeToColor(36, 40, V_LBM);
S_LBM = valueToState(36, 40, V_LBM);
} else if (umur >= 56 && umur <= 75) {
C_LBM = rangeToColor(32, 35, V_LBM);
S_LBM = valueToState(32, 35, V_LBM);
} else if (umur >= 76) {
C_LBM = rangeToColor(30, 32, V_LBM);
S_LBM = valueToState(30, 32, V_LBM);
}
}
V_BMI = BMI(cm, kg);
C_BMI = rangeToColor(18.5, 24.9, V_BMI);
S_BMI = valueToState(18.5, 24.9, V_BMI);
//FORCE UNTUK CHECKSOUND
/*cm = 170;
kg = 88;
V_BON = 1573;
V_BMI = 21;
V_FAT = 21;
V_LBM = 42;*/
//
//Penyajian data (LCD)
tft.setTextColor(UI_FNT_COLR);
yo = yo += 23;
tft.setCursor(xo, yo);
tft.print("Jenis kelamin");
tft.setCursor(xo + 120, yo);
tft.print(": ");
tft.print(gndr[gender]);
yo = yo += 23;
tft.setCursor(xo, yo);
tft.print("Tinggi Badan");
tft.setCursor(xo + 120, yo);
tft.print(": ");
tft.print(cm, 1);
tft.setCursor(xo + 180, yo);
tft.print("cm");
yo = yo += 23;
tft.setCursor(xo, yo);
tft.print("Berat Badan");
tft.setCursor(xo + 120, yo);
tft.print(": ");
tft.print(kg, 1);
tft.setCursor(xo + 180, yo);
tft.print("Kg");
yo = yo += 23;
tft.setCursor(xo, yo);
tft.print("Jumlah BMI");
tft.setCursor(xo + 120, yo);
tft.print(": ");
tft.setTextColor(C_BMI);
tft.print(V_BMI, 1);
tft.setTextColor(UI_FNT_COLR);
yo = yo += 23;
tft.setCursor(xo, yo);
tft.print("Lemak Tubuh");
tft.setCursor(xo + 120, yo);
tft.print(": ");
tft.setTextColor(C_FAT);
tft.print(V_FAT, 1);
tft.setTextColor(UI_FNT_COLR);
tft.setCursor(xo + 180, yo);
tft.print("%");
yo = yo += 23;
tft.setCursor(xo, yo);
tft.print("Mineral .T");
tft.setCursor(xo + 120, yo);
tft.print(": ");
tft.setTextColor(C_TBW);
tft.print(V_TBW, 1);
tft.setCursor(xo + 180, yo);
tft.setTextColor(UI_FNT_COLR);
tft.print("%");
yo = yo += 23;
tft.setCursor(xo, yo);
tft.print("Massa Otot");
tft.setCursor(xo + 120, yo);
tft.print(": ");
tft.setTextColor(C_LBM);
tft.print(V_LBM, 1);
tft.setCursor(xo + 180, yo);
tft.setTextColor(UI_FNT_COLR);
tft.print("%");
yo = yo += 23;
tft.setCursor(xo, yo);
tft.print("Massa Tulang");
tft.setCursor(xo + 120, yo);
tft.print(": ");
tft.print(V_BON, 1);
tft.setCursor(xo + 180, yo);
tft.print("Kg");
//Penyajian data (Suara)
Speech.playMp3Folder(OPENING);
delay(6800);
Speech.playMp3Folder(TB_SPEECH);
countToSpeech(cm);
delay(1000);
Speech.playMp3Folder(CM_SPEECH);
delay(4000);
Speech.playMp3Folder(BB_SPEECH);
countToSpeech(kg);
delay(1000);
Speech.playMp3Folder(KG_SPEECH);
delay(4000);
Speech.playMp3Folder(BMI_SPEECH);
delay(3000);
Speech.playMp3Folder(S_BMI);
delay(4000);
Speech.playMp3Folder(FM_SPEECH);
delay(3000);
Speech.playMp3Folder(S_FAT);
delay(4000);
Speech.playMp3Folder(TBW_SPEECH);
delay(3000);
Speech.playMp3Folder(S_TBW);
delay(4000);
Speech.playMp3Folder(LBM_SPEECH);
delay(3000);
Speech.playMp3Folder(S_LBM);
delay(4000);
Speech.playMp3Folder(BON_SPEECH);
delay(3000);
countToSpeech(V_BON);
delay(1000);
Speech.playMp3Folder(KG_SPEECH);
delay(4000);
fk1 = 0;
}
if (end_button(100, 270, 40, 40, String("OK"), UI_COLR)) {
tft.fillScreen(UI_BACKGROUND);
fk = 1;
fk1 = 1;
umur = 0;
gender = 0;
delay(100);
battery_scan(10, 10, 210, 20, true);
steps = 0;
}
break;
}
}
//===============================================================================================================================================================================
//Subfungsi lainnya==============================================================================================================================================================
unsigned int rangeToColor(int low, int high, int val) {
unsigned int range = high - low;
unsigned int quarter = range / 4;
if (val < low) return TFT_RED;
if (val >= low && val <= (low + quarter)) return TFT_YELLOW;
if (val > (low + quarter) && val < (high - quarter)) return TFT_GREEN;
if (val >= (high - quarter) && val <= high) return TFT_YELLOW;
if (val > high) return TFT_RED;
}
unsigned int rainbow(int low, int high, int val) {
int r,
g;
val <= low ? val = low : 0;
val >= high ? val = high : 0;
int d = map(val, low, high, 0, 100);
if ((d >= 50) && (d <= 85))
g = map(d, 50, 85, 255, 0);
else if ((d <= 49) && (d >= 0)) g = 255;
else if ((d >= 86) && (d <= 100)) g = 0;
if ((d >= 0) && (d <= 50))
r = map(d, 0, 50, 80, 255);
//else if((d <= 49) && (d >= 1))r = 0;
else if ((d >= 51) && (d <= 100)) r = 255;
return tft.color565(r, g, 0);
}
int end_button(int x, int y, int w, int h, String lab, int color) {
static bool fk = 1;
if (tsx > x && tsx < x + w && tsy > y && tsy < y + h && pressed) {
draw_button(x, y, w, h, lab, BUTT_PRESSED_COLR);
fk = 0;
} else {
draw_button(x, y, w, h, lab, color);
if (fk) {
return 0;
} else {
fk = 1;
return 1;
}
}
}
void gender_selector(int x, int y) {
static int fk = 1,
fo = 0,
gndr = 0,
oldgndr = 4;
if (fk) {
tft.drawRoundRect(x, y, 238, 170, 10, UI_COLR);
tft.setTextColor(UI_FNT_COLR);
tft.setCursor(x + 15, y + 5);
tft.fillRect(x + 14, y - 10, 150, 30, UI_BACKGROUND);
tft.println("Input gender anda");
tft.drawCircle(x + 25, y + 45, 15, UI_COLR);
tft.setCursor(x + 50, y + 50);
tft.println("Perempuan");
tft.drawCircle(x + 25, y + 85, 15, UI_COLR);
tft.setCursor(x + 50, y + 90);
tft.println("Laki-laki");
tft.drawRoundRect(x + 40, y + 120, 100, 30, 10, UI_COLR);
tft.setCursor(x + 75, y + 141);
tft.println("OK");
fk = 0;
}
if ((tsx > x + 10) && (tsx < x + 40) && (tsy > y + 70) && (tsy < y + 100) && pressed) {
gndr = 1;
}
if ((tsx > x + 10) && (tsx < x + 40) && (tsy > y + 30) && (tsy < y + 60) && pressed) {
gndr = 2;
}
if (oldgndr != gndr) {
tft.fillCircle(x + 25, y + 45, 15, UI_BACKGROUND);
tft.fillCircle(x + 25, y + 85, 15, UI_BACKGROUND);
tft.drawCircle(x + 25, y + 45, 15, UI_COLR);
tft.drawCircle(x + 25, y + 85, 15, UI_COLR);
if (gndr == 1) tft.fillCircle(x + 25, y + 85, 15, UI_COLR);
if (gndr == 2) tft.fillCircle(x + 25, y + 45, 15, UI_COLR);
oldgndr = gndr;
}
if ((tsx > x + 40) && (tsx < x + 140) && (tsy > y + 120) && (tsy < y + 150) && pressed) {
tft.drawRoundRect(x + 40, y + 120, 100, 30, 10, BUTT_PRESSED_COLR);
fo = 1;
} else {
if (fo == 1) {
tft.drawRoundRect(x + 40, y + 120, 100, 30, 10, UI_COLR);
if (gndr > 0) {
oldgndr = 4;
fk = 1;
//return gender;
gender = gndr;
gndr = 0;
}
fo = 0;
}
}
}
float ultrasonicscan() {
float distance;
static long duration;
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance = duration * 0.034 / 2;
return distance;
}
float dst_sampling, ds;
float dist_scan() {
for (int i = 0; i < 100; i++) {
ds = ultrasonicscan();
dst_sampling = dst_sampling + ds;
}
dst_sampling = dst_sampling / 100.0;
return dst_sampling;
}
void self_adjustment() {
tft.setCursor(22, 280);
tft.print("Menyiapkan sistem...");
tft.setCursor(22, 300);
tft.print("Kosongkan timbangan");
scale.tare(50);
tft.fillRect(0, 40, 240, 280, UI_BACKGROUND);
}
void draw_button(int x, int y, int w, int h, String lab, int color) {
tft.drawRoundRect(x, y, w, h, 10, color);
tft.drawRoundRect(x + 1, y + 1, w - 2, h - 2, 10, color);
tft.setTextColor(LABEL_COLR);
tft.setTextSize(LABEL_SIZ);
if (lab.length() != 1)
tft.setCursor(LABEL_X + x - 8, LABEL_Y + y);
else
tft.setCursor(LABEL_X + x, LABEL_Y + y);
tft.print(lab);
}
void keypad_init(int x, int y, int w, int h, int xs, int ys, int col, int row) {
static int count;
const int xc = w + xs;
const int yc = h + ys;
tft.drawRoundRect(x - (xs - 1), y - (xs - 1), col * (w + xs) + xs - 2, row * (h + ys) + ys, 10, UI_COLR);
tft.setFont(NULL);
tft.setTextColor(UI_FNT_COLR, UI_BACKGROUND);
tft.setCursor(x + xs, y - ys - 3);
tft.print("Klik OK untuk pengukuran");
for (int j = 0; j < row; j++) {
for (int i = 0; i < col; i++) {
Cor_X[count] = x + (xc * i);
Cor_Y[count] = y + (yc * j);
count++;
}
}
tft.setFont(&UI_FONT);
count = 0;
}
void keypad_run(int limit) {
static unsigned int cont;
static char picker,
shifter = 1;
static bool
fk = 0;
for (int a = 0; a <= 11; a++) {
if ((tsx >= Cor_X[a]) && (tsx <= (Cor_X[a] + BUTT_W)) && (tsy >= Cor_Y[a]) && (tsy <= (Cor_Y[a] + BUTT_H)) && (pressed == 1)) {
State_register[a] = true;
picker = data_keypad[a];
fk = 1;
} else State_register[a] = false;
State_register[a] == true ? draw_button(Cor_X[a], Cor_Y[a], BUTT_W, BUTT_H, label[a], BUTT_PRESSED_COLR) : draw_button(Cor_X[a], Cor_Y[a], BUTT_W, BUTT_H, label[a], But_Color[a]);
}
if (fk == 1 && pressed == 0) {
if (picker != 11 && picker != 12) {
cont = cont * shifter;
cont = cont + picker;
shifter = 10;
} else if (picker == 11) {
shifter = 1;
picker = 0;
cont = 0;
} else if (picker == 12) {
keypad_content = cont;
shifter = 1;
picker = 0;
cont = 0;
}
if (cont > limit) {
shifter = 1;
picker = 0;
cont = 0;
}
keypad_monitor = cont;
fk = 0;
}
}
#if defined(SPI_DISPLAY)
void ts_scan() {
pressed = 0;
int x, y;
for (int i = 0; i < 10; i++) {
if (ctp.touched()) {
TS_Point p = ctp.getPoint();
x = map(p.x, 0, 240, 240, 0);
y = map(p.y, 0, 320, 320, 0);
tsz = 600;
pressed = 1;
}
}
tsx = x;
tsy = y;
}
#elif defined(PARALLEL_DISPLAY)
void ts_scan() {
pressed = 0;
int x,
y;
for (int i = 0; i < 50; i++) {
TSPoint p = ts.getPoint();
if (p.z > 0 && p.z < 1000) {
x = p.x;
y = p.y;
tsz = p.z;
pressed = 1;
}
}
tsx = map(x, 191, 908, 0, 240);
tsy = map(y, 952, 218, 0, 320);
pinMode(A2, OUTPUT);
pinMode(A1, OUTPUT);
}
#endif
float BMI(float tb, float bb) {
tb = tb / 100;
tb = tb * tb;
//bb = bb*10000;
return bb / tb;
}
float BON_L(float bb, float fat) {
fat = fat * bb;
fat = fat / 100;
bb = bb - fat;
bb = bb / 10;
bb = bb * 2;
return bb;
}
float BON_P(float bb, float fat) {
fat = fat * bb;
fat = fat / 100;
bb = bb - fat;
bb = bb / 10;
bb = bb * 2;
return bb;
}
float TBW_L(float tb, float bb) {
tb = tb * 0.194786;
bb = bb * 0.296785;
tb = tb + bb;
tb = tb - 14.012934;
tb = tb * 2;
return tb;
}
float TBW_P(float tb, float bb) {
tb = tb * 0.34454;
bb = bb * 0.183809;
tb = tb + bb;
tb = tb - 35.270121;
tb = tb * 2;
return tb;
}
#if (CALCULATION == 0)
float FAT_L(float lbm, float bb) {
float bf = 100 - lbm;
return bf;
}
float FAT_P(float lbm, float bb) {
float bf = 100 - lbm;
return bf;
}
float LBM_L(float tb, float bb, float um, float impedance) {
float tbb = tb * tb;
tbb = tbb / impedance;
tbb = tbb * 0.360;
tb = tb * 0.162;
bb = bb * 0.289;
um = um * 0.134;
tbb = tbb + tb + bb - um + 4.83 + 6.83;
return tbb;
}
float LBM_P(float tb, float bb, float um, float impedance) {
float tbb = tb * tb;
tbb = tbb / impedance;
tbb = tbb * 0.360;
tb = tb * 0.162;
bb = bb * 0.289;
um = um * 0.134;
tbb = tbb + tb + bb - um + 6.83;
return tbb;
}
#else
float FAT_L(float tb, float bb, float us) {
float imt = BMI(tb, bb);
imt = imt * 1.2;
us = us * 0.23;
imt = imt + us;
imt -= 16.2;
return imt;
}
float FAT_P(float tb, float bb, float us) {
float imt = BMI(tb, bb);
imt = imt * 1.2;
us = us * 0.23;
imt = imt + us;
imt -= 5.4;
return imt;
}
float LBM_L(float tb, float bb) {
bb = bb * 0.3281;
tb = tb * 0.33929;
bb = bb + tb;
bb = bb - 29.5336;
return bb;
}
float LBM_P(float tb, float bb) {
bb = bb * 0.29569;
tb = tb * 0.41813;
bb = bb + tb;
bb = bb - 43.2933;
return bb;
}
#endif
float impedanceRead(int pin, float current_const, int gain) {
float count = 1023.0;
float Volt_bia = count * (5000.0 / 1023.0);
if (Volt_bia > 0) Volt_bia = Volt_bia + (VOLTAGE_OFFSET);
Volt_bia = Volt_bia / gain;
float imp = Volt_bia / current_const;
return imp;
}
unsigned int valueToState(int low, int high, int val) {
int range = high - low;
int quarter = range / 4;
if (val < low) return 43;
if (val >= low && val <= (low + quarter)) return 42;
if (val > (low + quarter) && val < (high - quarter)) return 41;
if (val >= (high - quarter) && val <= high) return 42;
if (val > high) return 43;
}
void countToSpeech(float bil) {
int count = int(bil);
int satuan, puluhan, ratusan, ribuan;
ribuan = count / 1000;
ribuan = ribuan * 1000;
ratusan = (count - ribuan) / 100;
ratusan = ratusan * 100;
puluhan = (count - (ribuan + ratusan)) / 10;
puluhan = puluhan * 10;
satuan = count - (ribuan + ratusan + puluhan);
if (ribuan > 0) Speech.playMp3Folder(ribuan);
delay(2800);
if (ratusan > 0) Speech.playMp3Folder(ratusan);
delay(2600);
if (puluhan > 0 && puluhan < 20) Speech.playMp3Folder(puluhan + satuan);
delay(2800);
if (puluhan >= 20) Speech.playMp3Folder(puluhan);
delay(2800);
if (puluhan >= 20) Speech.playMp3Folder(satuan);
delay(2800);
}
//kanvas koordinat
void mapp(int warna) {
static int h,
v;
for (int i = 0; i < 33; i++) {
tft.drawFastHLine(0, h, 240, warna);
h = h + 10;
}
for (int i = 0; i < 25; i++) {
tft.drawFastVLine(v, 0, 320, warna);
v = v + 10;
}
}
//================================================================================================================================================================================
Loading
ili9341-cap-touch
ili9341-cap-touch