-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbill_feed-dispencer.ino
187 lines (155 loc) · 3.51 KB
/
bill_feed-dispencer.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//https://github.com/zethra/aquqbot2
#include <Servo.h>
//#include <toneAC.h>
#include "pitches.h"
#define READY 0
#define GETTING 1
#define DISPENSING 2
#define DONE 3
#define IRPIN 23
#define PHOTOPIN 22
#define BILLPIN 24
#define SERVOPIN 26
#define LEDPIN 6
Servo servo;
int ledState = 0;
int irState = 0;
int photoState = 0;
int billPower = 0;
int servoPos = 45;
int servoState = 0;
int servoWait = 0;
int state = 0;
char* actionText;
void setup(){
Serial.begin(9600);
Serial.println("Running Setup");
servo.attach(SERVOPIN);
//Inputs
pinMode(IRPIN, INPUT);
pinMode(PHOTOPIN, INPUT);
//Outputs
pinMode(BILLPIN, OUTPUT);
pinMode(LEDPIN, OUTPUT);
}
void loop(){
unsigned long now = millis();
sensorUpdate(now);
motorUpdate(now);
ledUpdate(now);
printUpdate(now);
//Serial.println(state);
switch(state){
case READY:
if(irState == LOW){
state = GETTING;
}
actionText = "Ready.";
servoState = 0;
servoWait = 0;
return;
case GETTING:
billPower = 255;
if(photoState == HIGH) {
state = DISPENSING;
}
actionText = "Bill Detected";
return;
case DISPENSING:
if(photoState == HIGH){
actionText = "Waiting for Bill";
return;
}else{
billPower = 0;
dispense(now);
}
return;
case DONE:
Serial.println("Thank you and have a nice day.");
//toneAC(300);
state = READY;
return;
}
}
void sensorUpdate(unsigned long now){
static unsigned long sensorMillis = 0UL;
if(now - sensorMillis >= 10UL){
irState = digitalRead(IRPIN);
photoState = digitalRead(PHOTOPIN);
sensorMillis = now;
}
}
void motorUpdate(unsigned long now){
static unsigned long motorMillis = 0UL;
if(now - motorMillis >= 10UL){
analogWrite(BILLPIN, billPower);
servo.write(servoPos);
motorMillis = now;
}
}
void ledUpdate(unsigned long now){
static unsigned long ledMillis = 0UL;
if(now - ledMillis >= 1000UL){
if(ledState == HIGH){
ledState = LOW;
}else if(ledState == LOW) {
ledState = HIGH;
}
digitalWrite(LEDPIN, ledState);
ledMillis = now;
}
}
void printUpdate(unsigned long now){
static unsigned long printMillis = 0UL;
if(now - printMillis >= 10UL){
Serial.print("State: ");
Serial.print(state);
Serial.print(" | IR State: ");
Serial.print(irState);
Serial.print(" | Photo State: ");
Serial.print(photoState);
Serial.print(" | Motor Power: ");
Serial.print(billPower);
Serial.print(" | Servo Position: ");
Serial.print(servoPos);
Serial.print(" | Led State: ");
Serial.print(ledState);
Serial.print(" | Action: ");
Serial.print(actionText);
Serial.println();
printMillis = now;
}
}
void dispense(unsigned long now){
//for (servoPos = 5; servoPos < 85; servoPos++){}
//for (servoPos = 85; servoPos > 5; servoPos--){}
static unsigned long servoMillis = 0UL;
if(now - servoMillis >= 10UL){
sprintf(actionText, "Dispensing: ""%d", servoState);
if(servoState == 0){
if(servoPos < 135){
servoPos++;
}else{
servoState = 1;
}
}
if(servoState == 1){
if(servoWait < 40){
servoWait++;
}else{
servoState = 2;
}
}
if(servoState == 2){
if(servoPos > 45){
servoPos--;
}else{
servoState = 3;
}
}
if(servoState == 3){
state = DONE;
}
servoMillis = now;
}
}