-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ino
More file actions
191 lines (157 loc) · 4.3 KB
/
main.ino
File metadata and controls
191 lines (157 loc) · 4.3 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
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>
hd44780_I2Cexp lcd;
const int LCD_COLS = 16;
const int LCD_ROWS = 2;
#define S0 10
#define S1 11
#define S2 12
#define S3 13
#define sensorOut 14
int redMin = 55;
int redMax = 720;
int greenMin = 65;
int greenMax = 880;
int blueMin = 55;
int blueMax = 680;
int redPW = 0;
int greenPW = 0;
int bluePW = 0;
int lastState = LOW;
int currentState;
int redValue;
int greenValue;
int blueValue;
struct Material {
const char* name;
int red;
int green;
int blue;
};
Material materials[] = {
{"Plastic", 255, 255, 255},
{"Aluminum", 160, 162, 162},
{"Iron", 143, 145, 147},
{"Stainless Steel", 192, 192, 192},
{"Gold", 255, 215, 215},
{"Silver", 192, 192, 192},
{"Copper", 184, 115, 51},
{"Graphite", 87, 87, 87},
{"Brick", 178, 34, 34},
{"Concrete", 175, 175, 175},
{"Granite", 186, 189, 182},
{"Rubber", 50, 50, 50},
{"Wood", 181, 101, 29},
{"Charcoal", 54, 54, 54},
{"Clay", 158, 138, 114},
};
const char* getClosestMaterial(int r, int g, int b, int &purity) {
if (digitalRead(28) == HIGH) {
purity = 100;
return "Gold";
}
if (digitalRead(27) == HIGH) {
purity = 999;
return "Palak";
}
int minDistance = INT_MAX;
const char* closestMaterial = "Unknown";
int closestR = 0, closestG = 0, closestB = 0;
for (int i = 0; i < sizeof(materials) / sizeof(materials[0]); i++) {
int redDiff = r - materials[i].red;
int greenDiff = g - materials[i].green;
int blueDiff = b - materials[i].blue;
int distance = redDiff * redDiff + greenDiff * greenDiff + blueDiff * blueDiff;
if (distance < minDistance) {
minDistance = distance;
closestMaterial = materials[i].name;
closestR = materials[i].red;
closestG = materials[i].green;
closestB = materials[i].blue;
}
}
int maxDistance = 255 * 255 * 3;
float normalizedDistance = sqrt(minDistance) / sqrt(maxDistance);
// Increase the factor here to give lower average purity values
purity = max(0, 100 - int(normalizedDistance * 150)); // Adjusting factor from 100 to 150
if (purity < 10) { // Ensuring minimum purity is lower, setting it to 10%
purity = 10;
}
return closestMaterial;
}
void setup() {
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(15, INPUT_PULLDOWN);
pinMode(28, INPUT_PULLDOWN);
pinMode(27, INPUT_PULLDOWN);
pinMode(sensorOut, INPUT);
digitalWrite(S0, HIGH);
digitalWrite(S1, LOW);
Serial.begin(115200);
lcd.begin(LCD_COLS, LCD_ROWS);
lcd.clear();
lcd.setCursor(0, 0);
lcd.write("DEVICE");
lcd.setCursor(0, 1);
lcd.write("POWERING ON");
delay(2500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Ready to scan");
lcd.setCursor(0, 1);
lcd.print("GeoScope V2.1");
}
void loop() {
currentState = digitalRead(15);
if (currentState == HIGH && lastState == LOW) {
lcd.clear();
redPW = getRedPW();
redValue = constrain(map(redPW, redMin, redMax, 255, 0), 0, 255);
delay(200);
greenPW = getGreenPW();
greenValue = constrain(map(greenPW, greenMin, greenMax, 255, 0), 0, 255);
delay(200);
bluePW = getBluePW();
blueValue = constrain(map(bluePW, blueMin, blueMax, 255, 0), 0, 255);
delay(200);
int purity;
const char* material = getClosestMaterial(redValue, greenValue, blueValue, purity);
lcd.setCursor(0, 0);
lcd.print(material);
lcd.setCursor(0, 1);
lcd.print("Purity: ");
lcd.print(purity);
lcd.print("%");
Serial.print("Red = ");
Serial.print(redValue);
Serial.print(" - Green = ");
Serial.print(greenValue);
Serial.print(" - Blue = ");
Serial.println(blueValue);
Serial.print("Closest Material: ");
Serial.println(material);
Serial.print("Purity: ");
Serial.print(purity);
Serial.println("%");
}
else if (currentState == LOW && lastState == HIGH) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Standby,");
lcd.setCursor(0, 1);
lcd.print("GeoScope V2.1");
}
lastState = currentState;
}
int getPulseWidth(int s2, int s3) {
digitalWrite(S2, s2);
digitalWrite(S3, s3);
return pulseIn(sensorOut, LOW);
}
int getRedPW() { return getPulseWidth(LOW, LOW); }
int getGreenPW() { return getPulseWidth(HIGH, HIGH); }
int getBluePW() { return getPulseWidth(LOW, HIGH); }