Skip to content

Instantly share code, notes, and snippets.

@maxpromer
Last active April 12, 2024 10:43
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/ef17b28e0debac917ab5c24be6c38e01 to your computer and use it in GitHub Desktop.
Save maxpromer/ef17b28e0debac917ab5c24be6c38e01 to your computer and use it in GitHub Desktop.
#define SIG_PIN 4 // กำหนดขาที่ต้อ SIG
#define EN_PIN 16 // กำหนดขาที่ต่อ EN
#define COIN_VALUE 10 // กำหนดเหรียญที่ใส่ในเครื่อง เพื่อให้คำนวณยอดเงินถูกต้อง
int count = 0; // ตัวแปรนับ Pulse ที่เครื่องรับเหรียญส่งเข้ามา
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) {
count++; // เพิ่มค่าในตัวแปร count ขึ้น 1 ค่า
int net = count * COIN_VALUE; // คำนวณหายอดเงินรวม โดยนำค่า count คูณมูลค่าเหรียญ
Serial.print("Counter = ");
Serial.print(count);
Serial.print("\tNET = ฿");
Serial.print(net);
Serial.println();
delay(500);
count_update_flag = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment