Continuing the discussion from The easiest way to control a multiplexed 7-segment display unit!:
sevseg kamu sama dengan aku namun saya susah mengatur dp atau desimal nya bahkan program saat ini entah kenapa desimal nya tampil semua ,ada yang bisa membantu saya
#include <SevSeg.h>
SevSeg sevseg;
// ================= PIN =================
#define STEP_PIN A5
#define DIR_PIN 0
#define BUZZER A4
#define BTN_DEC_SPEED 2
#define BTN_INC_SPEED A2
#define BTN_FWD A3
#define BTN_REV A7
#define BTN_STOP A6
#define BTN_MAX_SPEED 3
// ================= SPEED DATA =================
float speedOptions[] = {
0.01, 0.02, 0.05,
0.1, 0.2, 0.5,
1.0, 2.0, 5.0,
10.0, 18.0
};
const int pulseDelays[] = {
1200, 1000, 800, 600, 400,
200, 100, 50, 20, 15, 10
};
int currentSpeedIndex = 6;
int pulseDelay = pulseDelays[currentSpeedIndex];
// ================= MOTOR =================
bool motorRun = false;
bool lastStepState = false;
unsigned long lastStepTime = 0;
// ================= BUTTON =================
bool lastBtnInc = LOW, lastBtnDec = LOW, lastBtnFwd = LOW;
bool lastBtnRev = LOW, lastBtnStop = LOW, lastBtnMax = LOW;
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 50;
// ================= BUZZER =================
bool beepActive = false;
unsigned long beepStart = 0;
// ===================================================
void showSpeed(float v) {
sevseg.blank();
if (v < 0.1) {
sevseg.setNumber((int)(v * 100 + 0.5), 2); // 0.01
}
else if (v < 1.0) {
sevseg.setNumber((int)(v * 10 + 0.5), 1); // 0.1
}
else if (v < 10.0) {
sevseg.setNumber((int)(v * 10 + 0.5), 1); // 1.0
}
else {
sevseg.setNumber((int)(v * 10 + 0.5), 1); // 10.0
}
}
// ===================================================
void startBeep() {
beepActive = true;
beepStart = millis();
digitalWrite(BUZZER, HIGH);
}
void updateBeep() {
if (beepActive && millis() - beepStart > 200) {
digitalWrite(BUZZER, LOW);
beepActive = false;
}
}
// ===================================================
void checkButtons() {
if (millis() - lastDebounceTime < debounceDelay) return;
bool inc = digitalRead(BTN_INC_SPEED);
bool dec = digitalRead(BTN_DEC_SPEED);
bool fwd = digitalRead(BTN_FWD);
bool maxs = digitalRead(BTN_MAX_SPEED);
bool stop = analogRead(BTN_STOP) > 500;
bool rev = analogRead(BTN_REV) > 500;
if (inc && !lastBtnInc && currentSpeedIndex < 10) {
currentSpeedIndex++;
pulseDelay = pulseDelays[currentSpeedIndex];
showSpeed(speedOptions[currentSpeedIndex]);
startBeep();
}
if (dec && !lastBtnDec && currentSpeedIndex > 0) {
currentSpeedIndex--;
pulseDelay = pulseDelays[currentSpeedIndex];
showSpeed(speedOptions[currentSpeedIndex]);
startBeep();
}
if (maxs && !lastBtnMax) {
currentSpeedIndex = 10;
pulseDelay = pulseDelays[10];
showSpeed(speedOptions[10]);
startBeep();
}
if (fwd && !lastBtnFwd) {
digitalWrite(DIR_PIN, LOW);
motorRun = true;
startBeep();
}
if (rev && !lastBtnRev) {
digitalWrite(DIR_PIN, HIGH);
motorRun = true;
startBeep();
}
if (stop && !lastBtnStop) {
motorRun = false;
startBeep();
}
lastBtnInc = inc;
lastBtnDec = dec;
lastBtnFwd = fwd;
lastBtnRev = rev;
lastBtnStop = stop;
lastBtnMax = maxs;
lastDebounceTime = millis();
}
// ===================================================
void stepMotor() {
if (!motorRun) return;
if (micros() - lastStepTime >= pulseDelay) {
lastStepTime = micros();
lastStepState = !lastStepState;
digitalWrite(STEP_PIN, lastStepState);
}
}
// ===================================================
void setup() {
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
pinMode(BUZZER, OUTPUT);
pinMode(BTN_DEC_SPEED, INPUT);
pinMode(BTN_INC_SPEED, INPUT);
pinMode(BTN_FWD, INPUT);
pinMode(BTN_REV, INPUT);
pinMode(BTN_STOP, INPUT);
pinMode(BTN_MAX_SPEED, INPUT);
byte numDigits = 4;
byte digitPins[] = {9, 10, 11, 12}; // lewat NPN
byte segmentPins[] = {A0, A1, 4, 5, 6, 7, 8, 13}; // a b c d e f g dp
sevseg.begin(
COMMON_ANODE,
numDigits,
digitPins,
segmentPins,
true, // resistor di segment
false,
false,
false // DP AKTIF
);
sevseg.setBrightness(90);
}
// ===================================================
void loop() {
checkButtons();
stepMotor();
sevseg.refreshDisplay(); // WAJIB
updateBeep();
}
