Colour Controlled Car using Colour Sensor and Arduino
Colour Controlled Car using Colour Sensor and Arduino
/*
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;
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();
delayTime = 2000;
delay(delayTime);
front(); //A function to move the car front
sensedColour = "none"; //Sets the sensed colour as ‘None’
delayTime = 2000;
delay(delayTime);
circle(); //Function to move the car in circular path
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";
digitalWrite(leftMotorForward, HIGH);
digitalWrite(rightMotorForward, HIGH);
delay(delayTime);
digitalWrite(leftMotorForward, LOW);
digitalWrite(rightMotorForward, LOW);
digitalWrite(leftMotorForward, LOW);
digitalWrite(rightMotorForward, HIGH);
delay(delayTime);
digitalWrite(leftMotorForward, LOW);
digitalWrite(rightMotorForward, LOW);
digitalWrite(leftMotorForward, HIGH);
digitalWrite(rightMotorForward, LOW);
delay(delayTime);
digitalWrite(leftMotorForward, LOW);
digitalWrite(rightMotorForward, LOW);
}
left();
left();
if (red > green and red > blue and red > 8) { //If the colour is red......
else if (green > red and green > blue and green > 8) { //If the colour is green......
else if (blue > green and blue > red and blue > 8) { //If the colour is blue......