Wednesday, June 30, 2021

Project 14: Arduino Robot Arm Color Detection & Sorting

 

Project 14:  Arduino Robot Arm Color Detection & Sorting

Main Ideas:

Making Arduino Robot Arm Color Detection & Sorting machine , in this project we have used the TCS3200 color sensor can detect a wide range of colors based on their wavelength and then programmed the robot Arm to take the sample which we want to the robot to detect the color for it , then robot Arm will take the sample to word the TCS3200 color sensor to detect the color and based on the detected color the robot Arm will move the sample to the right sorting place , i.e. it will move the red samples to red cup and so on , is same time the program will show the detection result on the LCD.








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();

}


Project 13: Arduino Color Sensor TCS3200 & LCD

Project 13:  Arduino Color Sensor TCS3200 & LCD






/*********
  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>

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 6 //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 setup() {
  // 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(3000);
}

void loop() {

  // 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(100);
  // 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 !");
  }
  if (greenColor < redColor && greenColor < blueColor) {
    Serial.println("****** Green color detected ******");
    lcd.clear();
    lcd.print("Green Color");
    lcd.setCursor(0, 1);
    lcd.print("Detected !");
  }
  if (blueColor < redColor && blueColor < greenColor) {
    Serial.println("****** Blue color detected ******");
    lcd.clear();
    lcd.print("Blue Color ");
    lcd.setCursor(0, 1);
    lcd.print("Detected !");
  }
  delay(400);
  Serial.println("");
}

Sunday, June 27, 2021

Project 12: Arduino IR Remote-infrared communication

 Project 12:  Arduino IR Remote-infrared communication 

Main Ideas:

IR or infrared communication is one of the most common methods of wireless communication due to being easy to use and having an affordable price. Infrared light, with a wavelength longer than visible light, is not within the range of human vision. That’s why it’s a good option for wireless communications. When you press a button on your TV control, an LED on your control turns on and off continuously and causes a modulated infrared signal to send from the control to your TV. The command will execute after the signal is demodulated. IR receiver modules are used to receive IR signals. These modules work in 3, 8 KHz frequency. When the sensor is not exposed to any light at its working frequency, the Vout output has a value equal to VS (power supply). With exposing to a 38 kHz infrared light, this output will be zero.






/*********

  https://arduino4everyones.blogspot.com/

 

*********/

#include <IRremote.h>

 

const int RECV_PIN = 7;

IRrecv irrecv(RECV_PIN);

decode_results results;

unsigned long key_value = 0;

int  onoff = 15; // control the blinking

int led_green = 11; // the red LED is connected to Pin 11 of the Arduino

int led_yellow = 12; // the yellow LED is connected to Pin 12 of the Arduino

int led_red = 13; // the green LED is connected to Pin 13 of the Arduino

int led_white = 10; // the green LED is connected to Pin 13 of the Arduino

void setup() {

  Serial.begin(9600);

  irrecv.enableIRIn();

  // irrecv.blink13(true);

  // set up all the LEDs as OUTPUT

  pinMode(led_red, OUTPUT);

  pinMode(led_yellow, OUTPUT);

  pinMode(led_green, OUTPUT);

 

  //blinking();

}

 

