Candy Sorter
The Sorting Logic
Comparing Colors
The sensor gives us an RGB value, like (200, 50, 50). We know that a "Red Candy" should be around (255, 0, 0). But it will never be exact.
How do we know if the sensor reading is "close enough" to Red?
Euclidean Distance
We treat the colors as points in 3D space (x=Red, y=Green, z=Blue). The distance between two colors is:
CPP
float distance = sqrt(pow(r2 - r1, 2) + pow(g2 - g1, 2) + pow(b2 - b1, 2));The smaller the distance, the closer the match!
The Code
Here is the logic to find the closest matching color:
CPP
int getClosestColor(int r, int g, int b) {
int minDist = 9999;
int detectedColor = -1;
// Check against known colors (Red, Green, Blue, Yellow)
// ... logic here ...
return detectedColor;
}