Lesson 30/31: Car-8 Line Tracking by Smart Car from SunFounder Arduino Kit
Lesson 30/31: Car-8 Line Tracking by Smart Car from SunFounder Arduino Kit| SunFounder Robojax
In this lesson we explaine how line tracking works and we detect black and white on the paper. then we apply it our smart car with motor driver to control the motors and keep the car on the like. We use SunFounder Arduino 3-in-1 Learning Kit.
Topics in this lesson
Use Chapters from timeline or click on the time
- 00:00 Introduction
- 1:08 Line Tracking Sensor
- 4:00 Wiring for line tracking
- 5:32 Demonstration of black line detected
- 7:24 How line tracking works on a car
- 9:07 Code for line tracking on car
- 11:34 Demonstration on serial monitor
- 12:24 Wiring explained
/*
Lesson 30/31: Car-8 Line Tracking by Smart Car from SunFounder Arduino Kit
Watch video instruction for this code on YouTube https://youtu.be/Cp2rD5RkYug
Resource page for this lesson and code:
http://robojax.com/course2/lecture30
Tutoril by Robojax.com
*/
const int in1 = 5; // in1,2 for right motor
const int in2 = 6;
const int in3 = 9; // in3,4 for left motor
const int in4 = 10;
const int lineTrack = 2;
void setup() {
Serial.begin(9600);
//motor
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
//line track
pinMode(lineTrack, INPUT);
}
void loop() {
int speed = 150;//0 to 255
int lineColor = digitalRead(lineTrack); // 0:white 1:black
//Serial.println(lineColor); //print on the serial monitor
if (lineColor) {
Serial.print("Black ");
moveLeft(speed);
} else {
Serial.print("White ");
moveRight(speed);
}
}
void moveLeft(int speed) {
Serial.println("LEFT");
analogWrite(in1, 0);
analogWrite(in2, speed);
analogWrite(in3, 0);
analogWrite(in4, 0);
}
void moveRight(int speed) {
Serial.println("RIGHT");
analogWrite(in1, 0);
analogWrite(in2, 0);
analogWrite(in3, speed);
analogWrite(in4, 0);
}