#include <TM1637Display.h>
// === TM1637 pin ===
#define CLK PA5
#define DIO PA6
// === Encoder pin ===
#define ENC_CLK PB10
#define ENC_DT PB11
// === Tombol ===
#define BTN_PLUS PA0 // tombol "+"
#define BTN_UNIT PA1 // tombol ganti satuan
#define BTN_MINUS PA2 // tombol "–"
// === Setup TM1637 ===
TM1637Display display(CLK, DIO);
// === Variabel ===
volatile long encoderValue = 0;
int lastStateCLK;
unsigned long lastButtonPress = 0;
int unitMode = 0; // 0=RPM, 1=RPS, 2=cm/s, 3=m/s, 4=m/m
// === Fungsi tampil sesuai mode ===
void showValue(long value) {
switch (unitMode) {
case 0: display.showNumberDec(value); break; // RPM
case 1: display.showNumberDec(value / 60); break; // RPS
case 2: display.showNumberDec(value * 2); break; // cm/s (contoh konversi)
case 3: display.showNumberDec(value / 100); break; // m/s
case 4: display.showNumberDec(value * 10); break; // m/m
}
}
void setup() {
pinMode(ENC_CLK, INPUT);
pinMode(ENC_DT, INPUT);
pinMode(BTN_PLUS, INPUT_PULLUP);
pinMode(BTN_UNIT, INPUT_PULLUP);
pinMode(BTN_MINUS, INPUT_PULLUP);
display.setBrightness(7);
display.showNumberDec(0);
lastStateCLK = digitalRead(ENC_CLK);
}
void loop() {
int currentStateCLK = digitalRead(ENC_CLK);
// Deteksi putaran encoder
if (currentStateCLK != lastStateCLK && currentStateCLK == HIGH) {
if (digitalRead(ENC_DT) != currentStateCLK) {
encoderValue++;
} else {
encoderValue--;
}
if (encoderValue < 0) encoderValue = 0;
if (encoderValue > 9999) encoderValue = 9999;
showValue(encoderValue);
}
lastStateCLK = currentStateCLK;
// Tombol "+" ditekan
if (digitalRead(BTN_PLUS) == LOW) {
encoderValue += 10;
if (encoderValue > 9999) encoderValue = 9999;
showValue(encoderValue);
delay(150);
}
// Tombol "–" ditekan
if (digitalRead(BTN_MINUS) == LOW) {
encoderValue -= 10;
if (encoderValue < 0) encoderValue = 0;
showValue(encoderValue);
delay(150);
}
// Tombol "Unit" ditekan
if (digitalRead(BTN_UNIT) == LOW && millis() - lastButtonPress > 300) {
unitMode++;
if (unitMode > 4) unitMode = 0;
lastButtonPress = millis();
showValue(encoderValue);
}
}