/*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 left = A2; //defines a pin for the left line sensor
int center = A1; //defines a pin for the center line sensor
int right = A0; //defines a pin for the right line sensor
int LeftSwitch = 6; //define a pin for the left Bumper
int RightSwitch = 10; //define a pin for the Right Bumper
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
pinMode(RightSwitch, INPUT_PULLUP);
pinMode(LeftSwitch, INPUT_PULLUP);
}
void loop()
{
//return;
Serial.println(digitalRead(LeftSwitch)); //Write the value of the a sensor to the serial monitor.
/*
While the switches on the front of the robot are not active, it will be looking
for edges to tell if it is going to fall off a table or run over a boundary line.
*/
while (digitalRead(LeftSwitch) == 1 && digitalRead(RightSwitch) == 1)
{
myMotor1->run(FORWARD);
myMotor2->run(FORWARD);
myMotor1->setSpeed(80);
myMotor2->setSpeed(80);
if (analogRead(left) >= 900 || analogRead(center) >= 900)
{
myMotor1->run(BACKWARD);
myMotor2->run(BACKWARD);
myMotor1->setSpeed(80);
myMotor2->setSpeed(80);
delay(1000);
myMotor1->run(FORWARD);
myMotor2->run(BACKWARD);
myMotor1->setSpeed(80);
myMotor2->setSpeed(80);
delay(2000);
}
if (analogRead(right) >= 900)
{
myMotor1->run(BACKWARD);
myMotor2->run(BACKWARD);
myMotor1->setSpeed(80);
myMotor2->setSpeed(80);
delay(1000);
myMotor1->run(BACKWARD);
myMotor2->run(FORWARD);
myMotor1->setSpeed(80);
myMotor2->setSpeed(80);
delay(2000);
}
}
delay(50);
/*
If one of the switches is pressed, the robot will back up and turn a different direction
and then procede running forward until either it comes to another obstacle, or a bright
light
*/
if (digitalRead(LeftSwitch) == 0)
{
myMotor1->run(BACKWARD);
myMotor2->run(BACKWARD);
delay(1000);
myMotor1->run(FORWARD);
myMotor2->run(BACKWARD);
delay(2000);
}
if (analogRead(RightSwitch) == 0)
{
myMotor1->run(BACKWARD);
myMotor2->run(BACKWARD);
delay(1000);
myMotor1->run(BACKWARD);
myMotor2->run(FORWARD);
delay(2000);
}
}
/*
Codes Written by
Peter Fickenwirth
*/
No comments:
Post a Comment