This video shows our Line Follower code being tested on the instructor provided test robot.
The Squash Hate Club
Monday, February 22, 2016
Thursday, February 18, 2016
Tuesday, December 15, 2015
Maze Solver Code
//this code is written for 5 line detecting sensors
#include <IRremote.h>
#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
#define leftoutsensor A1 //left outside line sensor
#define leftinsensor A2 //left inside line sensor
#define centersensor A3 //center line sensor
#define rightinsensor A4 //right inside line sensor
#define rightoutsensor A5 //right outside line sensor
#define doublecheck 200
int leftoutReading;
int leftinReading;
int centerReading;
int rightinReading;
int rightoutReading;
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()
{
readSensors(); //reads sensors
//return;
if(leftoutReading < 200 && rightoutReading < 200 && centerReading > 200)
{
straight();
}
else
{
LeftHandRule();
}
}
void readSensors()
{
leftoutReading = analogRead(leftoutsensor);
leftinReading = analogRead(leftinsensor);
centerReading = analogRead(centersensor);
rightinReading = analogRead(rightinsensor);
rightoutReading = analogRead(rightoutsensor);
}
void LeftHandRule()
{
if(leftoutReading > 200 && rightoutReading > 200) //both sides detect line
{
inch();
if(leftoutReading > 200 || rightoutReading > 200) //either side detects line again
{
done();
}
if(leftoutReading < 200 && rightoutReading < 200) //neither side detects line
{
turnLeft();
}
}
if(leftoutReading > 200) //left side detects line
{
inch();
if(leftoutReading < 200 && rightoutReading < 200) //
{
turnLeft();
}
else
{
done();
}
}
if(rightoutReading > 200)
{
inch();
if(leftoutReading > 200)
{
delay(doublecheck - 30);
readSensors();
if(rightoutReading > 200 && leftoutReading > 200)
{
done();
}
else
{
turnLeft();
return;
}
}
delay(doublecheck - 30);
readSensors();
if(leftoutReading < 200 && rightoutReading < 200 && centerReading < 200)
{
turnRight();
return;
}
readSensors();
if(leftoutReading < 200 && leftinReading < 200 && centerReading < 200
&& rightinReading < 200 && rightoutReading < 200)
{
turnAround();
}
}
}
void inch ()
{
myMotor1->run(FORWARD);
myMotor2->run(FORWARD);
myMotor1->setSpeed(50);
myMotor2->setSpeed(50);
delay(doublecheck);
readSensors();
}
void done()
{
myMotor1->run(FORWARD);
myMotor2->run(FORWARD);
myMotor1->setSpeed(0);
myMotor2->setSpeed(0);
}
void turnLeft()
{
while (analogRead(centersensor) > 200)
{
myMotor1->run(BACKWARD);
myMotor2->run(FORWARD);
myMotor1->setSpeed(50);
myMotor2->setSpeed(50);
delay(2);
}
while(analogRead(centersensor) < 200)
{
myMotor1->run(BACKWARD);
myMotor2->run(FORWARD);
myMotor1->setSpeed(50);
myMotor2->setSpeed(50);
delay(2);
}
}
void turnRight()
{
while(analogRead(centersensor) > 200)
{
myMotor1->run(FORWARD);
myMotor2->run(BACKWARD);
myMotor1->setSpeed(50);
myMotor2->setSpeed(50);
delay(2);
}
while(analogRead(centersensor) < 200)
{
myMotor1->run(FORWARD);
myMotor2->run(BACKWARD);
myMotor1->setSpeed(50);
myMotor2->setSpeed(50);
delay(2);
}
}
void straight()
{
if (analogRead(leftinsensor) > 200)
{
myMotor1->run(FORWARD);
myMotor2->run(FORWARD);
myMotor1->setSpeed(0);
myMotor2->setSpeed(50);
delay(5);
}
if (analogRead(rightinsensor) > 200)
{
myMotor1->run(FORWARD);
myMotor2->run(FORWARD);
myMotor1->setSpeed(50);
myMotor2->setSpeed(0);
delay(5);
}
else
{
myMotor1->run(FORWARD);
myMotor2->run(FORWARD);
myMotor1->setSpeed(50);
myMotor2->setSpeed(50);
delay(5);
}
}
void turnAround()
{
myMotor1->run(FORWARD);
myMotor2->run(FORWARD);
myMotor1->setSpeed(50);
myMotor2->setSpeed(50);
delay(150);
while(analogRead(centersensor) < 200)
{
myMotor1->run(FORWARD);
myMotor2->run(FORWARD);
myMotor1->setSpeed(50);
myMotor2->setSpeed(50);
delay(5);
}
}
/*
Codes Written by
Peter Fickenwirth
*/
#include <IRremote.h>
#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
#define leftoutsensor A1 //left outside line sensor
#define leftinsensor A2 //left inside line sensor
#define centersensor A3 //center line sensor
#define rightinsensor A4 //right inside line sensor
#define rightoutsensor A5 //right outside line sensor
#define doublecheck 200
int leftoutReading;
int leftinReading;
int centerReading;
int rightinReading;
int rightoutReading;
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()
{
readSensors(); //reads sensors
//return;
if(leftoutReading < 200 && rightoutReading < 200 && centerReading > 200)
{
straight();
}
else
{
LeftHandRule();
}
}
void readSensors()
{
leftoutReading = analogRead(leftoutsensor);
leftinReading = analogRead(leftinsensor);
centerReading = analogRead(centersensor);
rightinReading = analogRead(rightinsensor);
rightoutReading = analogRead(rightoutsensor);
}
void LeftHandRule()
{
if(leftoutReading > 200 && rightoutReading > 200) //both sides detect line
{
inch();
if(leftoutReading > 200 || rightoutReading > 200) //either side detects line again
{
done();
}
if(leftoutReading < 200 && rightoutReading < 200) //neither side detects line
{
turnLeft();
}
}
if(leftoutReading > 200) //left side detects line
{
inch();
if(leftoutReading < 200 && rightoutReading < 200) //
{
turnLeft();
}
else
{
done();
}
}
if(rightoutReading > 200)
{
inch();
if(leftoutReading > 200)
{
delay(doublecheck - 30);
readSensors();
if(rightoutReading > 200 && leftoutReading > 200)
{
done();
}
else
{
turnLeft();
return;
}
}
delay(doublecheck - 30);
readSensors();
if(leftoutReading < 200 && rightoutReading < 200 && centerReading < 200)
{
turnRight();
return;
}
readSensors();
if(leftoutReading < 200 && leftinReading < 200 && centerReading < 200
&& rightinReading < 200 && rightoutReading < 200)
{
turnAround();
}
}
}
void inch ()
{
myMotor1->run(FORWARD);
myMotor2->run(FORWARD);
myMotor1->setSpeed(50);
myMotor2->setSpeed(50);
delay(doublecheck);
readSensors();
}
void done()
{
myMotor1->run(FORWARD);
myMotor2->run(FORWARD);
myMotor1->setSpeed(0);
myMotor2->setSpeed(0);
}
void turnLeft()
{
while (analogRead(centersensor) > 200)
{
myMotor1->run(BACKWARD);
myMotor2->run(FORWARD);
myMotor1->setSpeed(50);
myMotor2->setSpeed(50);
delay(2);
}
while(analogRead(centersensor) < 200)
{
myMotor1->run(BACKWARD);
myMotor2->run(FORWARD);
myMotor1->setSpeed(50);
myMotor2->setSpeed(50);
delay(2);
}
}
void turnRight()
{
while(analogRead(centersensor) > 200)
{
myMotor1->run(FORWARD);
myMotor2->run(BACKWARD);
myMotor1->setSpeed(50);
myMotor2->setSpeed(50);
delay(2);
}
while(analogRead(centersensor) < 200)
{
myMotor1->run(FORWARD);
myMotor2->run(BACKWARD);
myMotor1->setSpeed(50);
myMotor2->setSpeed(50);
delay(2);
}
}
void straight()
{
if (analogRead(leftinsensor) > 200)
{
myMotor1->run(FORWARD);
myMotor2->run(FORWARD);
myMotor1->setSpeed(0);
myMotor2->setSpeed(50);
delay(5);
}
if (analogRead(rightinsensor) > 200)
{
myMotor1->run(FORWARD);
myMotor2->run(FORWARD);
myMotor1->setSpeed(50);
myMotor2->setSpeed(0);
delay(5);
}
else
{
myMotor1->run(FORWARD);
myMotor2->run(FORWARD);
myMotor1->setSpeed(50);
myMotor2->setSpeed(50);
delay(5);
}
}
void turnAround()
{
myMotor1->run(FORWARD);
myMotor2->run(FORWARD);
myMotor1->setSpeed(50);
myMotor2->setSpeed(50);
delay(150);
while(analogRead(centersensor) < 200)
{
myMotor1->run(FORWARD);
myMotor2->run(FORWARD);
myMotor1->setSpeed(50);
myMotor2->setSpeed(50);
delay(5);
}
}
/*
Codes Written by
Peter Fickenwirth
*/
Infrared Remote Code
#include <IRremote.h>
#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 RECV_PIN = 7; //defines the pin that the IR Receiver is in
IRrecv irrecv(RECV_PIN); //tells the Arduino an IR Receiver is being used
decode_results results; //tells the Arduino to decode the IR Signals
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
irrecv.enableIRIn(); //starts the infrared receiver
Serial.begin(9600); //starts monitoring the values from the analog input
}
void loop()
{
//return;
if (irrecv.decode(&results))
{
Serial.println(results.value, HEX); //sends the IR signal code to the serial monitor
/*the switch/case statement tells the Arduino what it should do
when it receives an IR signal. If the signal is unknown, it will
stop the wheels until a known signal is received.*/
switch(results.value)
{
case 0xFD807F: //if "forward" is pressed, the motors run forward
myMotor1->run(FORWARD);
myMotor2->run(FORWARD);
myMotor1->setSpeed(50);
myMotor2->setSpeed(50);
delay(50);
break;
case 0xFD20DF: //if left is pressed, the motors pivot counterclockwise
myMotor1->run(BACKWARD);
myMotor2->run(FORWARD);
myMotor1->setSpeed(50);
myMotor2->setSpeed(50);
delay(50);
break;
case 0xFD906F: //if right is pressed, the motors pivot clockwise
myMotor1->run(BACKWARD);
myMotor2->run(BACKWARD);
myMotor1->setSpeed(50);
myMotor2->setSpeed(50);
delay(50);
break;
case 0xFD609F: //if back is pressed, the motors run backwards
myMotor1->run(FORWARD);
myMotor2->run(BACKWARD);
myMotor1->setSpeed(50);
myMotor2->setSpeed(50);
delay(50);
break;
default: //if an undefined button is pressed, the motors stop
myMotor1->setSpeed(0);
myMotor2->setSpeed(0);
delay(50);;
break;
}
irrecv.resume(); // Receives the next signal
}
}
/*
Codes Written by
Peter Fickenwirth
*/
#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 RECV_PIN = 7; //defines the pin that the IR Receiver is in
IRrecv irrecv(RECV_PIN); //tells the Arduino an IR Receiver is being used
decode_results results; //tells the Arduino to decode the IR Signals
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
irrecv.enableIRIn(); //starts the infrared receiver
Serial.begin(9600); //starts monitoring the values from the analog input
}
void loop()
{
//return;
if (irrecv.decode(&results))
{
Serial.println(results.value, HEX); //sends the IR signal code to the serial monitor
/*the switch/case statement tells the Arduino what it should do
when it receives an IR signal. If the signal is unknown, it will
stop the wheels until a known signal is received.*/
switch(results.value)
{
case 0xFD807F: //if "forward" is pressed, the motors run forward
myMotor1->run(FORWARD);
myMotor2->run(FORWARD);
myMotor1->setSpeed(50);
myMotor2->setSpeed(50);
delay(50);
break;
case 0xFD20DF: //if left is pressed, the motors pivot counterclockwise
myMotor1->run(BACKWARD);
myMotor2->run(FORWARD);
myMotor1->setSpeed(50);
myMotor2->setSpeed(50);
delay(50);
break;
case 0xFD906F: //if right is pressed, the motors pivot clockwise
myMotor1->run(BACKWARD);
myMotor2->run(BACKWARD);
myMotor1->setSpeed(50);
myMotor2->setSpeed(50);
delay(50);
break;
case 0xFD609F: //if back is pressed, the motors run backwards
myMotor1->run(FORWARD);
myMotor2->run(BACKWARD);
myMotor1->setSpeed(50);
myMotor2->setSpeed(50);
delay(50);
break;
default: //if an undefined button is pressed, the motors stop
myMotor1->setSpeed(0);
myMotor2->setSpeed(0);
delay(50);;
break;
}
irrecv.resume(); // Receives the next signal
}
}
/*
Codes Written by
Peter Fickenwirth
*/
Edge Detection/Bumper Detection Code
/*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
*/
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
*/
Line Follower Code
/*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
*/
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
*/
Monday, December 7, 2015
Subscribe to:
Comments (Atom)


