0% found this document useful (0 votes)
76 views2 pages

Smars Demo 1 PDF

This sketch controls a robot to move within a delimited area using a line sensor. It defines left and right motors connected to an Adafruit Motor Shield. In setup, it configures serial communication and motor speeds. In loop, it reads the line sensor value and moves motors forward if the value is below 800, indicating no line detected. It then reverses and turns left for delays before repeating.

Uploaded by

Jose Navarro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views2 pages

Smars Demo 1 PDF

This sketch controls a robot to move within a delimited area using a line sensor. It defines left and right motors connected to an Adafruit Motor Shield. In setup, it configures serial communication and motor speeds. In loop, it reads the line sensor value and moves motors forward if the value is below 800, indicating no line detected. It then reverses and turns left for delays before repeating.

Uploaded by

Jose Navarro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <AFMotor.

h>

//SMARS Demo 1 with Line sensor


//This sketch makes the robot move inside a delimitated Area (you can make a
perimeter with insulating tape)
//you'll need an Adafruit Motor shield V1 https://goo.gl/7MvZeo and a IR
sensor https://goo.gl/vPWfzx

AF_DCMotor R_motor(2); // defines Right motor connector


AF_DCMotor L_motor(1); // defines Left motor connector
// declare variables

int lineNumber; //defines the variable where it will store the


line sensor value

void setup() {
Serial.begin(9600); // sets up Serial library at 9600 bps

//changes the following values to make the robot drive as straight as


possible

L_motor.setSpeed(200); // sets L motor speed


R_motor.setSpeed(140); // sets R motor speed

R_motor.run(RELEASE); //turns L motor on


L_motor.run(RELEASE); //turns R motor on
}

void loop() {
lineNumber= analogRead(A4); //reads the sensor value from pin A4 and stores
it in the variable lineNumber

while(lineNumber<800) // repeats the following part of code until the light


sensor will find a darker zone
{

L_motor.run(FORWARD); //moves motor L Forward


R_motor.run(FORWARD); //moves motor L Forward
lineNumber= analogRead(A4); //reads the sensor value from pin A4
and stores it in the variable lineNumber

};
// the following operations will make the robot goes backward for 2 seconds
and turns left for 1.5 seconds
R_motor.run(RELEASE);
L_motor.run(RELEASE);

//go backward
R_motor.run(BACKWARD);
L_motor.run(BACKWARD);
delay(2000);

// turning left
R_motor.run(FORWARD);
L_motor.run(BACKWARD);
delay(1500);

Serial.println(lineNumber); //send the value to the serial monitor

You might also like