0% found this document useful (0 votes)
4 views6 pages

Code Projet Tapi Roulant

This document contains an Arduino sketch that integrates various components including ultrasonic sensors, servos, a color sensor, and LEDs. The program detects objects and their colors, activating corresponding servos and indicators based on the detected color. It also includes functions to handle red, blue, and green objects with specific actions for each color.

Uploaded by

jospinzebaze25
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Code Projet Tapi Roulant

This document contains an Arduino sketch that integrates various components including ultrasonic sensors, servos, a color sensor, and LEDs. The program detects objects and their colors, activating corresponding servos and indicators based on the detected color. It also includes functions to handle red, blue, and green objects with specific actions for each color.

Uploaded by

jospinzebaze25
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

#include <Servo.

h>

#include <Wire.h>

#include "TCS230.h"

// Define pin connections

const int trigPin = 9;

const int echoPin = 10;

const int motorPin = 8;

const int buzzerPin = 12;

const int ledRedPin = 3;

const int ledGreenPin = 4;

const int ledBluePin = 5;

// Create Servo objects

Servo servo1;

Servo servo2;

Servo servo3;

Servo servo4;

// Define servo pins

const int servoPin1 = 6;

const int servoPin2 = 7;

const int servoPin3 = 11;

const int servoPin4 = 13;

// Initialize the color sensor

TCS230 tcs230;

void setup() {

// Initialize serial communication

Serial.begin(9600);
// Initialize ultrasonic sensor

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

// Initialize motor and buzzer

pinMode(motorPin, OUTPUT);

pinMode(buzzerPin, OUTPUT);

// Initialize LEDs

pinMode(ledRedPin, OUTPUT);

pinMode(ledGreenPin, OUTPUT);

pinMode(ledBluePin, OUTPUT);

// Attach servos to pins

servo1.attach(servoPin1);

servo2.attach(servoPin2);

servo3.attach(servoPin3);

servo4.attach(servoPin4);

// Set initial position of servos

servo1.write(0);

servo2.write(0);

servo3.write(0);

servo4.write(0);

// Initialize color sensor

tcs230.init();

tcs230.setFrequency(TCS230_FREQ_20);

}
void loop() {

// Check for object detection using ultrasonic sensor

if (detectObject()) {

// Turn on the DC motor

digitalWrite(motorPin, HIGH);

// Open the 4th servo motor

servo4.write(90);

delay(5000);

servo4.write(0);

// Activate the color sensor

String color = detectColor();

if (color == "Red") {

handleRedObject();

} else if (color == "Blue") {

handleBlueObject();

} else if (color == "Green") {

handleGreenObject();

// Turn off the DC motor after processing

digitalWrite(motorPin, LOW);

bool detectObject() {

// Send a pulse to trigger the ultrasonic sensor

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);

digitalWrite(trigPin, LOW);

// Read the pulse duration

long duration = pulseIn(echoPin, HIGH);

// Calculate distance

long distance = (duration / 2) / 29.1;

// Check if the distance is less than a threshold (e.g., 10 cm)

return (distance < 10);

String detectColor() {

int red = tcs230.readRed();

int green = tcs230.readGreen();

int blue = tcs230.readBlue();

Serial.print("R: "); Serial.print(red);

Serial.print(" G: "); Serial.print(green);

Serial.print(" B: "); Serial.println(blue);

// Determine the detected color

if (red > green && red > blue) {

return "Red";

} else if (blue > red && blue > green) {

return "Blue";

} else if (green > red && green > blue) {

return "Green";

} else {

return "Unknown";
}

void handleRedObject() {

// Turn on red LED and buzzer

digitalWrite(ledRedPin, HIGH);

tone(buzzerPin, 1000);

// Move the 1st servo motor

servo1.write(90);

delay(3000);

servo1.write(0);

// Turn off red LED and buzzer

digitalWrite(ledRedPin, LOW);

noTone(buzzerPin);

void handleBlueObject() {

// Turn on blue LED and buzzer

digitalWrite(ledBluePin, HIGH);

tone(buzzerPin, 1000);

// Move the 2nd servo motor

servo2.write(90);

delay(4000);

servo2.write(0);

// Turn off blue LED and buzzer

digitalWrite(ledBluePin, LOW);

noTone(buzzerPin);
}

void handleGreenObject() {

// Turn on green LED and buzzer

digitalWrite(ledGreenPin, HIGH);

tone(buzzerPin, 1000);

// Move the 3rd servo motor

servo3.write(90);

delay(5000);

servo3.write(0);

// Turn off green LED and buzzer

digitalWrite(ledGreenPin, LOW);

noTone(buzzerPin);

You might also like