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
|
// SERVO 44, 45
// DISTANCE 25
// METAL 27
// CONVEYOR 4
// MOISTURE A10
#include <Servo.h>
bool isNotIdle = false;
// Input
int distance = 25;
int metalSensor = 27;
int moisture = A10;
// Output
Servo servo1, servo2;
int conveyor = 7;
// Program
enum Material {
dry = 0, wet = 1, metal = 2, none = 3, unknown = 4,
};
Material material = none;
Material prevMat;
int wetThreshold = 900;
int tickDuration = 100; // Every clock ticks in ms
int conveyorTimer;
int servoTimer;
int wetThresholdCounter;
// Duration for timer to reset. 1 : 100 ms (1 : 0,1 s)
int conveyorDuration = 200;
int servoDuration = 70;
int wetDuration = 7;
// Counting
int total;
int metalPassed;
int wetPassed;
int dryPassed;
// Servo positions
int servo1Init = 40;
int servo1On = 5;
int servo2Init = 60;
int servo2On = 10;
// Turning off conveyor after servoTime == -1
bool justTurnedOff = true;
void setup() {
pinMode(metalSensor, INPUT_PULLUP); digitalWrite(metal, HIGH);
pinMode(moisture, INPUT);
pinMode(distance, INPUT);
pinMode(conveyor, OUTPUT);
servo1.attach(44); servo2.attach(45);
servo1.write(servo1Init); servo2.write(servo2Init);
Serial.begin(9600);
}
void loop() {
int distanceState = !digitalRead(distance);
int metalState = !digitalRead(metalSensor);
int moistureState = analogRead(moisture);
// Checking Logic
if (distanceState) {
conveyorTimer = conveyorDuration;
}
if (metalState) {
servoTimer = servoDuration;
material = metal;
}
if (moistureState <= wetThreshold && material != wet) {
wetThresholdCounter++;
if (wetThresholdCounter >= wetDuration) {
servoTimer = servoDuration;
material = wet;
}
//servoTimer = servoDuration;
//material = wet;
}
// Timer & Logics
if (conveyorTimer >= 0) {
conveyorTimer--;
if (conveyorTimer == conveyorDuration - 1) {
justTurnedOff = false;
}
if (conveyorTimer == 0) {
if (material == none || material == unknown || prevMat == none) {
material = dry;
}
}
} else {
material = none;
}
if (servoTimer >= 0) {
servoTimer--;
if (servoTimer == servoDuration) {
servo1.write(servo1Init);
servo2.write(servo2Init);
}
if (material == metal) {
servo1.write(servo1On);
} else if (material == wet) {
servo2.write(servo2On);
}
} else {
servo1.write(servo1Init);
servo2.write(servo2Init);
if (!justTurnedOff && material != none) {
conveyorTimer = -1;
wetThresholdCounter = 0;
justTurnedOff = true;
}
}
digitalWrite(conveyor, conveyorTimer >= 0);
// Counting
if (prevMat != material) {
if (material == metal) {
metalPassed++;
} else if (material == wet) {
wetPassed++;
} else if (material == dry) {
dryPassed++;
}
total = metalPassed + wetPassed + dryPassed;
prevMat = material;
}
// On or off?
if (conveyorTimer >= 0) {
isNotIdle = true;
} else {
isNotIdle = false;
}
// Debugging
Serial.print("{\"running\":");
Serial.print(isNotIdle);
Serial.print(",\"material\":");
Serial.print(material);
Serial.print(",\"countTotal\":");
Serial.print(total);
Serial.print(",\"countMetal\":");
Serial.print(metalPassed);
Serial.print(",\"countWet\":");
Serial.print(wetPassed);
Serial.print(",\"countDry\":");
Serial.print(dryPassed);
Serial.print(",\"conveyorTimer\":");
Serial.print(conveyorTimer);
Serial.print(",\"servoTimer\":");
Serial.print(servoTimer);
Serial.print(",\"wetTimer\":");
Serial.print(wetThresholdCounter);
Serial.print(",\"distance\":");
Serial.print(distanceState);
Serial.print(",\"moisture\":");
Serial.print(moistureState);
Serial.print(",\"metal\":");
Serial.print(metalState);
Serial.println("}");/*
Serial.print("Distance ");
Serial.print(distanceState);
Serial.print(" Metal ");
Serial.print(digitalRead(27));
Serial.print(" Moist ");
Serial.print(moistureState);
Serial.print(" | Conveyor Timer ");
Serial.print(conveyorTimer);
Serial.print(" Servo Timer ");
Serial.print(servoTimer);
Serial.print(" Wet Timer ");
Serial.println(wetThresholdTimer);*/
delay(tickDuration);
}
|