-
Notifications
You must be signed in to change notification settings - Fork 0
/
coinsorter.ino
153 lines (123 loc) · 3.66 KB
/
coinsorter.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* To do:
* - use direct port manipulation for coin detection, enables for simultaneous readings, saves time and asm lines
* - change LCD 2 modes to 9 modes, probably reducing the amount of LCD comms
* - some performance metrics testing
* - clean the code
*/
/*
*
*DDRD = B11111110; // sets Arduino pins 1 to 7 as outputs, pin 0 as input
DDRD = DDRD | B11111100; // this is safer as it sets pins 2 to 7 as outputs
// without changing the value of pins 0 & 1, which are RX & TX
*/
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 16, 2);
// A4 - sda, A5 - clk
const int IRinput[] = {0, 1, 2, 3, 4, 5, 6, 7};
const float coinvalue[] = {0.01, 0.02, 0.05, 0.10, 0.20, 0.5, 1, 2};
float num = 1.34;
char total[32] = "";
int ctrl = 0;
//int b_pres = 0;
int i = 0;
/****************/
/* button stuff */
/****************/
const int buttonPin = A0; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int lcdState = LOW; // the current state of the LCD mode
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 70; // the debounce time; increase if the output flickers
int reading; // actual reading
/*******************/
/* mode ctrl stuff */
/*******************/
unsigned long runtime;
unsigned long msTime;
unsigned long mode;
void setup() {
Serial.begin(9600);
for(i = 0; i < 4; i++) pinMode(IRinput[i], INPUT);
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
lcd.init();
lcd.backlight();
lcd.print("Total acumulado:");
}
void loop() {
print_total_to_lcd(num, lcdState);
ctrl = 0;
while(ctrl == 0){
for(i=0;i<4;i++){
if(digitalRead(IRinput[i]) == HIGH){
delay(100);
if(digitalRead(IRinput[i]) == HIGH){
Serial.println("Detection1");
ctrl = 1;
break;
}
}
}
}
while(digitalRead(IRinput[i]) == HIGH);
//Serial.println("Detection2");
num+=coinvalue[i];
// button debounce
reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
Serial.println("toggle");
lcdState = !lcdState;
msTime = millis();
}
}
}
lastButtonState = reading;
//Serial.println(lcdState);
}
void print_total_to_lcd(float num, int state){
if(state){
runtime = millis() - msTime;
mode = (runtime>>10) & 7; // magic shit, roll over 8 modes (7) for 1 sec each (<<10)
lcd.setCursor(0, 1);
switch(mode){
case 0:
lcd.print("0.01 -> x2");
break;
case 1:
lcd.print("0.02 -> x1");
break;
case 2:
lcd.print("0.05 -> x2");
break;
case 3:
lcd.print("0.10 -> x2");
break;
case 4:
lcd.print("0.20 -> x0");
break;
case 5:
lcd.print("0.50 -> x2");
break;
case 6:
lcd.print("1.00 -> x0");
break;
case 7:
lcd.print("2.00 -> x0");
break;
}
} else {
dtostrf(num, 0, 2, total);
lcd.setCursor(0, 1);
lcd.print(total);
lcd.print(" euros");
}
}