-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino.ino
77 lines (42 loc) · 1.59 KB
/
arduino.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
73
74
75
76
77
const int coinSelector = A0; // Analog input pin that the coin selector uses
const int signalCostFactor = 5; // Each signal pulse is worth 5p
int signalValue = 0; // For storing value from analog input
int state; // Current state
int lastState = 0; // Last state
int balance = 0; // Pence
int update = 0; // Used for updating
long updateDebounceTime = 0; // The last time we sent an update
long updateDebounceDelay = 500; // Update 500ms after last singal pulse
void setup() {
pinMode(13, OUTPUT); // Status LED
Serial.begin(9600); // Setup serial at 9600 baud
delay(2000); // Don't start main loop untill we're sure that the coin selector has started
}
void loop() {
signalValue = analogRead(coinSelector); // Read analog value from coin selector
if (signalValue > 1000) {
state = 1; // State is 1 as we're high
} else {
state = 0;
// Should we send a balance update
if (update == 0) {
if ((millis() - updateDebounceTime) > updateDebounceDelay) {
Serial.println(balance); // Send an update over serial
update = 1; // Make sure we don't run again, till next coin
}
}
}
if (state != lastState) {
// Process new signal
if (state == 1) {
digitalWrite(13, HIGH); // Turn status LED on to show signal
balance = balance + signalCostFactor; // Update balance
updateDebounceTime = millis(); // Update last time we processed a signal
update = 0; // Time to send a update now?
} else {
digitalWrite(13, LOW); // Turn status LED off
}
lastState = state; // Update last state
}
delay(1);
}