1 hinzugefügt

This commit is contained in:
sans_red 2026-02-11 22:45:15 +07:00
commit 9d2f303415
1 changed files with 52 additions and 0 deletions

52
1 Normal file
View File

@ -0,0 +1,52 @@
// --- กำหนดขาอุปกรณ์ ---
const int pinX = A1;
const int pinY = A0;
const int pinMG = 2;
const int pinBomb = 3;
const int pinSafety = 4;
void setup() {
Serial.begin(9600);
pinMode(pinMG, INPUT_PULLUP);
pinMode(pinBomb, INPUT_PULLUP);
pinMode(pinSafety, INPUT_PULLUP);
}
void loop() {
// 1. ตรวจสอบสวิตช์เปิด-ปิด (Safety)
// ถ้า D4 ต่อลง GND (LOW) คือระบบทำงาน
if (digitalRead(pinSafety) == LOW) {
// 2. อ่านค่าคันโยกและแปลงช่วงเป็น -90 ถึง 90
int posX = map(analogRead(pinX), 0, 1023, 90, -90);
int posY = map(analogRead(pinY), 0, 1023, -90, 90);//S/W
// 3. ทำ Deadzone: ถ้าอยู่ระหว่าง -1 ถึง 1 ให้เป็น 0
if (posX >= -1 && posX <= 1) posX = 0;
if (posY >= -1 && posY <= 1) posY = 0;
// 4. แสดงผลแกน X (90L / 90R)
Serial.print("X: ");
if (posX > 0) { Serial.print(posX); Serial.print("R"); }//D
else if (posX < 0) { Serial.print(abs(posX)); Serial.print("L");}//A
else { Serial.print("0"); }
// 5. แสดงผลแกน Y และปุ่ม
Serial.print(" | Y: "); Serial.print(posY);
// อ่านปุ่ม: ถ้ากด (LOW) ให้โชว์ 1, ถ้าไม่กดให้โชว์ 0
int mg = !digitalRead(pinMG);
int bomb = !digitalRead(pinBomb);
if (mg) Serial.print(" | [MG FIRE!] ");
if (bomb) Serial.print(" | [BOMB DROP!] ");
Serial.println(); // ขึ้นบรรทัดใหม่
} else {
// ถ้าสวิตช์ปิดอยู่ (Safety On)
Serial.println("SYSTEM OFF (Safety Engaged)");
}
delay(50); // ความไวในการส่งข้อมูล
}