#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "DHT.h"
#define TFT_DC 8
#define TFT_CS 10
#define PowerPin 2 // Deklarasi pin untuk power
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
#define LATAR ILI9341_BLACK
#define HURUF ILI9341_WHITE
const int temperaturePin = 4; // Pin untuk sensor suhu
DHT dht(temperaturePin, DHT22);
const int fadeInSpeed = 10; // Kecepatan fading (ms per langkah)
const int fadeSteps = 255; // Jumlah langkah fading
// Pins untuk potensiometer dari tone control
const int potTreblePin = A1;
const int potBassPin = A2;
const int BTN1Pin = A3; // Pin untuk tombol 1
const int BTN2Pin = A4; // Pin untuk tombol 2
bool powerState = false;
int button1State = 0;
int button2State = 0; // Variabel untuk menyimpan status tombol
int currentInput = 1; // Status Input awal
int prevPotValues[3] = {0, 0, 0}; // Nilai pembacaan potensiometer sebelumnya
int previousButton1State = LOW;
int previousButton2State = LOW;
// Fungsi untuk Center text
void rataTengah(String text, int yPos, int xPos, int textSize) {
tft.setTextSize(textSize);
int textWidth = text.length() * 8 * textSize;
int adjustedXPos = xPos - (textWidth / 2);
tft.setCursor(adjustedXPos, yPos);
tft.print(text);
}
//Fungsi rata kiri
void ratakiri(String text, int yPos, int xOffset, int textSize) {
tft.setTextSize(textSize);
tft.setCursor(xOffset, yPos);
tft.print(text);
}
void setup() {
// Inisialisasi Pin
pinMode(BTN1Pin, INPUT); // Pin Untuk Button
pinMode(BTN2Pin, INPUT); // Pin Untuk Button
pinMode(PowerPin, OUTPUT); // Deklarasi power pin
pinMode(3, OUTPUT); // Pin untuk Switch Input
// Inisialisasi TFT
tft.begin();
tft.fillScreen(LATAR);
tft.setRotation(3); // Set orientasi layar (0=0, 1=90 , 2=180, 3=270
dht.begin();
// Tampilkan pesan awal pada TFT
tft.setTextSize(3);
tft.setTextColor(HURUF);
rataTengah("WELCOME", 100, 200, 4);
delay(50);
tft.fillScreen(LATAR); // Clear screen
rataTengah("High Fidelity", 60, 215, 3);
rataTengah("AMPLIFIER", 100, 215, 4); // Ubah ke displayTextCentered agar teks terletak di tengah
tft.setTextColor(ILI9341_RED);
for (int i = 0; i <= 10; i++) {
int lineLength = map(i, 0, 10, 0, 300);
int yPosLine = 132;
tft.drawLine(30, yPosLine, 30 + lineLength, yPosLine,HURUF);
rataTengah("by Z M R Elcotronic", 135, 215, 2);
delay(70);
}
delay(20);
tft.fillScreen(LATAR); // Clear screen
tft.setTextColor(HURUF);
digitalWrite(PowerPin, HIGH);
digitalWrite(3, LOW);
ratakiri("Hi-Fi Amplifier", 15, 5, 2);
ratakiri("Input :", 35, 5, 2);
ratakiri("Left", 160, 55, 2.5);
ratakiri("Right", 160, 180, 2.5);
ratakiri("Volume", 190, 100, 3);
}
void loop() {
tft.setTextColor(HURUF);
// Baca nilai dari potensio
int potTrebleValue = analogRead(potTreblePin);
int potBassValue = analogRead(potBassPin);
drawDonutBar(potTrebleValue, 1023, 100, 110, 38, 25);
drawDonutBar(potBassValue, 1023, 220, 110, 38, 25);
ratakiri((currentInput == 1) ? "AUX" : "BT", 35, 100, 2);
float prevTemperature = 0; // Variabel untuk menyimpan suhu sebelumnya
float temperature = dht.readTemperature();
if (temperature != prevTemperature) {
// Hapus teks suhu sebelumnya dengan menggambar kotak putih di atasnya
tft.fillRect(240, 35, 70, 15, LATAR);
// Simpan nilai suhu saat ini sebagai nilai suhu sebelumnya
prevTemperature = temperature;
}
if (temperature >= 65) {
digitalWrite(PowerPin, LOW); // Matikan perangkat jika suhu mencapai 50 derajat Celsius
ratakiri("Temp: HIGH!", 35, 170, 2); // Tampilkan peringatan suhu tinggi pada layar
} else {
digitalWrite(PowerPin, HIGH);
char temperatureStr[6]; // Array karakter untuk menyimpan string suhu
temperatureStr[5] = '\0'; // Pastikan null-terminated string
dtostrf(temperature, 5, 2, temperatureStr);
ratakiri("Temp: " + String(temperatureStr) + "C", 35, 170, 2); // Tampilkan suhu pada layar
}
// Baca status tombol
int button1State = digitalRead(BTN1Pin);
int button2State = digitalRead(BTN2Pin);
// Periksa apakah tombol ditekan
if (button1State == HIGH && previousButton1State == LOW) {
tft.fillScreen(0);
delay(500);
rataTengah("GOODBYE", 100, 200, 5);
delay(1000);
tft.fillScreen(0);
digitalWrite(PowerPin, !digitalRead(PowerPin));
rataTengah("High Fidelity", 60, 215, 3);
rataTengah("AMPLIFIER", 100, 215, 4);
tft.setTextColor(ILI9341_RED);
for (int i = 0; i <= 10; i++) {
int lineLength = map(i, 0, 10, 0, 300);
int yPosLine = 132;
tft.drawLine(30, yPosLine, 30 + lineLength, yPosLine,HURUF);
rataTengah("by Z M R Elcotronic", 135, 215, 2);
delay(70);
}
delay(900);
}
previousButton1State = button1State;
if (button2State == HIGH && previousButton2State == LOW) {
delay(300);
tft.fillRect(100, 35, 40, 20, LATAR);
currentInput = (currentInput == 1) ? 2 : 1;
digitalWrite(3, !digitalRead(3));
}
previousButton2State = button2State;
// Save previous potentiometer values
prevPotValues[1] = potTrebleValue;
prevPotValues[2] = potBassValue;
delay(50);
}
// Fungsi Fadein Text
void fadeInText(String text, int yPos, int textSize) {
int screenWidth = tft.width(); // Dapatkan lebar layar
int textWidth = text.length() * 6 * textSize; // Hitung lebar teks secara kasar (asumsi 6 pixel per karakter)
int xPos = (screenWidth - textWidth) / 2; // Hitung posisi x relatif terhadap lebar teks
tft.setTextSize(textSize);
// Fade in teks secara bertahap
for (int brightness = 0; brightness <= fadeSteps; brightness++) {
// Set alpha level untuk fade in
tft.setTextColor(HURUF, 0); // Set warna teks putih dengan alpha level sesuai brightness
tft.setCursor(xPos, yPos);
tft.print(text);
delay(fadeInSpeed); // Tunggu sebelum langkah berikutnya
}
}
// Fungsi bar potensio di TFT
void drawDonutBar(int value, int max, int xPos, int yPos, int outerRadius, int innerRadius) {
// Hitung besar sudut untuk PIE
float startAngle = radians(120); // Sudut awal di mana potensiometer berada pada nilai 0
float endAngle = radians(38); // Sudut akhir di mana potensiometer berada pada nilai 100
float angleRange = startAngle - endAngle; // Rentang sudut di mana potensiometer bergerak
// Variabel untuk menyimpan nilai potensio sebelumnya
static int prevValue = 0;
int numLines = map(value, 0, max, 0, 360); // Jumlah garis sesuai dengan nilai potensiometer
// Memeriksa apakah nilai potensi telah berubah
if (value != prevValue) {
// Hapus garis sebelumnya dengan menggambar garis baru dengan warna latar belakang
for (int i = 0; i < prevValue; i++) {
float potAngle = startAngle + (i * angleRange / 100);
float startX = xPos + innerRadius * cos(potAngle);
float startY = yPos + innerRadius * sin(potAngle);
float endX = xPos + outerRadius * cos(potAngle);
float endY = yPos + outerRadius * sin(potAngle);
tft.drawLine(startX, startY, endX, endY, LATAR);
}
// Simpan nilai potensio saat ini sebagai nilai potensio sebelumnya
prevValue = value;
}
// Gambar garis potensio baru
for (int i = 0; i < numLines; i++) {
float potAngle = startAngle + (i * angleRange / 100);
float startX = xPos + innerRadius * cos(potAngle);
float startY = yPos + innerRadius * sin(potAngle);
float endX = xPos + outerRadius * cos(potAngle);
float endY = yPos + outerRadius * sin(potAngle);
tft.drawLine(startX, startY, endX, endY, HURUF);
}
}