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

Arduino Line Follower

The document describes code for a robot car that can detect and follow colored markers. It initializes variables to read sensor values and control motors. Functions are defined to move the car forward, left, right, and stop. The color sensor reads red, green, and blue values, and the detected color is used to control LEDs and count markers.

Uploaded by

Blagoj
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Arduino Line Follower

The document describes code for a robot car that can detect and follow colored markers. It initializes variables to read sensor values and control motors. Functions are defined to move the car forward, left, right, and stop. The color sensor reads red, green, and blue values, and the detected color is used to control LEDs and count markers.

Uploaded by

Blagoj
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

//Include the library

#include <tcs3200.h>

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

/*
Initialize the variables
for motor driver
*/
int leftMotorForward = 9;
int rightMotorForward = 10;
int RIR = 11;
int LIR = 12;
int redLED = 3;
int greeenLED = 13;
int redMarker = 0;
tcs3200 sensor(4, 5, 6, 7, 8); //S0, S1, S2, S3, output pin

void setup() {

pinMode(redLED, OUTPUT);
pinMode(leftMotorForward, OUTPUT);
pinMode(rightMotorForward, OUTPUT);
pinMode(RIR, INPUT);
pinMode(LIR, INPUT);
pinMode(greeenLED, OUTPUT);

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

Serial.begin(9600);

void loop() {
if(redMarker < 5){
readColour();
checkColour();

if((digitalRead(RIR) == 1)&&(digitalRead(LIR) == 1)){


front(); //A function to move the car front
delay(delayTime);
}

if((digitalRead(RIR) == 0)&&(digitalRead(LIR) == 1)){


right();
delay(delayTime);
}
if((digitalRead(RIR) == 1)&&(digitalRead(LIR) == 0)){
left();
delay(delayTime);
}

if((digitalRead(RIR) == 0)&&(digitalRead(LIR) == 0)){


front(); //A function to move the car front
delay(delayTime);
}

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


redMarker = redMarker + 1;
digitalWrite(redLED , HIGH);
digitalWrite(greenLED , LOW);
sensedColour = "none"; //Sets the sensed colour as ‘None’

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

digitalWrite(redLED , LOW);
digitalWrite(greenLED , HIGH);
sensedColour = "none"; //Sets the sensed colour as ‘None’

if(redMarker == 5){
stop();
}
}
}

//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);

void stop() {
digitalWrite(leftMotorForward, LOW);
digitalWrite(rightMotorForward, LOW);
delay(delayTime);
}

//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 && red > blue && red > 8) { //If the colour is red......

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

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

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

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

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

You might also like