-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlcd_ER_button.ino
More file actions
207 lines (187 loc) · 4.72 KB
/
lcd_ER_button.ino
File metadata and controls
207 lines (187 loc) · 4.72 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <Servo.h> // include Servo Motor Library
#include <LiquidCrystal.h>
/* RS pin -> 14 A0
enable pin -> 15 A1
D4-D7 -> 16-19 A2-A5
*/
LiquidCrystal lcd(14, 15, 16, 17, 18, 19);
//emergency stop button
int ES = 2;
int buzz = 12; // sound for emergency stop
int signallight = 13;
//global variables
boolean start = true;
volatile boolean e_stop = false;
volatile int state = 0;
// define colour sensor
#define S0 4
#define S1 5
#define S2 6
#define S3 7
#define sensorOut A7
// define servo motors (top and bottom)
Servo top;
Servo bottom;
// variables for counters and frequency
int freq = 0;
int colour = 0;
int count_red = 0;
int count_orange = 0;
int count_green = 0;
int count_yellow = 0;
int count_blue = 0;
int count_brown = 0;
int count_other = 0;
int total = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
//emergency button
pinMode(ES, INPUT_PULLUP);
pinMode(signallight, OUTPUT);
delay(100);
//colour sensor
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(sensorOut, OUTPUT);
//set frequency scale to 20%
digitalWrite(S0, HIGH);
digitalWrite(S1, LOW);
//servo motors
top.attach(9); // connect to arduino D9
bottom.attach(10); // connect to arduino D10
//lcd display
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("START");
lcd.setCursor(0, 1);
lcd.print("LOADING...");
delay(2500);
}
void loop() {
if (start == true) {
if (e_stop == false) {
lcd.clear();
lcd.print("START SORTING....");
top.write(75);
delay(500);
colour = readColour();
delay(10);
//if (Serial.available() > 0) {
top.write(30);
delay(2000);
// int input = Serial.read();
switch (colour) {
case '1':
Serial.println("red");
top.write(0);
bottom.write(0);
delay(500);
count_red += 1; // counter
total += 1;
//lcd display prints data
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Colour Identify:");
lcd.setCursor(0, 1);
lcd.print("RED");
break;
case '2':
Serial.println("orange");
count_orange += 1; //counter
total += 1;
// servo motor send candy to container
bottom.write(25);
//lcd display prints data
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Colour Identify:");
lcd.setCursor(0, 1);
lcd.print("ORANGE");
break;
case '3':
Serial.println("green");
count_green += 1; //counter
total += 1;
// servo motor send candy to container
bottom.write(50);
//lcd display prints data
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Colour Identify:");
lcd.setCursor(0, 1);
lcd.print("GREEN");
break;
case '4':
Serial.println("yellow");
count_yellow += 1; //counter
total += 1;
// servo motor send candy to container
bottom.write(75);
//lcd display prints data
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Colour Identify:");
lcd.setCursor(0, 1);
lcd.print("YELLOW");
break;
case '5':
Serial.println("brown");
count_brown += 1; //counter
total += 1;
// servo motor send candy to container
bottom.write(100);
//lcd display prints data
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Colour Identify:");
lcd.setCursor(0, 1);
lcd.print("BROWN");
break;
case '6':
Serial.println("blue");
count_blue += 1; //counter
total += 1;
// servo motor send candy to container
bottom.write(125);
//lcd display prints data
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Colour Identify:");
lcd.setCursor(0, 1);
lcd.print("BLUE");
break;
case '0':
top.write(0);
delay(500);
Serial.println("OUT");
count_other += 1; //counter
total += 1;
bottom.write(150);
Serial.println("OUT OF RANGE");
//lcd display prints data
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Colour Identify:");
lcd.setCursor(0, 1);
lcd.print("OUT OF RANGE");
break;
}
delay(1000);
}
}
else {
start = false;
}
}
int readColour() {
colour = 1;
colour = 3;
colour = 4;
colour = 0;
colour = 2;
colour = 5;
return colour;
}