Skip to content

Instantly share code, notes, and snippets.

@maxpromer
Created April 12, 2024 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxpromer/70a0d375c0ae663fae41b78a4aebf23d to your computer and use it in GitHub Desktop.
Save maxpromer/70a0d375c0ae663fae41b78a4aebf23d to your computer and use it in GitHub Desktop.
#define SIG_PIN 4 // กำหนดขาที่ต้อ SIG
#define EN_PIN 16 // กำหนดขาที่ต่อ EN
bool count_update_flag = false; // ตัวแปรเก็บค่าว่า count อัพเดทแล้ว
void pulse_in_cb() { // เมื่อได้รับ Pulse
count_update_flag = true;
}
void setup() {
pinMode(SIG_PIN, INPUT); // กำหนดขา SIG เป็นอินพุต
attachInterrupt(digitalPinToInterrupt(SIG_PIN), pulse_in_cb, FALLING); // เปิดใช้อินเตอร์รัพท์ภายนอก
pinMode(EN_PIN, OUTPUT); // กำหนดขา EN เป็นอินพุต
digitalWrite(EN_PIN, HIGH); // สั่งให้ขา EN เป็นลอจิก 1 (HIGH) เพื่อให้รับเหรียญ
Serial.begin(115200); // ใช้ Serial ที่ความเร็ว 115200
}
void loop() {
if (count_update_flag) {
Serial.println("Got coin !!!"); // แสดงข้อความใน Serial Monitor
delay(200);
count_update_flag = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment