Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 13

Line Follower Robot

Line Follower Robot

2
Controlling a DC Motor
int vSpeed = 110; // MAX 255 void loop() {
int turn_speed = 230; // MAX 255
int turn_delay = 10; left_sensor_state = analogRead(left_sensor_pin);
right_sensor_state = analogRead(right_sensor_pin);
//L293 Connection
const int motorA1 = 8; if(right_sensor_state > 500 && left_sensor_state < 500)
const int motorA2 = 10; {
const int motorAspeed = 9; Serial.println("turning right");
const int motorB1 = 12;
const int motorB2 = 13; digitalWrite (motorA1,LOW);
const int motorBspeed = 11; digitalWrite(motorA2,HIGH);
digitalWrite (motorB1,LOW);
//Sensor Connection digitalWrite(motorB2,HIGH);
const int left_sensor_pin =A0;
const int right_sensor_pin =A1; analogWrite (motorAspeed, vSpeed);
analogWrite (motorBspeed, turn_speed);
int left_sensor_state;
int right_sensor_state; }
if(right_sensor_state < 500 && left_sensor_state > 500)
void setup() { {
pinMode(motorA1, OUTPUT); Serial.println("turning left");
pinMode(motorA2, OUTPUT);
pinMode(motorB1, OUTPUT); digitalWrite (motorA1,HIGH);
pinMode(motorB2, OUTPUT); digitalWrite(motorA2,LOW);
digitalWrite (motorB1,HIGH);
Serial.begin(9600); digitalWrite(motorB2,LOW);
3 delay(3000);
}
analogWrite (motorAspeed, turn_speed); if(right_sensor_state < 500 && left_sensor_state < 500)
analogWrite (motorBspeed, vSpeed); {
Serial.println("stop");
delay(turn_delay);
} analogWrite (motorAspeed, 0);
analogWrite (motorBspeed, 0);
if(right_sensor_state > 500 && left_sensor_state > 500)
{ }
Serial.println("going forward"); }

digitalWrite (motorA2,LOW);
digitalWrite(motorA1,HIGH);
digitalWrite (motorB2,HIGH);
digitalWrite(motorB1,LOW);

analogWrite (motorAspeed, vSpeed);


analogWrite (motorBspeed, vSpeed);

delay(turn_delay);

4
Libraries
Libraries
• The Arduino environment can be extended through the use of libraries, just like most
programming platforms. Libraries provide extra functionality for use in sketches, e.g. working with
hardware or manipulating data. To use a library in a sketch, select it from Sketch > Import Library.

6
Servo Motors
Servo Motors
• There are many types of servo motors and their main feature is the ability to precisely control the
position of their shaft. A servo motor is a closed-loop system that uses position feedback to control
its motion and final position.

8
Servo

9
Code
// Include the Servo library

#include <Servo.h>
// Declare the Servo pin
int servoPin = 3;
// Create a servo object
Servo Servo1;
void setup() {
// We need to attach the servo to the used pin number
Servo1.attach(servoPin);
}
void loop(){
// Make servo go to 0 degrees
Servo1.write(0);
delay(1000);
// Make servo go to 90 degrees
Servo1.write(90);
delay(1000);
// Make servo go to 180 degrees
Servo1.write(180);
delay(1000);
}

10
Angle Control
#include //Servo library

Servo servo_test; //initialize a servo object for the connected servo

int angle = 0;

void setup()
{
servo_test.attach(9); // attach the signal pin of servo to pin9 of arduino
}

void loop()
{
for(angle = 0; angle < 180; angle += 1) // command to move from 0 degrees to 180 degrees
{
servo_test.write(angle); //command to rotate the servo to the specified angle
delay(15);
}

delay(1000);

for(angle = 180; angle>=1; angle-=5) // command to move from 180 degrees to 0 degrees
{
servo_test.write(angle); //command to rotate the servo to the specified angle
delay(5);
}

delay(1000);
}
11
Servo with potentiometer

#include //Servo library

Servo servo_test;

int angle = 0;
int potentio = A0

void setup()
{
servo_test.attach(9);

void loop()
{
angle = analogRead(potentio);
angle = map(angle, 0, 1023, 0, 179);
servo_test.write(angle);
delay(5);
}

12
4

You might also like