/*this tells the program itself to start
referencing from the necessary libraries to
know what certain commands in the setup and loop mean*/
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
#include <Servo.h>
//this tells the arduino that it will be using a motor shield and two DC motors run through ports 1 and 2
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *myMotor1 = AFMS.getMotor(1); //left motor
Adafruit_DCMotor *myMotor2 = AFMS.getMotor(2); //right motor
int Speed = 80;
void setup()
{
AFMS.begin(); //begins running through the motor shield
myMotor1->run(RELEASE); //begins running the motor in port 1
myMotor2->run(RELEASE); //begins running the motor in port 2
Serial.begin(9600); //starts monitoring the values from the analog input
}
void loop()
{
//return;
int left = A0; //left line sensor
int center = A1; //middle line sensor
int right = A2; //right line sensor
Serial.println(analogRead(center)); //prints the analog readigns to the serial monitor
/*
the following while loop tells the arduino to
follow a line as long as it is on a table. It will
adjust its path by stopping the motor opposite of the
line sensor that has come off the line.
*/
if (analogRead(left) < 900 && analogRead(center) > 900 && analogRead(right) < 900)
{
myMotor1->run(FORWARD);
myMotor2->run(FORWARD);
myMotor1->setSpeed(Speed);
myMotor2->setSpeed(Speed);
}
if (analogRead(left) >= 900)
{
myMotor1->run(FORWARD);
myMotor2->run(FORWARD);
myMotor1->setSpeed(Speed);
myMotor2->setSpeed(0);
}
if (analogRead(right) >= 900)
{
myMotor1->run(FORWARD);
myMotor2->run(FORWARD);
myMotor1->setSpeed(0);
myMotor2->setSpeed(Speed);
}
if(analogRead(left) >= 900 && analogRead(center) <= 900)
{
myMotor1->run(FORWARD);
myMotor2->run(BACKWARD);
myMotor1->setSpeed(Speed);
myMotor2->setSpeed(Speed);
}
if(analogRead(right) >= 900 && analogRead(center) <= 900)
{
myMotor1->run(BACKWARD);
myMotor2->run(FORWARD);
myMotor1->setSpeed(Speed);
myMotor2->setSpeed(Speed);
}
else{
myMotor1->run(FORWARD);
myMotor2->run(FORWARD);
myMotor1->setSpeed(Speed);
myMotor2->setSpeed(Speed);
}
}
/*
Codes Written by
Peter Fickenwirth
*/
No comments:
Post a Comment