const int latchPinLED = 21; // Latch pin ของ LEDs
const int latchPin7Seg = 19; // Latch pin ของ 7-segment
const int clockPin = 18; // Clock pin ใช้ร่วมกัน
const int dataPinLED1 = 23; // Data pin สำหรับ LEDs
const int dataPinLED2 = 33; // Data pin สำหรับ LEDs
const int dataPin7Seg = 22; // Data pin สำหรับ 7-segment
byte leds1 = 0;
byte leds2 = 0;
const int buttonUp = 25;
const int buttonDown = 26;
const int buttonSet = 4;
const int ledPin = 13;
bool ledState = false;
bool runningPattern = false;
int patternIndex = 0;
unsigned long lastUpdate = 0;
const unsigned long patternDelay = 500;
const byte digitPatterns[12] = {
0b10011100, // C
0b01100000, // 1
0b11011010, // 2
0b11110010, // 3
0b01100110, // 4
0b10110110, // 5
0b10111110, // 6
0b11100000, // 7
0b11111110, // 8
0b11110110, // 9
0b11101110, // A
0b00111110, // B
};
int counter = 1;
bool paused = false;
void setup() {
pinMode(dataPinLED1, OUTPUT);
pinMode(dataPinLED2, OUTPUT);
pinMode(dataPin7Seg, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPinLED, OUTPUT);
pinMode(latchPin7Seg, OUTPUT);
pinMode(buttonUp, INPUT_PULLUP);
pinMode(buttonDown, INPUT_PULLUP);
pinMode(buttonSet, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
showDigit(counter);
}
void loop() {
// ตรวจสอบการกดปุ่ม buttonSet
if (digitalRead(buttonSet) == LOW) {
delay(50); // debounce
while (digitalRead(buttonSet) == LOW); // wait for release
delay(50);
// ถ้า ledPin = HIGH → รีเซ็ตทุกอย่าง (ยกเว้นเลขบน 7-segment)
if (ledState == true) {
leds1 = 0;
leds2 = 0;
runningPattern = false;
patternIndex = 0;
paused = false;
updateShiftRegisterLEDs(); // ปิด LED ทั้งหมด
digitalWrite(ledPin, LOW); // ปิด LED pin 13
ledState = false; // รีเซ็ตสถานะ LED
} else {
// toggle LED pin ตามปกติ
ledState = !ledState;
digitalWrite(ledPin, ledState ? HIGH : LOW);
}
delay(300); // หน่วงปุ่ม
}
if (counter == 1 && ledState == HIGH && !runningPattern) {
runningPattern = true;
patternIndex = 0;
leds1 = 0;
updateShiftRegisterLEDs();
lastUpdate = millis();
}
if (counter == 2 && ledState == HIGH && !runningPattern) {
runningPattern = true;
patternIndex = 0;
leds2 = 0;
updateShiftRegisterLEDs();
lastUpdate = millis();
}
if (runningPattern) {
if (millis() - lastUpdate >= patternDelay) {
if (counter == 1) {
bitSet(leds1, patternIndex);
} else if (counter == 2) {
bitSet(leds2, patternIndex);
}
updateShiftRegisterLEDs();
patternIndex++;
lastUpdate = millis();
if (patternIndex >= 8) {
runningPattern = false;
}
}
}
if(ledState == LOW){
if (digitalRead(buttonUp) == LOW) {
counter++;
if (counter > 12) counter = 1;
showDigit(counter);
delay(500);
}
if (digitalRead(buttonDown) == LOW) {
counter--;
if (counter < 1) counter = 12;
showDigit(counter);
delay(500);
}
}
}
void updateShiftRegisterLEDs() {
if (counter == 1) {
digitalWrite(latchPinLED, LOW);
shiftOut(dataPinLED1, clockPin, LSBFIRST, leds1);
digitalWrite(latchPinLED, HIGH);
}
if (counter == 2) {
digitalWrite(latchPinLED, LOW);
shiftOut(dataPinLED2, clockPin, LSBFIRST, leds2);
digitalWrite(latchPinLED, HIGH);
}
}
void showDigit(int num) {
byte pattern;
if (num >= 1 && num <= 11) {
pattern = digitPatterns[num];
} else if (num == 12) {
pattern = digitPatterns[0];
} else {
pattern = 0;
}
digitalWrite(latchPin7Seg, LOW);
shiftOut(dataPin7Seg, clockPin, MSBFIRST, pattern);
digitalWrite(latchPin7Seg, HIGH);
}