// กำหนดขาที่เชื่อมต่อกับ LED ทั้ง 5 หลอด (สำหรับ ESP32)
// ตรวจสอบให้แน่ใจว่าขาเหล่านี้เหมาะสมกับการใช้งาน LED ของคุณ
const int ledPins[5] = {23, 22, 19, 18, 5};
// กำหนดขาที่เชื่อมต่อกับสวิตช์ทั้ง 3 ตัว (สำหรับ ESP32)
// D0 คือ GPIO0, D1 คือ GPIO1 (ปกติใช้ในการสื่อสาร USB/Serial ระวังการชนกัน), D15 คือ GPIO15
const int switch1Pin = 0; // GPIO0 (มักเป็น D0 บน ESP32 dev boards)
const int switch2Pin = 2; // GPIO1 (มักเป็น D1 บน ESP32 dev boards - อาจมีปัญหากับ Serial Monitor)
const int switch3Pin = 15; // GPIO15 (มักเป็น D15 บน ESP32 dev boards)
// กำหนด pattern แต่ละรูปแบบ: [รูปแบบ][บรรทัด][หลอด]
const byte patterns[3][8][5] = {
{ // รูปแบบที่ 0 (ซ้ายบน)
{1, 1, 1, 1, 1},
{1, 1, 0, 1, 1},
{1, 0, 0, 0, 1},
{0, 0, 0, 0, 0},
{1, 0, 0, 0, 1},
{1, 1, 0, 1, 1},
{1, 1, 1, 1, 1},
{0, 0, 0, 0, 0}
},
{ // รูปแบบที่ 1 (ขวาบน)
{1, 0, 0, 0, 0},
{1, 1, 0, 0, 0},
{1, 1, 1, 0, 0},
{1, 1, 1, 1, 0},
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0}
},
{ // รูปแบบที่ 2 (ขวาล่าง)
{0, 0, 0, 0, 1},
{0, 0, 0, 1, 1},
{0, 0, 1, 1, 1},
{0, 1, 1, 1, 1},
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0}
}
};
// ตัวแปรสำหรับเก็บสถานะปัจจุบัน
int currentPattern = -1; // เริ่มต้นที่ -1 เพื่อระบุว่ายังไม่มี Pattern ใดทำงาน
int currentLine = 0;
// ตัวแปรสำหรับ Debouncing สวิตช์
unsigned long lastSwitchPressTime[3] = {0, 0, 0};
const unsigned long debounceDelay = 50; // 50 มิลลิวินาที
// ตัวแปรสำหรับสถานะการทำงานของแต่ละ Pattern (เปิด/ปิด)
bool patternActive[3] = {false, false, false}; // เริ่มต้นทุก Pattern เป็น False (หยุดทำงาน)
// ตัวแปรสำหรับจับเวลาการแสดงผลอัตโนมัติ (เมื่อ Pattern ทำงาน)
unsigned long lastUpdateTime = 0;
const unsigned long displayInterval = 200; // ความเร็วในการเลื่อนบรรทัด 200 มิลลิวินาที
void setup() {
// กำหนดให้ขา LED เป็น OUTPUT
for (int i = 0; i < 5; i++) {
pinMode(ledPins[i], OUTPUT);
}
// กำหนดให้ขาสวิตช์เป็น INPUT_PULLUP
// GPIO0 (D0) และ GPIO1 (D1) เป็นขาที่สำคัญในการบูต ESP32
// การใช้ INPUT_PULLUP ควรระมัดระวังเป็นพิเศษ และอาจต้องลองสลับขาถ้ามีปัญหา
pinMode(switch1Pin, INPUT_PULLUP);
pinMode(switch2Pin, INPUT_PULLUP);
pinMode(switch3Pin, INPUT_PULLUP);
// เริ่มต้นให้ LED ดับทั้งหมด
turnAllLedsOff();
}
void loop() {
// ตรวจสอบสถานะของสวิตช์
checkSwitches();
// ถ้ามี Pattern ใดกำลังทำงานอยู่ ให้เลื่อนบรรทัดอัตโนมัติ
if (currentPattern != -1 && patternActive[currentPattern]) {
if (millis() - lastUpdateTime >= displayInterval) {
lastUpdateTime = millis();
currentLine++;
if (currentLine >= 8) {
currentLine = 0; // วนกลับไปบรรทัดแรกเมื่อถึงบรรทัดสุดท้าย
}
displayCurrentLine(); // แสดงผลบรรทัดใหม่
}
}
}
//ฟังก์ชันสำหรับตรวจสอบสถานะสวิตช์และควบคุม Pattern
void checkSwitches() {
// ตรวจสอบสวิตช์ 1 (ควบคุม Pattern 0)
if (digitalRead(switch1Pin) == LOW) { // สวิตช์ถูกกด
if (millis() - lastSwitchPressTime[0] > debounceDelay) {
lastSwitchPressTime[0] = millis(); // บันทึกเวลาที่กด
if (currentPattern == 0) { // ถ้า Pattern 0 กำลังถูกเลือกอยู่
patternActive[0] = !patternActive[0]; // สลับสถานะ (เปิด -> ปิด, ปิด -> เปิด)
if (!patternActive[0]) { // ถ้าเพิ่งเปลี่ยนเป็นหยุดทำงาน
currentPattern = -1; // ไม่มี Pattern ใดทำงาน
turnAllLedsOff(); // ดับ LED ทั้งหมด
}
} else { // ถ้าไม่ใช่ Pattern 0 ที่กำลังทำงานอยู่
// ปิด Pattern ที่เคยทำงานอยู่ก่อนหน้านี้
if (currentPattern != -1) {
patternActive[currentPattern] = false;
}
currentPattern = 0; // เปลี่ยนไป Pattern 0
currentLine = 0; // เริ่มจากบรรทัดแรก
patternActive[0] = true; // ตั้งให้ Pattern 0 เริ่มทำงาน
displayCurrentLine(); // แสดงผลทันที
lastUpdateTime = millis(); // รีเซ็ตเวลาสำหรับ Pattern ใหม่
}
}
}
// ตรวจสอบสวิตช์ 2 (ควบคุม Pattern 1)
if (digitalRead(switch2Pin) == LOW) {
if (millis() - lastSwitchPressTime[1] > debounceDelay) {
lastSwitchPressTime[1] = millis();
if (currentPattern == 1) {
patternActive[1] = !patternActive[1];
if (!patternActive[1]) {
currentPattern = -1;
turnAllLedsOff();
}
} else {
if (currentPattern != -1) {
patternActive[currentPattern] = false;
}
currentPattern = 1;
currentLine = 0;
patternActive[1] = true;
displayCurrentLine();
lastUpdateTime = millis();
}
}
}
// ตรวจสอบสวิตช์ 3 (ควบคุม Pattern 2)
if (digitalRead(switch3Pin) == LOW) {
if (millis() - lastSwitchPressTime[2] > debounceDelay) {
lastSwitchPressTime[2] = millis();
if (currentPattern == 2) {
patternActive[2] = !patternActive[2];
if (!patternActive[2]) {
currentPattern = -1;
turnAllLedsOff();
}
} else {
if (currentPattern != -1) {
patternActive[currentPattern] = false;
}
currentPattern = 2;
currentLine = 0;
patternActive[2] = true;
displayCurrentLine();
lastUpdateTime = millis();
}
}
}
}
//ฟังก์ชันสำหรับแสดงผล LED ตามรูปแบบและบรรทัดปัจจุบัน
void displayCurrentLine() {
for (int i = 0; i < 5; i++) {
digitalWrite(ledPins[i], patterns[currentPattern][currentLine][i] ? HIGH : LOW);
}
}
//ฟังก์ชันสำหรับดับ LED ทั้งหมด
void turnAllLedsOff() {
for (int i = 0; i < 5; i++) {
digitalWrite(ledPins[i], LOW);
}
}Loading
esp32-devkit-c-v4
esp32-devkit-c-v4