-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSM_code.ino
178 lines (155 loc) · 3.92 KB
/
CSM_code.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
#include <Servo.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64
#define S0 1
#define S1 2
#define S2 3
#define S3 4
#define sensorOut 5
Servo topServo;
Servo bottomeServo;
int frequency = 0;
int color = 0;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
Serial.begin(115200);
// Initialize OLED display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2000);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
// Display title
display.setCursor(50, 0);
display.println("Colour");
display.setCursor(50, 10);
display.println("Sorting");
display.setCursor(50, 20);
display.println("Machine");
// Display color labels
display.setTextColor(BLACK, WHITE);
display.setCursor(0, 35);
display.println("Red");
display.setCursor(50, 35);
display.println("Green");
display.setCursor(100, 35);
display.println("Blue");
display.display();
// Configure color sensor pins
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(sensorOut, INPUT);
digitalWrite(S0, HIGH);
digitalWrite(S1, HIGH);
// Attach servo motors
topServo.attach(6);
bottomeServo.attach(7);
}
void loop() {
// Initial position
topServo.write(115);
delay(500);
// Move object under sensor
for(int i = 115; i > 60; i--) {
topServo.write(i);
delay(5);
}
delay(1000);
// Read and process color
color = readColor();
delay(100);
// Sort based on color
switch(color) {
case 1:
bottomeServo.write(25);
Serial.println("RED");
break;
case 2:
bottomeServo.write(100);
Serial.println("GREEN");
break;
case 3:
bottomeServo.write(175);
Serial.println("BLUE");
break;
case 0:
break;
}
delay(1000);
// Drop object
for(int i = 60; i > 25; i--) {
topServo.write(i);
delay(5);
}
delay(200);
// Return to start position
for(int i = 25; i < 115; i++) {
topServo.write(i);
delay(5);
}
color = 0;
}
// Color detection function
int readColor() {
// Read Red
digitalWrite(S2, LOW);
digitalWrite(S3, LOW);
frequency = pulseIn(sensorOut, LOW);
int R = frequency;
Serial.print("R= ");
Serial.print(frequency);
Serial.print(" ");
delay(20);
// Read Green
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);
frequency = pulseIn(sensorOut, LOW);
int G = frequency;
Serial.print("G= ");
Serial.print(frequency);
Serial.print(" ");
delay(50);
// Read Blue
digitalWrite(S2, LOW);
digitalWrite(S3, HIGH);
frequency = pulseIn(sensorOut, LOW);
int B = frequency;
Serial.print("B= ");
Serial.print(frequency);
Serial.println(" ");
delay(50);
// Determine color
if(R < G && R < B) {
color = 1; // Red
updateDisplayCount(0, 50); // Update red count
}
else if(G < R && G < B) {
color = 2; // Green
updateDisplayCount(60, 50); // Update green count
}
else if(B < R && B < G) {
color = 3; // Blue
updateDisplayCount(105, 50); // Update blue count
}
return color;
}
// Helper function to update display count
void updateDisplayCount(int x, int y) {
for (int i = 1; i <= 20; i++) {
display.setTextColor(WHITE);
display.setCursor(x, y);
display.println(i);
display.display();
delay(2000);
display.fillRect(x, y, 20, 30, BLACK);
display.display();
}
}