0% found this document useful (0 votes)
16 views3 pages

Arduino Line Follower Robot Code

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

Arduino Line Follower Robot Code

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

//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 = [Link]('r', 20); //Scaling the sensor to 20%

[Link](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 = [Link]('r'); //reads colour value for red


green = [Link]('g'); //reads colour value for green
blue = [Link]('b'); //reads colour value for blue

//Prints the values on the serial monitor


[Link]("R = ");
[Link](red);
[Link]("G = ");
[Link](green);
[Link]("B = ");
[Link](blue);
[Link](" ");
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”

Common questions

Powered by AI

The system's color sensing functionality affects its decision-making by dictating motor and LED states upon detection of specific colors. For example, detecting red increases the redMarker, influencing when the stop function is activated. Similarly, detected colors control LED outputs, providing visual signals for specific states. These decisions are made in the loop, taking the current sensedColor and the conditions defined around it to trigger the necessary actions .

The TCS3200 color sensor is integrated into the motor control system by using it to detect specific colors, which then trigger actions by the motors. For example, when the sensor detects the color red, it increments the redMarker counter until it reaches 5, at which point it stops the motor. The detected color is used to control the LED state—turning a red LED on if red is detected, and a green LED on if green is detected .

The system can be optimized for better color detection by refining the threshold values, adjusting the sensor scaling for different lighting conditions, or implementing a calibration routine to dynamically adjust sensor responses. Furthermore, optimizing the delay times based on movement speed and environmental feedback could improve performance. Enhancements might also include error-checking protocols to validate sensor readings, minimizing false positives, and integrating more complex decision-making algorithms to handle ambiguous readings .

The stopping condition implemented in the loop is based on the redMarker count reaching 5, which triggers the stop function. This condition is significant as it acts as a termination control based on a specific task completion, which in this context relates to achieving a set number of red detections. It ensures the system halts further operations, preventing continuous looping and possible motor wear or misbehavior .

Adding more color conditions would require additional logic and possibly recalibration of the sensor thresholds to distinguish more color signatures accurately. The control algorithm must be expanded to handle new conditions effectively, which could involve more complex decision trees. This expansion allows greater functionality and interaction flexibility, but it would also increase the system's processing load, which might necessitate hardware upgrades or more efficient code to maintain overall performance .

The RIR and LIR variables act as input indicators for right and left infrared sensors, respectively, helping in the line-following logic. The system reads these inputs to decide the car's movement; for instance, both being HIGH triggers forward motion, RIR LOW and LIR HIGH cause a right turn, and vice versa for a left turn. These decisions are implemented using digitalRead to check each sensor and conditional statements to define motor actions based on the readings .

The delay function impacts the system's responsiveness by causing predefined pauses after each action, potentially reducing responsiveness to rapid environmental changes. While it provides necessary timing for actuator control, excessive use might slow down the system's ability to react to new inputs, as each delay holds the system in its current state and prevents immediate subsequent actions. This is balanced to ensure LED indications, and motor actions align with the sensor's detection cycle .

The system uses GPIO pins to control motor and LED states because they serve as versatile interfaces that allow digital control of external components. Pins are set either to INPUT or OUTPUT modes. For instance, pins are set to OUTPUT to enable the forward and right movement of the motors and to control the state of LEDs based on color detection. This setup allows for simple, direct control of peripherals essential for the robotic car's operation .

The system distinguishes colors based on relative intensity readings from the TCS3200 sensor for red, green, and blue. The criteria for color detection involve comparing the intensity values of these colors; the highest value indicates the detected color, provided it exceeds a threshold of 8. This method requires that one color’s intensity must predominate over the others, ensuring clarity in detection under varying lighting conditions .

The delayTime variable determines the duration for which the motors remain in their current state after a command is given (e.g., move forward, turn left). This pause is essential to ensure that the motor's actions are sustained long enough to result in meaningful movement and to coordinate timings with sensor readings and LED indicators .

You might also like