Skip to content

Instantly share code, notes, and snippets.

@maxpromer
Last active April 12, 2024 10:31
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/aaece92b1fa6d4411792ceac7cf0b87a to your computer and use it in GitHub Desktop.
Save maxpromer/aaece92b1fa6d4411792ceac7cf0b87a to your computer and use it in GitHub Desktop.
โค้ด Arduino เครื่องรับเหรียญ เครื่องหยอดเหรียญ แบบใช้เหรียญต้นแบบ CL-1006A : https://www.artronshop.co.th/p/594
/* ArtronShop : www.ArtronShop.co.th */
#define SIGNAL_PIN (2) // ขาที่ต่อเครื่องรับเหรียญ
#define COIN_VALUE (10) // กำหนดเหรียญที่ใส่ในเครื่อง เพื่อให้คำนวณยอดเงินถูกต้อง
int count = 0; // ตัวแปรนับ Pulse ที่เครื่องรับเหรียญส่งเข้ามา
bool count_update_flag = false; // ตัวแปรเก็บค่าว่า count อัพเดทแล้ว
void setup() {
Serial.begin(9600);
pinMode(SIGNAL_PIN, INPUT_PULLUP); // กำหนดขาที่ต่อเครื่องรับเหรียญเป็นอินพุตพร้อมเปิดใช้วงจร Pull-up ภายใน
attachInterrupt(digitalPinToInterrupt(SIGNAL_PIN), pulse_in_cb, FALLING); // เปิดใช้อินเตอร์รัพท์ภายนอก
}
void loop() {
if (count_update_flag) {
count_update_flag = false;
int net = count * COIN_VALUE; // คำนวณหายอดเงินรวม โดยนำค่า count คูณมูลค่าเหรียญ
Serial.print("Counter = ");
Serial.print(count);
Serial.print("\tNET = ฿");
Serial.print(net);
Serial.println();
}
delay(10);
}
void pulse_in_cb() { // เมื่อได้รับ Pulse
count++; // เพิ่มค่าในตัวแปร count ขึ้น 1 ค่า
count_update_flag = true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment