Build a color-sorting machine with Arduino
FunduinoPublished on
Schokolinsen automatisch nach Farbe sortieren
How the sorting machine works
1 / 3
Required materials
Assembly: populate the base carrier
1 / 2
Assembly: Mount the slide and servo
1 / 3
Design: Rotary wheel and cover plate
1 / 2
Wiring
1 / 2
Position the rotary knob
Turn the rotary knob to the starting position.
#include <Servo.h>
Servo Turntable; // The servo is called Turntable.
// Servo positions
int Load=0; // The position named “Load” has a rotation angle of 0.
void setup()
{
Turntable.attach(4); // Connect the servo to pin 4.
}
void loop()
{
Turntable.write(Load); // Load the position.
delay(1000); // Wait until the position is reached.
}Move to the three positions of the rotary knob
#include <Servo.h>
Servo Drehrad; // The servo is called Drehrad.
// Servo positions
int Load=0; // Rotation angle for loading the candy
int Scan=55; // Rotation angle for scanning the candy
int Ext=120; // Rotation angle for removing the candy
void setup()
{
Drehrad.attach(4); // Connect the servo to pin 4
}
void loop()
{
Drehrad.write(Load); // Set the loading position.
delay(1000); // Wait until the position is reached.
Drehrad.write(Scan); // Set the position for scanning the candy.
delay(2000); // Wait until the position is reached.
Drehrad.write(Ext); // Set the position for ejecting the candy.
delay(3000); // Wait until the position is reached.
}Adjust these three rotation angles
int Load=0; // Rotation angle for loading the candy int Scan=55; // Rotation angle for scanning the candy int Ext=120; // Rotation angle for removing the candy
Position the slide
Turn the slide to the middle position
#include <Servo.h>
Servo servoSchwenker; // The servo is called servoSchwenker.
void setup()
{
servoSchwenker.attach(3); // Connect the servo to pin 3
}
void loop()
{
servoSchwenker.write(90); // Set the center position
delay(1000); // Wait until the position is reached.
}Move to the six defined positions
#include <Servo.h>
Servo servoSchwenker; // The servo is called servoSchwenker.
// Servo positions
int Fach1=0;
int Fach2=45;
int Fach3=75;
int Fach4=105;
int Fach5=135;
int Fach6=180;
void setup()
{
servoSchwenker.attach(3); // Connect the servo to pin 3
}
void loop()
{
servoSchwenker.write(Fach1); // The servo turns to position 1 (Fach1)
delay(1000); // Wait until the position is reached.
servoSchwenker.write(Fach2); // The servo turns to position 2 (Fach2)
delay(1000); // Wait until the position is reached.
servoSchwenker.write(Fach3); // The servo turns to position 3 (Fach3)
delay(1000); // Wait until the position is reached.
servoSchwenker.write(Fach4); // The servo turns to position 4 (Fach4)
delay(1000); // Wait until the position is reached.
servoSchwenker.write(Fach5); // The servo turns to position 5 (Fach5)
delay(1000); // Wait until the position is reached.
servoSchwenker.write(Fach6); // The servo turns to position 6 (Fach6)
delay(1000); // Wait until the position is reached.
}Adjust these six angles
int Compartment1=0; int Compartment2=45; int Compartment3=75; int Compartment4=105; int Compartment5=135; int Compartment6=180;
The scanning process: measuring color values
Scan process: Measure and output color values
// Add servo
#include <Servo.h>
Servo servoDrehrad;
// Servo positions
int Load=0;
int Scan=55;
int m=1;
// RGB LED
int LEDrot = 5; // Red color on pin 5
int LEDblau=6; // Blue color on pin 6
int LEDgruen = 9; // Green color on pin 9
int p=300; // p is the variable for a pause
int LeuchtkraftBlau = 200; // Brightness of the individual LED colors. These values are chosen so that all colors have the same measured brightness against a black background.
int LeuchtkraftRot = 200; // Brightness of the individual LED colors. These values are chosen so that all colors have the same measured brightness against a black background.
int LeuchtkraftGruen = 200; // Brightness of the individual LED colors. These values are chosen so that all colors have the same measured brightness against a black background.
int dunkel = 0; // A value of 0 means a voltage of 0 V - i.e. LED off.
long FarbwertRot=0; // Variable for measured values with an initial value of 0
long FarbwertGruen=0; // Variable for measured values with an initial value of 0
long FarbwertBlau=0; // Variable for measured values with an initial value of 0
int DurchschnittFarbwertRot=0; // Variable for measured values with an initial value of 0
int DurchschnittFarbwertGruen=0; // Variable for measured values with an initial value of 0
int DurchschnittFarbwertBlau=0; // Variable for measured values with an initial value of 0
// Photoresistor
int eingang= A0; // The word "eingang" now stands for the value "A0" (designation of analog port 0)
int sensorWert = 0; // Variable for the sensor value with 0 as the initial value
void setup()
{
// Add servo and center it
servoDrehrad.attach(4); // The setup contains the information that the servo is connected to pin 4 via the control wire (yellow). Another pin can of course also be used here.
Serial.begin(9600); // Communication with the serial port is started. This is needed to display the actual reading later in the serial monitor.
pinMode(LEDblau, OUTPUT); // Define LED color as output
pinMode(LEDgruen, OUTPUT); // Define LED color as output
pinMode(LEDrot, OUTPUT); // Define LED color as output
}
void loop()
{
// Clear all variables from previous measurements
FarbwertRot=0;
FarbwertGruen=0;
FarbwertBlau=0;
// Now load the candies to be measured. Be sure to watch the data in the Serial Monitor.
servoDrehrad.write (Load); // Load a new candy. If the candies don't load properly, you can program a brief back-and-forth movement of the servo here.
delay(1000);
servoDrehrad.write (Scan); // Turn the candy tray toward the sensor for measurement
delay(1000);
// Blue scanning process begins.
analogWrite(LEDblau, LeuchtkraftBlau); // Turn on blue
delay(p); // pause
// Repeat 200 times to calculate a good average for the measurement and minimize incorrect readings.
for (int i=1; i <= 200; i++)
{
sensorWert =analogRead(eingang);
FarbwertBlau=FarbwertBlau+sensorWert;
delay(m); // Wait one millisecond
}
DurchschnittFarbwertBlau=FarbwertBlau/200; // Divide the 200 added readings by 200 to obtain an average.
// Blue color output
Serial.print("Durchschnitt FarbwertBlau= " ); // Output in the Serial Monitor: the text "Durchschnitt FarbwertBlau= "
Serial.print(DurchschnittFarbwertBlau); // Output in the Serial Monitor. The Serial.print command sends the measured color value to the serial monitor.
analogWrite(LEDblau, dunkel); // Turn off the blue LED
// Green scanning process begins.
analogWrite(LEDgruen, LeuchtkraftGruen); // Turn on green
delay(p); // pause
// Repeat 200 times to calculate a good average for the measurement and minimize incorrect readings.
for (int i=1; i <= 200; i++)
{
sensorWert =analogRead(eingang);
FarbwertGruen=FarbwertGruen+sensorWert;
delay(m); // Wait one millisecond
}
DurchschnittFarbwertGruen=FarbwertGruen/200; // Divide the 200 added readings by 100 to obtain an average.
// Green color output
Serial.print("Durchschnitt FarbwertGruen= " ); // Output in the Serial Monitor: the text "Durchschnitt FarbwertGruen= "
Serial.print(DurchschnittFarbwertGruen); // Output in the Serial Monitor. The Serial.print command sends the measured color value to the serial monitor.
analogWrite(LEDgruen, dunkel); // Turn off the green LED
// Red scanning process begins.
analogWrite(LEDrot, LeuchtkraftRot); // Turn on red
delay(p); // pause
// Repeat 200 times to calculate a good average for the measurement and minimize incorrect readings.
for (int i=1; i <= 200; i++)
{
sensorWert =analogRead(eingang);
FarbwertRot=FarbwertRot+sensorWert;
delay(m); // Wait one millisecond
}
DurchschnittFarbwertRot=FarbwertRot/200; // Divide the 200 added readings by 100 to obtain an average.
// Red color output
Serial.print("Durchschnitt FarbwertRot= " ); // Output in the Serial Monitor: the text "Durchschnitt FarbwertRot= "
Serial.println(DurchschnittFarbwertRot); // Output in the Serial Monitor. The Serial.print command sends the measured color value to the serial monitor.
analogWrite(LEDrot, dunkel); // Turn off the red LED
// The measured values for the individual colors have been displayed in the Serial Monitor. Now comes the important part of the evaluation. Here, you need to determine which measured values correspond to each candy color.
}Evaluate the measured values
Schematic: an IF condition for the red candy
// Für den Fall, dass ein Rotes Bonbon vorhanden ist.
if (DurchschnittFarbwertBlau > 180 && DurchschnittFarbwertBlau < 220 && DurchschnittFarbwertGruen > 230 && DurchschnittFarbwertGruen < 270 && DurchschnittFarbwertRot > 730 && DurchschnittFarbwertRot < 770)
{
//In dieser geschweiften Klammer wird später ergänzt, was passiert, wenn die oben genannte Bedingung erfüllt ist.
}Extension: Eject the candy into the appropriate compartment
// Für den Fall, dass ein rotes Bonbon vorhanden ist.
if (DurchschnittFarbwertBlau > 180 && DurchschnittFarbwertBlau < 220 && DurchschnittFarbwertGruen > 230 && DurchschnittFarbwertGruen < 270 && DurchschnittFarbwertRot > 730 && DurchschnittFarbwertRot < 770)
{
servoSchwenker.write (0); // Position des Schwenkers geht zu Fach1
delay(1000); // Pause, bis die Drehung der Rutsche zum richtigen Fach abgeschlossen ist.
servoDrehrad.write (ext); //Das Drehrad dreht sich und das Bonbon fällt unten in den Trichter der Rutsche.
delay(1000); // Pause, bis die Drehung erfolgt ist und das Bonbon in den Trichter gefallen ist.
}The complete example program
Complete example program for three candy colors
// Add servos
#include <Servo.h>
Servo servoSchwenker;
Servo servoDrehrad;
// Servo positions
int Load=0;
int Scan=55;
int ext=120;
int m=1;
// RGB LED
int LEDrot = 5; // Red color on pin 5
int LEDblau=6; // Blue color on pin 6
int LEDgruen = 9; // Green color on pin 9
int p=50; // p is the variable for a pause
int LeuchtkraftBlau = 200; // Brightness of the individual LED colors. The values are chosen so that the brightness measured for all colors is the same against a black background.
int LeuchtkraftRot = 200; // Brightness of the individual LED colors.
int LeuchtkraftGruen = 200; // Brightness of the individual LED colors.
int dunkel = 0; // A value of 0 means 0 V - so the LED is off.
long FarbwertRot=0; // Variable for measurement values with an initial value of 0
long FarbwertGruen=0;
long FarbwertBlau=0;
int DurchschnittFarbwertRot=0;
int DurchschnittFarbwertGruen=0;
int DurchschnittFarbwertBlau=0;
int messungen =200;
// Photoresistor
int eingang= A0; //The word "eingang" now stands for the value "A0"
int sensorWert = 0; //Variable for the sensor value with 0 as the initial value
void setup()
{
//Add servos and center them
servoSchwenker.attach(3); //Servo connected to the control wire (yellow) at pin 3.
servoDrehrad.attach(4); //Servo connected to the control wire (yellow) at pin 4.
servoSchwenker.write (90); // The swing arm moves to a central position.
delay(2000); //Wait until the servos stop. Otherwise, fluctuating voltage could affect the measured values.
Serial.begin(9600);
pinMode(LEDblau, OUTPUT); // Define LED color as output
pinMode(LEDgruen, OUTPUT); // Define LED color as output
pinMode(LEDrot, OUTPUT); // Define LED color as output
}
void loop()
{
// Clear all variables from previous measurements
FarbwertRot=0;
FarbwertGruen=0;
FarbwertBlau=0;
servoDrehrad.write (Load); // Load a new candy.
delay(1000);
servoDrehrad.write (Scan); // Turn the candy holder toward the sensor for measurement
delay(1000);
// Blue scanning process begins.
analogWrite(LEDblau, LeuchtkraftBlau); // turn on blue
delay(p); // pause
for (int i=1; i <= messungen; i++)
{
sensorWert =analogRead(eingang);
FarbwertBlau=FarbwertBlau+sensorWert;
delay(m); //Wait one millisecond
}
DurchschnittFarbwertBlau=FarbwertBlau/messungen;
//Blue color output
Serial.print("Average blue color value= " );
Serial.print(DurchschnittFarbwertBlau);
analogWrite(LEDblau, dunkel); // Turn off the blue LED
// Green scanning process begins.
analogWrite(LEDgruen, LeuchtkraftGruen); // turn on green
delay(p); // pause
for (int i=1; i <= messungen; i++)
{
sensorWert =analogRead(eingang);
FarbwertGruen=FarbwertGruen+sensorWert;
delay(m); //Wait one millisecond
}
DurchschnittFarbwertGruen=FarbwertGruen/messungen;
//Green color output
Serial.print("Average green color value= " );
Serial.print(DurchschnittFarbwertGruen);
analogWrite(LEDgruen, dunkel); // Turn off the green LED
// Red scanning process begins.
analogWrite(LEDrot, LeuchtkraftRot); // turn on red
delay(p); // pause
for (int i=1; i <= messungen; i++)
{
sensorWert =analogRead(eingang);
FarbwertRot=FarbwertRot+sensorWert;
delay(m); //Wait one millisecond
}
DurchschnittFarbwertRot=FarbwertRot/messungen;
//Red color output
Serial.print("Average red color value= " );
Serial.println(DurchschnittFarbwertRot);
analogWrite(LEDrot, dunkel); // Turn off the red LED
// Evaluation and processing
// If red:
if (DurchschnittFarbwertBlau > 140 && DurchschnittFarbwertBlau < 230 && DurchschnittFarbwertGruen > 140 && DurchschnittFarbwertGruen < 220 && DurchschnittFarbwertRot > 550 && DurchschnittFarbwertRot < 750) //Case 1: Red candy
{
servoSchwenker.write (0); // The swing arm moves to position 1
delay(500);
servoDrehrad.write (ext); //The turntable rotates and the candy falls into the funnel below
delay(700);
}
// If green:
else if (DurchschnittFarbwertBlau > 260 && DurchschnittFarbwertBlau < 340 && DurchschnittFarbwertGruen > 290 && DurchschnittFarbwertGruen < 430 && DurchschnittFarbwertRot > 260 && DurchschnittFarbwertRot < 320) //Case 2: Green candy
{
servoSchwenker.write (30); // The swing arm moves to position 1
delay(500);
servoDrehrad.write (ext);
delay(700);
}
// If blue:
else if (DurchschnittFarbwertBlau > 450 && DurchschnittFarbwertBlau < 490 && DurchschnittFarbwertGruen > 300 && DurchschnittFarbwertGruen < 360 && DurchschnittFarbwertRot > 140 && DurchschnittFarbwertRot < 245) //Case 3: Blue candy
{
servoSchwenker.write (60); // The swing arm moves to position 1
delay(500);
servoDrehrad.write (ext);
delay(700);
}
else
{
servoSchwenker.write (180); // The swing arm moves to position 1
delay(500);
servoDrehrad.write (ext);
delay(700);
}
}Rate the instructions