// การกำหนดขาของ 7-segment display
const int segmentPins[] = {0, 1, 2, 3, 4, 5, 6, 7};
// ขาสำหรับปุ่ม pushbutton
const int buttonPin = 10;
int count = 0; // ตัวแปรเก็บค่าการนับ
void setup() {
// กำหนดขาของ 7-segment display เป็น OUTPUT
for (int i = 0; i < 8; i++) {
pinMode(segmentPins[i], OUTPUT);
digitalWrite(segmentPins[i], LOW); // ปิด segment ทุกตัวเริ่มต้น
}
// กำหนดขาของปุ่ม pushbutton เป็น INPUT
pinMode(buttonPin, INPUT_PULLUP);
//pinMode(resetbottonPin, INPUT_PULLUP);
displayNumber(0);
}
void loop() {
if (digitalRead(buttonPin) == LOW) { // ตรวจสอบว่าปุ่มถูกกดหรือไม่
count = (count + 1) % 10; // เพิ่มค่าการนับและรีเซ็ตเมื่อค่าเกิน 9
displayNumber(count); // แสดงค่าการนับบน 7-segment display
delay(200); // รอเพื่อป้องกันการกดปุ่มซ้ำ
}
}
}
void displayNumber(int number) {
// สร้างโค้ดส่งสัญญาณไปที่ 7-segment display
const byte digitPins[10][7] = {
{0, 0, 0, 0, 0, 0, 1}, // 0
{1, 0, 0, 1, 1, 1, 1}, // 1
{0, 0, 1, 0, 0, 1, 0}, // 2
{0, 0, 0, 0, 1, 1, 0}, // 3
{1, 0, 0, 1, 1, 0, 0}, // 4
{0, 1, 0, 0, 1, 0, 0}, // 5
{0, 1, 0, 0, 0, 0, 0}, // 6
{0, 0, 0, 1, 1, 1, 1}, // 7
{0, 0, 0, 0, 0, 0, 0}, // 8
{0, 0, 0, 0, 1, 0, 0} // 9
};
for (int i = 0; i < 7; i++) {
digitalWrite(segmentPins[i], digitPins[number][i]);
}
}