Components and supplies
Arduino Nano R3
5V Cell
Jumper wires (generic)
TCS3200 Color Sensor
DC Motor, 12 V
9V battery (generic)
Breadboard mini
L298N Motor Driver
Apps and platforms
Arduino IDE
Project description
Code
Code
arduino
1/* 2 This program moves a car in three 3 different patterns based on the 4 colour sensed by the colour sensor. 5 The colour sensor is mounted on the 6 car. 7 This program is created by Shreyas for 8 Electronics Champ YouTube Channel. 9 Please subscribe to this channel 10 Thank You 11*/ 12 13//Include the library 14#include <tcs3200.h> 15 16/* 17 Initialize the variables 18 for colour sensor 19*/ 20int red; 21int green; 22int blue; 23String sensedColour = "none"; 24int delayTime = 0; 25 26/* 27 Initialize the variables 28 for motor driver 29*/ 30int leftMotorForward = 9; 31int leftMotorBackward = 10; 32int rightMotorForward = 11; 33int rightMotorBackward = 12; 34 35tcs3200 sensor(4, 5, 6, 7, 8); //S0, S1, S2, S3, output pin 36 37void setup() { 38 39 /* 40 Set pin 9, 10, 11 41 and 12 as output 42 */ 43 for (int i = 9; i < 13; i = i + 1) { 44 45 pinMode(i, OUTPUT); 46 47 } 48 49 red = sensor.colorRead('r', 20); //Scaling the sensor to 20% 50 51 Serial.begin(9600); 52 53} 54 55void loop() { 56 57 readColour(); 58 checkColour(); 59 60 if (sensedColour == "red") { //If the colour is red….. 61 62 delayTime = 2000; 63 delay(delayTime); 64 front(); //A function to move the car front 65 sensedColour = "none"; //Sets the sensed colour as ‘None’ 66 67 } 68 69 else if (sensedColour == "green") { //If the colour is green….. 70 71 delayTime = 2000; 72 delay(delayTime); 73 circle(); //Function to move the car in circular path 74 sensedColour = "none"; //Sets the sensed colour as ‘None’ 75 76 } 77 78 else if (sensedColour == "blue") { //If the colour is blue….. 79 //move the car in a triangular path 80 delayTime = 2000; 81 delay(delayTime); 82 delayTime = 1000; 83 front(); 84 delayTime = 500; 85 delay(50); 86 right(); 87 delayTime = 1000; 88 delay(50); 89 front(); 90 delayTime = 500; 91 delay(50); 92 right(); 93 delayTime = 1000; 94 front(); 95 delayTime = 500; 96 right(); 97 sensedColour = "none"; //Sets the sensed colour as ‘None’ 98 99 } 100 101 if (red < 8 and green < 8 and blue < 8) { //If the colour sensor is not sensing any colour….. 102 103 digitalWrite(leftMotorForward, LOW); 104 digitalWrite(leftMotorBackward, LOW); 105 digitalWrite(rightMotorForward, LOW); 106 digitalWrite(rightMotorBackward, LOW); //Stops the car 107 sensedColour = "none"; 108 109 } 110 111} 112 113//function to move the car forward 114void front() { 115 116 digitalWrite(leftMotorForward, HIGH); 117 digitalWrite(rightMotorForward, HIGH); 118 delay(delayTime); 119 digitalWrite(leftMotorForward, LOW); 120 digitalWrite(rightMotorForward, LOW); 121 122} 123 124 125//function to turn the car left 126void left() { 127 128 digitalWrite(leftMotorForward, LOW); 129 digitalWrite(rightMotorForward, HIGH); 130 delay(delayTime); 131 digitalWrite(leftMotorForward, LOW); 132 digitalWrite(rightMotorForward, LOW); 133 134} 135 136//function to turn the car right 137void right() { 138 139 digitalWrite(leftMotorForward, HIGH); 140 digitalWrite(rightMotorForward, LOW); 141 delay(delayTime); 142 digitalWrite(leftMotorForward, LOW); 143 digitalWrite(rightMotorForward, LOW); 144 145} 146 147//car moves in a circular path 148void circle() { 149 150 left(); 151 left(); 152 153} 154 155//function to read the colour 156void readColour() { 157 158 red = sensor.colorRead('r'); //reads colour value for red 159 green = sensor.colorRead('g'); //reads colour value for green 160 blue = sensor.colorRead('b'); //reads colour value for blue 161 162 //Prints the values on the serial monitor 163 Serial.print("R = "); 164 Serial.println(red); 165 Serial.print("G = "); 166 Serial.println(green); 167 Serial.print("B = "); 168 Serial.println(blue); 169 Serial.println(" "); 170 delay(10); 171 172} 173 174//sets the colour that is sensed 175void checkColour() { 176 177 if (red > green and red > blue and red > 8) { //If the colour is red...... 178 179 sensedColour = "red"; //Sets the value of this variable to “red” 180 181 } 182 183 else if (green > red and green > blue and green > 8) { //If the colour is green...... 184 185 sensedColour = "green"; //Sets the value of this variable to “green” 186 187 } 188 189 else if (blue > green and blue > red and blue > 8) { //If the colour is blue...... 190 191 sensedColour = "blue"; //Sets the value of this variable to “blue” 192 193 } 194 195}
Code
arduino
1/* 2 This program moves a car in three 3 different patterns based on the 4 colour sensed by the colour sensor. 5 The colour sensor is mounted on the 6 car. 7 This program is created by Shreyas for 8 Electronics Champ YouTube Channel. 9 Please subscribe to this channel 10 Thank You 11*/ 12 13//Include the library 14#include <tcs3200.h> 15 16/* 17 Initialize the variables 18 for colour sensor 19*/ 20int red; 21int green; 22int blue; 23String sensedColour = "none"; 24int delayTime = 0; 25 26/* 27 Initialize the variables 28 for motor driver 29*/ 30int leftMotorForward = 9; 31int leftMotorBackward = 10; 32int rightMotorForward = 11; 33int rightMotorBackward = 12; 34 35tcs3200 sensor(4, 5, 6, 7, 8); //S0, S1, S2, S3, output pin 36 37void setup() { 38 39 /* 40 Set pin 9, 10, 11 41 and 12 as output 42 */ 43 for (int i = 9; i < 13; i = i + 1) { 44 45 pinMode(i, OUTPUT); 46 47 } 48 49 red = sensor.colorRead('r', 20); //Scaling the sensor to 20% 50 51 Serial.begin(9600); 52 53} 54 55void loop() { 56 57 readColour(); 58 checkColour(); 59 60 if (sensedColour == "red") { //If the colour is red….. 61 62 delayTime = 2000; 63 delay(delayTime); 64 front(); //A function to move the car front 65 sensedColour = "none"; //Sets the sensed colour as ‘None’ 66 67 } 68 69 else if (sensedColour == "green") { //If the colour is green….. 70 71 delayTime = 2000; 72 delay(delayTime); 73 circle(); //Function to move the car in circular path 74 sensedColour = "none"; //Sets the sensed colour as ‘None’ 75 76 } 77 78 else if (sensedColour == "blue") { //If the colour is blue….. 79 //move the car in a triangular path 80 delayTime = 2000; 81 delay(delayTime); 82 delayTime = 1000; 83 front(); 84 delayTime = 500; 85 delay(50); 86 right(); 87 delayTime = 1000; 88 delay(50); 89 front(); 90 delayTime = 500; 91 delay(50); 92 right(); 93 delayTime = 1000; 94 front(); 95 delayTime = 500; 96 right(); 97 sensedColour = "none"; //Sets the sensed colour as ‘None’ 98 99 } 100 101 if (red < 8 and green < 8 and blue < 8) { //If the colour sensor is not sensing any colour….. 102 103 digitalWrite(leftMotorForward, LOW); 104 digitalWrite(leftMotorBackward, LOW); 105 digitalWrite(rightMotorForward, LOW); 106 digitalWrite(rightMotorBackward, LOW); //Stops the car 107 sensedColour = "none"; 108 109 } 110 111} 112 113//function to move the car forward 114void front() { 115 116 digitalWrite(leftMotorForward, HIGH); 117 digitalWrite(rightMotorForward, HIGH); 118 delay(delayTime); 119 digitalWrite(leftMotorForward, LOW); 120 digitalWrite(rightMotorForward, LOW); 121 122} 123 124 125//function to turn the car left 126void left() { 127 128 digitalWrite(leftMotorForward, LOW); 129 digitalWrite(rightMotorForward, HIGH); 130 delay(delayTime); 131 digitalWrite(leftMotorForward, LOW); 132 digitalWrite(rightMotorForward, LOW); 133 134} 135 136//function to turn the car right 137void right() { 138 139 digitalWrite(leftMotorForward, HIGH); 140 digitalWrite(rightMotorForward, LOW); 141 delay(delayTime); 142 digitalWrite(leftMotorForward, LOW); 143 digitalWrite(rightMotorForward, LOW); 144 145} 146 147//car moves in a circular path 148void circle() { 149 150 left(); 151 left(); 152 153} 154 155//function to read the colour 156void readColour() { 157 158 red = sensor.colorRead('r'); //reads colour value for red 159 green = sensor.colorRead('g'); //reads colour value for green 160 blue = sensor.colorRead('b'); //reads colour value for blue 161 162 //Prints the values on the serial monitor 163 Serial.print("R = "); 164 Serial.println(red); 165 Serial.print("G = "); 166 Serial.println(green); 167 Serial.print("B = "); 168 Serial.println(blue); 169 Serial.println(" "); 170 delay(10); 171 172} 173 174//sets the colour that is sensed 175void checkColour() { 176 177 if (red > green and red > blue and red > 8) { //If the colour is red...... 178 179 sensedColour = "red"; //Sets the value of this variable to “red” 180 181 } 182 183 else if (green > red and green > blue and green > 8) { //If the colour is green...... 184 185 sensedColour = "green"; //Sets the value of this variable to “green” 186 187 } 188 189 else if (blue > green and blue > red and blue > 8) { //If the colour is blue...... 190 191 sensedColour = "blue"; //Sets the value of this variable to “blue” 192 193 } 194 195}
Downloadable files
Schematic
Schematic
Comments
Only logged in users can leave comments
shreyas_arbatti
0 Followers
•0 Projects
Table of contents
Intro
0
0