-
Notifications
You must be signed in to change notification settings - Fork 16
/
coin-acceptor.ino
72 lines (54 loc) · 1.61 KB
/
coin-acceptor.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Arduino coin acceptor code by hxlnt
// Originally created for "Adding a Coin Acceptor to Your Arduino Project" on the Maker Show
// See the entire video tutorial at https://channel9.msdn.com/Shows/themakershow/10
//
// Read your coin acceptor's specs and instructions first for hookup specifics
// Modifications to this code may be needed
// Coin acceptor model used in this example is JY-923
//
// xoxox
// Constants
const int coinpin = 2;
const int ledpin = 3;
const int targetcents = 100;
// Variables
volatile int cents = 0;
int credits = 0;
// Setup
void setup() {
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(coinpin), coinInterrupt, RISING);
pinMode(ledpin, OUTPUT);
}
// Main loop
void loop() {
// If we've hit our target amount of coins, increment our credits and reset the cents counter
if (cents >= targetcents) {
credits = credits + 1;
cents = cents - targetcents;
}
// If we haven't reached our target, keep waiting...
else {
}
// Debugging zone
Serial.print(cents);
Serial.print(" cents toward current credit and ");
Serial.print(credits);
Serial.println(" credit(s) earned so far.");
delay(1000);
// Turn off LED
digitalWrite(ledpin, LOW);
// Now, write your own cool code here that triggers an event when the player has credits!
if (credits > 0) {
// Play music?
// Spin up a motor?
// Start a game?
// It's up to you!
}
}
// Interrupt
void coinInterrupt(){
// Each time a pulse is sent from the coin acceptor, interrupt main loop to add 1 cent and flip on the LED
cents = cents + 1;
digitalWrite(ledpin, HIGH);
}