0% found this document useful (0 votes)
17 views2 pages

Color

This code uses an IR sensor and two servo motors to sort objects by color. It defines pins for the IR sensor and servo motors. In the setup, it initializes the sensors, motors, and LCD display. The main loop uses the IR sensor to detect color, displays it on the LCD, and directs the bottom servo motor to position for sorting based on detecting black or white. It then rotates the top servo motor to pick up another object and repeats the process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

Color

This code uses an IR sensor and two servo motors to sort objects by color. It defines pins for the IR sensor and servo motors. In the setup, it initializes the sensors, motors, and LCD display. The main loop uses the IR sensor to detect color, displays it on the LCD, and directs the bottom servo motor to position for sorting based on detecting black or white. It then rotates the top servo motor to pick up another object and repeats the process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <Servo.

h>
#include <LiquidCrystal.h> // Include the LCD library

// Define the pin for the IR sensor


const int irSensorPin = A0;

// Define the pins for servo motors


const int servoTopPin = 9; // Top servo for picking
const int servoBottomPin = 10; // Bottom servo for sorting

Servo servoTop;
Servo servoBottom;

int threshold = 500; // Initial threshold value


int currentColor = -1; // Initial color state: -1 indicates undefined
bool topServoReady = false; // Flag to indicate if top servo is ready

unsigned long servoMoveStartTime = 0;


const unsigned long servoMoveDuration = 5000; // 5 seconds

// Define LCD pins


LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Adjust pin numbers as needed

void setup() {
// Initialize serial communication
Serial.begin(9600);

// Initialize the LCD


lcd.begin(16, 2); // Set the LCD size (16 columns, 2 rows)

// Attach servos to their respective pins


servoTop.attach(servoTopPin);
servoBottom.attach(servoBottomPin);

// Initialize the position of the bottom servo


servoBottom.write(90); // Starting position is 90 degrees
}

void loop() {
unsigned long currentTime = millis();

// Check if the top servo is at 90 degrees


if (!topServoReady) {
// Top servo actions
// Rotate to 90 degrees
servoTop.write(90);
servoMoveStartTime = currentTime; // Record the start time of servo movement
topServoReady = true; // Set topServoReady flag after top servo reaches 90
degrees
}

// Check if the top servo movement duration has elapsed


if (currentTime - servoMoveStartTime >= servoMoveDuration) {
// Read analog value from the IR sensor only when the top servo is ready
if (topServoReady) {
int irValue = analogRead(irSensorPin);

Serial.print("IR Value: ");


Serial.println(irValue);
// Check if the color is black
if (irValue > threshold) {
if (currentColor != 1) { // If the current color is not black
Serial.println("Color Detected: Black");
lcd.setCursor(0, 0); // Set cursor to the first column of the first row
lcd.print("Color: Black"); // Display color on LCD
// Move the bottom servo to a specific position
servoBottom.write(50); // Rotate to 50 degrees (adjust as needed)
delay(3000); // Adjust the delay as needed
currentColor = 1; // Update current color to black
}
}
// Check if the color is white
else {
if (currentColor != 0) { // If the current color is not white
Serial.println("Color Detected: White");
lcd.setCursor(0, 0); // Set cursor to the first column of the first row
lcd.print("Color: White"); // Display color on LCD
// Move the bottom servo to a specific position for white color
servoBottom.write(120); // Rotate to 120 degrees (adjust as needed)
delay(3000); // Adjust the delay as needed
currentColor = 0; // Update current color to white
}
}
}

// Reset top servo readiness for the next cycle


topServoReady = false;

// Rotate to 180 degrees


servoTop.write(180);
delay(2000); // Adjust the delay as needed

// Return to 0 degrees
servoTop.write(0);
delay(2000); // Adjust the delay as needed
}
}

You might also like