void loop() {

  if (irrecv.decode(&results)) {

 

    if (results.value == 0XFFFFFFFF)

      results.value = key_value;

 

    switch (results.value) {

      case 0xFFA25D:

        Serial.println("CH-");

        // turn the green LED on and the other LEDs off

        digitalWrite(led_red, LOW);

        digitalWrite(led_yellow, LOW);

        digitalWrite(led_green, HIGH );

        digitalWrite(led_white, LOW );

        break;

      case 0xFF629D:

        Serial.println("CH");

        // turn the yellow LED on and the other LEDs off

        digitalWrite(led_red, LOW);

        digitalWrite(led_yellow, HIGH);

        digitalWrite(led_green, LOW);

        digitalWrite(led_white, LOW );

        break;

      case 0xFFE21D:

        Serial.println("CH+");

        // turn the red LED on and the other LEDs off

        digitalWrite(led_red, HIGH);

        digitalWrite(led_yellow, LOW);

        digitalWrite(led_green, LOW);

        break;

      case 0xFF22DD:

        Serial.println("|<<");

        // turn the red LED on and the other LEDs off

        digitalWrite(led_red, HIGH);

        digitalWrite(led_yellow, HIGH);

        digitalWrite(led_green, LOW);

        digitalWrite(led_white, LOW );

        break;

      case 0xFF02FD:

        Serial.println(">>|");

        // turn the red LED on and the other LEDs off

        digitalWrite(led_red, LOW);

        digitalWrite(led_yellow, HIGH);

        digitalWrite(led_green, HIGH);

        digitalWrite(led_white, LOW );

        break ;

      case 0xFFC23D:

        Serial.println(">|");

        // turn the red LED on and the other LEDs off

        digitalWrite(led_red, HIGH);

        digitalWrite(led_yellow, HIGH);

        digitalWrite(led_green, HIGH);

        break ;

      case 0xFFE01F:

        Serial.println("-");

        digitalWrite(led_red, HIGH);

        digitalWrite(led_yellow, HIGH);

        digitalWrite(led_green, HIGH );

        digitalWrite(led_white, HIGH );

        break ;

      case 0xFFA857:

        Serial.println("+");

        break ;

      case 0xFF906F:

        Serial.println("EQ");

        break ;

      case 0xFF6897:

        Serial.println("0");

        // turn the red LED on and the other LEDs off

        digitalWrite(led_red, LOW);

        digitalWrite(led_yellow, LOW);

        digitalWrite(led_green, LOW);

        digitalWrite(led_white, LOW );

        break ;

      case 0xFF9867:

        Serial.println("100+");

        blinking();

        break ;

      case 0xFFB04F:

        Serial.println("200+");

        break ;

      case 0xFF30CF:

        Serial.println("1");

        break ;

      case 0xFF18E7:

        Serial.println("2");

        break ;

      case 0xFF7A85:

        Serial.println("3");

        break ;

      case 0xFF10EF:

        Serial.println("4");

        break ;

      case 0xFF38C7:

        Serial.println("5");

        break ;

      case 0xFF5AA5:

        Serial.println("6");

        break ;

      case 0xFF42BD:

        Serial.println("7");

        break ;

      case 0xFF4AB5:

        Serial.println("8");

        break ;

      case 0xFF52AD:

        Serial.println("9");

        break ;

    }

    key_value = results.value;

    irrecv.resume();

    delay(100);

  }

}

 

void blinking()

{

 

  int  looponoff = 0; // save the time that we started moveback

  while (looponoff < onoff) // stay in this loop until timeToTrunRight5CM ( seconds) has elapsed

  {

    looponoff = looponoff + 1;

    // turn the green LED on and the other LEDs off

    digitalWrite(led_red, LOW);

    digitalWrite(led_yellow, LOW);

    digitalWrite(led_green, LOW );

    digitalWrite(led_white, HIGH );

    delay(100);    // wait 2 seconds

    // turn the green LED on and the other LEDs off

    digitalWrite(led_red, LOW);

    digitalWrite(led_yellow, LOW);

    digitalWrite(led_green, HIGH );

    digitalWrite(led_white, LOW );

    delay(100);    // wait 2 seconds

 

    // turn the yellow LED on and the other LEDs off

    digitalWrite(led_red, LOW);

    digitalWrite(led_yellow, HIGH);

    digitalWrite(led_green, LOW);

    digitalWrite(led_white, LOW );

    delay(100);   // wait 0.5 second

 

    // turn the red LED on and the other LEDs off

    digitalWrite(led_red, HIGH);

    digitalWrite(led_yellow, LOW);

    digitalWrite(led_green, LOW);

    digitalWrite(led_white, LOW );

    delay(100);  // wait 2 seconds

  }

  // turn off all EDs off

  digitalWrite(led_red, LOW);

  digitalWrite(led_yellow, LOW);

  digitalWrite(led_green, LOW);

  digitalWrite(led_white, LOW );

}