0% found this document useful (0 votes)
14 views

Colour Controlled Car using Colour Sensor and Arduino

This program controls a car's movement based on colors detected by a color sensor, allowing it to move forward, in circles, or in triangular patterns depending on whether it senses red, green, or blue. The car stops when no color is detected. The code includes functions for reading colors, checking the sensed color, and controlling the motors for movement.

Uploaded by

dborcic61
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)
14 views

Colour Controlled Car using Colour Sensor and Arduino

This program controls a car's movement based on colors detected by a color sensor, allowing it to move forward, in circles, or in triangular patterns depending on whether it senses red, green, or blue. The car stops when no color is detected. The code includes functions for reading colors, checking the sensed color, and controlling the motors for movement.

Uploaded by

dborcic61
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/ 5

/*

This program moves a car in three


different patterns based on the
colour sensed by the colour sensor.
The colour sensor is mounted on the
car.
This program is created by Shreyas for
Electronics Champ YouTube Channel.
Please subscribe to this channel
Thank You
*/

//Include the library


#include <tcs3200.h>

/*
Initialize the variables
for colour sensor
*/
int red;
int green;
int blue;
String sensedColour = "none";
int delayTime = 0;

/*
Initialize the variables
for motor driver
*/
int leftMotorForward = 9;
int leftMotorBackward = 10;
int rightMotorForward = 11;
int rightMotorBackward = 12;

tcs3200 sensor(4, 5, 6, 7, 8); //S0, S1, S2, S3, output pin

void setup() {

/*
Set pin 9, 10, 11
and 12 as output
*/
for (int i = 9; i < 13; i = i + 1) {

pinMode(i, OUTPUT);

}
red = sensor.colorRead('r', 20); //Scaling the sensor to 20%

Serial.begin(9600);

void loop() {

readColour();
checkColour();

if (sensedColour == "red") { //If the colour is red…..

delayTime = 2000;
delay(delayTime);
front(); //A function to move the car front
sensedColour = "none"; //Sets the sensed colour as ‘None’

else if (sensedColour == "green") { //If the colour is green…..

delayTime = 2000;
delay(delayTime);
circle(); //Function to move the car in circular path
sensedColour = "none"; //Sets the sensed colour as ‘None’

else if (sensedColour == "blue") { //If the colour is blue…..


//move the car in a triangular path
delayTime = 2000;
delay(delayTime);
delayTime = 1000;
front();
delayTime = 500;
delay(50);
right();
delayTime = 1000;
delay(50);
front();
delayTime = 500;
delay(50);
right();
delayTime = 1000;
front();
delayTime = 500;
right();
sensedColour = "none"; //Sets the sensed colour as ‘None’

if (red < 8 and green < 8 and blue < 8) { //If the colour sensor is not sensing any colour…..

digitalWrite(leftMotorForward, LOW);
digitalWrite(leftMotorBackward, LOW);
digitalWrite(rightMotorForward, LOW);
digitalWrite(rightMotorBackward, LOW); //Stops the car
sensedColour = "none";

//function to move the car forward


void front() {

digitalWrite(leftMotorForward, HIGH);
digitalWrite(rightMotorForward, HIGH);
delay(delayTime);
digitalWrite(leftMotorForward, LOW);
digitalWrite(rightMotorForward, LOW);

//function to turn the car left


void left() {

digitalWrite(leftMotorForward, LOW);
digitalWrite(rightMotorForward, HIGH);
delay(delayTime);
digitalWrite(leftMotorForward, LOW);
digitalWrite(rightMotorForward, LOW);

//function to turn the car right


void right() {

digitalWrite(leftMotorForward, HIGH);
digitalWrite(rightMotorForward, LOW);
delay(delayTime);
digitalWrite(leftMotorForward, LOW);
digitalWrite(rightMotorForward, LOW);
}

//car moves in a circular path


void circle() {

left();
left();

//function to read the colour


void readColour() {

red = sensor.colorRead('r'); //reads colour value for red


green = sensor.colorRead('g'); //reads colour value for green
blue = sensor.colorRead('b'); //reads colour value for blue

//Prints the values on the serial monitor


Serial.print("R = ");
Serial.println(red);
Serial.print("G = ");
Serial.println(green);
Serial.print("B = ");
Serial.println(blue);
Serial.println(" ");
delay(10);

//sets the colour that is sensed


void checkColour() {

if (red > green and red > blue and red > 8) { //If the colour is red......

sensedColour = "red"; //Sets the value of this variable to “red”

else if (green > red and green > blue and green > 8) { //If the colour is green......

sensedColour = "green"; //Sets the value of this variable to “green”

else if (blue > green and blue > red and blue > 8) { //If the colour is blue......

sensedColour = "blue"; //Sets the value of this variable to “blue”


}

You might also like