#include <RGBmatrixPanel.h>
#define CLK 15
#define OE 13
#define LAT 12
#define A A0
#define B A1
#define C A2
#define D A3
RGBmatrixPanel matrix(A, B, C, D, CLK, LAT, OE, false, 64);
const int startButtonPin = 34;
const int pauseButtonPin = 35;
const int stopButtonPin = 32;
int countUpValue = 0;
int countDownValue = 0;
int countUpTimer = 0;
int countDownTimer = 0;
bool countingUp = false;
bool countingDown = false;
unsigned long previousMillis = 0;
const long interval = 1000;
void setup() {
// lcd.init();
// lcd.backlight();
// lcd.setCursor(0, 0);
// lcd.print("Count Up & Down");
// lcd.setCursor(0, 1);
// lcd.print("Program");
// delay(3000);
// lcd.clear();
matrix.begin(); // Inisialisasi panel RGB matrix
matrix.setTextWrap(false);
matrix.setTextSize(1);
matrix.setTextColor(matrix.Color888(255, 0, 0)); // Warna teks (merah)
matrix.fillScreen(matrix.Color888(0, 0, 0)); // Bersihkan layar
matrix.setCursor(0, 0);
matrix.print("Hello, ESP32!");
matrix.setCursor(0, 8); // Geser ke baris kedua
matrix.print("16x2 Matrix");
delay(5000);
Serial.begin(9600);
pinMode(startButtonPin, INPUT);
pinMode(pauseButtonPin, INPUT);
pinMode(stopButtonPin, INPUT);
}
void loop(){
int startButtonState = digitalRead(startButtonPin);
int pauseButtonState = digitalRead(pauseButtonPin);
int stopButtonState = digitalRead(stopButtonPin);
if (startButtonState == 0) {
countingUp = true;
countingDown = true;
}
else if (pauseButtonState == 0) {
countingUp = false;
countingDown = false;
}
else if (stopButtonState == 0) {
countingUp = false;
countingDown = false;
countUpValue = 0;
countDownValue = 0;
countUpTimer = 0;
countDownTimer = 0;
}
if (Serial.available() > 0) {
int presetValue = Serial.parseInt();
if (presetValue > 0) {
countUpValue = presetValue;
countDownValue = presetValue;
countUpTimer = 0;
countDownTimer = presetValue;
Serial.begin(presetValue);
}
}
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
if (countingUp) {
countUpTimer++;
if (countUpTimer >= countUpValue) {
countingUp = false;
}
}
if (countingDown) {
countDownTimer--;
if (countDownTimer <= 0) {
countingDown = false;
}
}
// lcd.setCursor(0, 1);
// lcd.print("Count Up: ");
// // lcd.setCursor(10, 1);
// lcd.print(countUpTimer);
// lcd.print(" ");
// lcd.setCursor(0, 0);
// lcd.print("Count Down: ");
// // lcd.setCursor(12, 0);
// lcd.print(countDownTimer);
// lcd.print(" ");
previousMillis = currentMillis;
}
}