Project 14: Arduino Robot Arm Color Detection &
Sorting
Main Ideas:
Code:
/********* https://arduino4everyones.blogspot.com/ Color ranges 2 cm till 5 cm Main Ideas: The TCS3200 color sensor can detect a wide
range of colors based on their wavelength. This sensor is can be used for color recognition/detection
projects such as color matching, color sorting. The sensor has four different types of
filter covered diodes. In the 8 x 8 array of photodiodes, 16 photodiodes have Red filters, 16 have
Blue filters, 16 have Green filters and the rest 16 photodiodes are clear with no filters. Each type can be
activated using the S2, S3 selection inputs. *********/ #include <Wire.h> #include
<LiquidCrystal_PCF8574.h> #include <Servo.h> //Robot Arm Servo middle, left, right,
claw ; // creates 4 "servo objects" int middlepin = 11; // orange attaches the Base
servo int leftpin = 10; // green wire attaches the
left servo int rightpin = 9; // gray wire attaches right
servo int clawpin = 6; // yellow wire attaches the
servo on pin 6 to the claw object int pos = 0; // variable to store the servo position //LCD LiquidCrystal_PCF8574
lcd(0x27); // set the LCD address to
0x27 for a 16 chars and 2 line display // TCS230 or TCS3200 pins
wiring to Arduino #define S0 2 // gray
#define S1 3 //maron #define S2 4 //yellow #define S3 5 //green #define sensorOut 7 //orange // Capture frequencies read
by the TCS230/TCS3200 Sensor int redFrequency = 0; int greenFrequency = 0; int blueFrequency = 0; //Define the colors //red color int redColor = 0; //green color int greenColor = 0; //blue color int blueColor = 0; void basered() { //we will start from the trun right near
color sensor pos was zero pos==0 //we will move back =45 and drop item middle.attach(middlepin); for (pos = 0; pos <= 45; pos += 1) { //
goes from 0 degrees to 45 degrees // in steps of 1 degree middle.write(pos); delay(20); // waits 15ms for the
servo to reach the position } delay(50); middle.detach(); hand(); int z = backmiddel (45, 1) ; } void baseblue() { //we will start from the trun right near
color sensor pos was zero pos==0 //we will move back =45 and drop item // left trun=130 middle.attach(middlepin); // attaches the servo on pin 9 to the servo
object for (pos = 0; pos <= 130; pos += 1) { //
goes from 0 degrees to 180 degrees // in steps of 1 degree middle.write(pos); delay(20); // waits 15ms for the
servo to reach the position } delay (50); middle.detach(); hand(); int z = backmiddel (130, 2) ; } void basegreen() { //we will start from the trun right near
color sensor pos was zero pos==0 //we will move back =45 and drop item // left trun=130 middle.attach(middlepin); // attaches the servo on pin 9 to the servo
object for (pos = 0; pos <= 180; pos += 1) { //
goes from 0 degrees to 180 degrees // in steps of 1 degree middle.write(pos); delay(20); // waits 15ms for the
servo to reach the position } delay (50); middle.detach(); hand(); int z = backmiddel (180, 3) ; } int backmiddel (int posm,
int directiona) // function declaration { if (directiona == 1) { //back middle =90 middle.attach(middlepin); // attaches the servo on pin 9 to the servo
objectclawV.write(90); for (pos = posm; pos <= 90; pos += 1) { // in steps of 1 degree middle.write(pos); delay(20); // waits 15ms for the
servo to reach the position } delay(50); middle.detach(); } if (directiona == 2) { //back middle =90 middle.attach(middlepin); // attaches the servo on pin 9 to the servo
objectclawV.write(90); for (pos = 0; pos <= 50; pos += 1) { // in steps of 1 degree middle.write(130 - pos); delay(20); // waits 15ms for the
servo to reach the position } //
middle.write(90); delay (50); middle.detach(); } if (directiona == 3) { //back middle =90 middle.attach(middlepin); // attaches the servo on pin 9 to the servo
objectclawV.write(90); for (pos = 0; pos <= 90; pos += 1) { // in steps of 1 degree middle.write(180 - pos); delay(20); // waits 15ms for the
servo to reach the position } //
middle.write(90); delay (50); middle.detach(); } return 1; // return the value } void gotosensor() { hand(); // right trun =0 middle.attach(middlepin); // attaches the servo on pin 9 to the servo
object for (pos = 0; pos <= 90; pos += 1) { // goes
from 0 degrees to 180 degrees // in steps of 1 degree middle.write(90 - pos); delay(20); // waits 15ms for the
servo to reach the position } delay(50); middle.detach(); // hand(); delay(100); color_detection(); } void setup() { right_left_Up(); // hand(); //gotosensor(); // basered(); // baseblue(); // basegreen(); // basemove(); //movebasecenterfromleft(); //movebaseleft(); // Setting the outputs pinMode(S0, OUTPUT); pinMode(S1, OUTPUT); pinMode(S2, OUTPUT); pinMode(S3, OUTPUT); // Setting the sensorOut as an input pinMode(sensorOut, INPUT); // Setting frequency scaling to 20% digitalWrite(S0, HIGH); digitalWrite(S1, LOW); // Begins serial communication Serial.begin(9600); int LCDError; Serial.println("LCD..."); // http://arduino4everyones.blogspot.com Wire.begin(); Wire.beginTransmission(0x27); //Define LCD
Address LCDError = Wire.endTransmission(); Serial.print("LCD Error Found:
"); Serial.print(LCDError); if (LCDError == 0) { Serial.println("No Error: LCD
found."); } else { Serial.println("LCD is Missing
, check connection."); } // initialize the lcd lcd.begin(16, 2); lcd.setBacklight(255); lcd.home(); lcd.clear(); lcd.print("LCD Found"); delay(1500); lcd.clear(); lcd.print("Welcome to "); lcd.setCursor(0, 1); lcd.print("Arduino4Everyones"); delay(2000); lcd.clear(); lcd.print("Color Detection"); lcd.setCursor(0, 1); lcd.print("Started... "); delay(1000); } void color_detection() { // Set RED (R) filtered photodiodes to be
read digitalWrite(S2, LOW); digitalWrite(S3, LOW); // Reading the output frequency redFrequency = pulseIn(sensorOut, LOW); // Remaping the value of the RED (R)
frequency from 0 to 255 // You must replace with your own values.
Here's an example: // this range for 2-3 cm distance //redColor = map(redFrequency, 137, 144,
255, 0); redColor = redFrequency; // Printing the RED (R) value Serial.print(" R = "); Serial.print(redColor); delay(100); // Setting GREEN (G) filtered photodiodes
to be read digitalWrite(S2, HIGH); digitalWrite(S3, HIGH); // Reading the output frequency greenFrequency = pulseIn(sensorOut, LOW); // Remaping the value of the GREEN (G)
frequency from 0 to 255 // You must replace with your own values.
Here's an example: // Printing the GREEN (G) value // this range for 2-3 cm distance //greenColor = map(greenFrequency, 206, 212, 255, 0); greenColor = greenFrequency; // Printing the GREEN (G) value Serial.print(" G = "); Serial.print(greenColor); delay(100); // Setting BLUE (B) filtered photodiodes to
be read digitalWrite(S2, LOW); digitalWrite(S3, HIGH); // Reading the output frequency blueFrequency = pulseIn(sensorOut, LOW); // Remaping the value of the BLUE (B)
frequency from 0 to 255 // You must replace with your own values.
Here's an example: // this range for 2-5 cm distance //blueColor = map(blueFrequency, 96, 102,
255, 0); blueColor = blueFrequency; // Printing the BLUE (B) value Serial.print(" B = "); Serial.print(blueColor); Serial.println(""); delay(200); // Checks the current detected color and
prints // a message in the serial monitor if (redColor < greenColor &&
redColor < blueColor) { Serial.println("****** Red color
detected *****"); lcd.clear(); lcd.print("Red Color"); lcd.setCursor(0, 1); lcd.print("Detected !"); basered(); } if (greenColor < redColor &&
greenColor < blueColor) { Serial.println("****** Green color
detected ******"); lcd.clear(); lcd.print("Green Color"); lcd.setCursor(0, 1); lcd.print("Detected !"); basegreen(); } if (blueColor < redColor &&
blueColor < greenColor) { Serial.println("****** Blue color
detected ******"); lcd.clear(); lcd.print("Blue Color "); lcd.setCursor(0, 1); lcd.print("Detected !"); baseblue(); } delay(400); //Serial.println(""); } void loop() { gotosensor(); delay(2500); /* // Checks the current detected color and
prints // a message in the serial monitor if (redColor > greenColor &&
redColor > blueColor) { Serial.println("****** Red color
detected *****"); } if (greenColor > redColor &&
greenColor > blueColor) { Serial.println("****** Green color
detected ******"); } if (blueColor > redColor &&
blueColor > greenColor) { Serial.println("****** Blue color
detected ******"); } delay(400); */ } void right_left_Up() { left.attach(leftpin); // attaches the servo on pin 9 to the servo
object right.attach(rightpin); // attaches the servo on pin 9 to the servo
object left.write(0); right.write(90); // tell servo to go to position in
variable 'pos' delay (500); left.detach(); right.detach(); delay (500); } void hand() { // claw // open claw.attach(clawpin); // attaches the servo on pin 9 to the servo
object claw.write(0); delay (700); claw.detach(); //close claw.attach(clawpin); // attaches the servo on pin 9 to the servo
objectclawV.write(90); claw.write(90); delay (1000); //claw.detach(); delay (500); } void basemove() { hand(); // right trun =0 middle.attach(middlepin); // attaches the servo on pin 9 to the servo
object for (pos = 0; pos <= 90; pos += 1) { //
goes from 0 degrees to 180 degrees // in steps of 1 degree middle.write(90 - pos); delay(20); // waits 15ms for the
servo to reach the position } delay(50); middle.detach(); // hand(); //back middle =90 middle.attach(middlepin); // attaches the servo on pin 9 to the servo
objectclawV.write(90); for (pos = 0; pos <= 90; pos += 1) { //
goes from 0 degrees to 180 degrees // in steps of 1 degree middle.write(pos); delay(20); // waits 15ms for the
servo to reach the position } // middle.write(90); //delay (500); delay(50); middle.detach(); //hand(); // left trun=180 middle.attach(middlepin); // attaches the servo on pin 9 to the servo
object for (pos = 90; pos <= 180; pos += 1) {
// goes from 0 degrees to 180 degrees // in steps of 1 degree middle.write(pos); delay(20); // waits 15ms for the
servo to reach the position } //middle.write(180); delay (50); middle.detach(); //back middle =90 middle.attach(middlepin); // attaches the servo on pin 9 to the servo
objectclawV.write(90); for (pos = 0; pos <= 90; pos += 1) { //
goes from 0 degrees to 180 degrees // in steps of 1 degree middle.write(180 - pos); delay(15); // waits 15ms for the
servo to reach the position } //
middle.write(90); delay (50); middle.detach(); // hand(); } |
No comments:
Post a Comment