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

ACTIVITY 4 Without Shortest Path

The document defines an Arduino line following car project that uses motors and sensors to follow a line and detect different colored surfaces. It initializes motor objects, sets motor speeds, declares sensor pins as inputs, and defines functions to read sensor values, detect colors, and control motor directions to follow the line or turn when it is detected. The main loop calls functions to get the detected color, print sensor values, and control the motors accordingly to move forward, turn left/right, or stop based on the sensor readings and detected color.
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)
38 views3 pages

ACTIVITY 4 Without Shortest Path

The document defines an Arduino line following car project that uses motors and sensors to follow a line and detect different colored surfaces. It initializes motor objects, sets motor speeds, declares sensor pins as inputs, and defines functions to read sensor values, detect colors, and control motor directions to follow the line or turn when it is detected. The main loop calls functions to get the detected color, print sensor values, and control the motors accordingly to move forward, turn left/right, or stop based on the sensor readings and detected color.
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
You are on page 1/ 3

#include <AFMotor.

h>

//Project-1: //ARDUINO LINE FOLLOWING CAR - QUAD Robotics - A unit of Quad Store//
//www.quadstore.in

// YOU HAVE TO INSTALL THE AFMOTOR LIBRARY BEFORE UPLOAD THE CODE//
// GO TO SKETCH >> INCLUDE LIBRARY >> ADD .ZIP LIBRARY >> SELECT AF MOTOR ZIP
FILE //

//defining pins and variables


#define lefts A0
#define rights A1
#define outPin 13
#define s0 A2
#define s1 A3
#define s2 A4
#define s3 A5

boolean DEBUG = true;

// Variables
int red, grn, blu;
String color ="";
long startTiming = 0;
long elapsedTime =0;

//defining motors
AF_DCMotor motor1(1, MOTOR12_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);

void setup() {
//Setting the motor speed
motor1.setSpeed(150);
motor4.setSpeed(150);
//Declaring PIN input types
pinMode(lefts,INPUT);
pinMode(rights,INPUT);
//Begin serial communication
Serial.begin(9600);
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);
pinMode(outPin, INPUT); //out from sensor becomes input to arduino

// Setting frequency scaling to 100%


digitalWrite(s0,HIGH);
digitalWrite(s1,HIGH);

void loop(){
getColor();

if(DEBUG)printData();
elapsedTime = millis()-startTiming;
if (elapsedTime > 1000) {
startTiming = millis();
}
//Printing values of the sensors to the serial monitor
Serial.println(analogRead(lefts));
Serial.println(analogRead(rights));
//START
if(color=="BLUE"){
//Forward
motor1.run(FORWARD);
motor4.run(FORWARD);
}
//Straight
else if(analogRead(lefts)<=350 && analogRead(rights)<=350 && color!="WHITE" ){
//Forward
motor1.run(FORWARD);
motor4.run(FORWARD);
}
//line detected by left sensor
else if(analogRead(lefts)<=350 && !analogRead(rights)<=350){
//turn left
motor1.run(FORWARD);
motor4.run(BACKWARD);
}
//line detected by right sensor
else if(!analogRead(lefts)<=350 && analogRead(rights)<=350){
//turn right
motor1.run(BACKWARD);
motor4.run(FORWARD);
}
//Dead-end Turn around
else if(analogRead(lefts)<=350 && analogRead(rights)<=350 && color=="WHITE"){
//Turn around
motor1.run(FORWARD);
motor4.run(BACKWARD);
}
else if(!analogRead(lefts)<=350 && !analogRead(rights)<=350){
//stop
motor1.run(RELEASE);
motor4.run(RELEASE);
}

}
/* read RGB components */
void readRGB(){
red = 0, grn=0, blu=0;

int n = 10;
for (int i = 0; i < n; ++i){
//read red component
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
red = red + pulseIn(outPin, LOW);

//read green component


digitalWrite(s2, HIGH);
digitalWrite(s3, HIGH);
grn = grn + pulseIn(outPin, LOW);
//let's read blue component
digitalWrite(s2, LOW);
digitalWrite(s3, HIGH);
blu = blu + pulseIn(outPin, LOW);
}
red = red/n;
grn = grn/n;
blu = blu/n;
}

/***************************************************
* Showing captured data at Serial Monitor
****************************************************/
void printData(void){
Serial.print("red= ");
Serial.print(red);
Serial.print(" green= ");
Serial.print(grn);
Serial.print(" blue= ");
Serial.print(blu);
Serial.print (" - ");
Serial.print (color);
Serial.print (" detected!");
}
void getColor(){
readRGB();
if(red>3 && red<9 && grn>8 && grn<20 && blu>7 && blu<16) color = "RED";
else if(red>10 && red<22 && grn>8 && grn<17 && blu>5 && blu<10) color = "BLUE";
else if(red>3 && red<8 && grn>4 && grn<8 && blu>3 && blu<7) color = "WHITE";
else if(red>18 && red<35 && grn>20 && grn<35 && blu>18 && blu<35) color = "BLACK";
else color = "NO_COLOR";

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy