final lab, arduino robot challenge

33
Wentworth Institute of Technology ELEC2950-02 Embedded Computer Systems Professor Marpaung: Final Exam Arduino-Robot Lab 2 December 2016 Lab Group: James Smith, Carlfred Malcolm

Upload: james-smith

Post on 07-Feb-2017

26 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Final Lab, Arduino Robot Challenge

Wentworth Institute of TechnologyELEC2950-02 Embedded Computer Systems

Professor Marpaung:Final Exam Arduino-Robot Lab

2 December 2016Lab Group:

James Smith, Carlfred Malcolm

Page 2: Final Lab, Arduino Robot Challenge

Section 1: Objectives

Using an Arduino, teams of two are tasked with programming a robot that can navigate a tape playing-field, pictured below, in order to either knock down a number of water bottles, or hunt down the opposing teams robot. This is to be accomplished using a number of sensors, including ultrasonic and infrared sensors. A whisker, or lance, may be employed in the knocking down the water bottles, requiring a majority to be knocked down by the victorious team. Likewise, the whisker may be employed in hunting down the opposing teams’ robots. Hunting down a robot requires the robot or lance to make contact with either the back or the sides of the opposing robot, the front of the robot is not vulnerable to such attacks. These robots are pitted against one another, in order to discover whose program was most successful in completing the stated objectives. This competition will be organized into a set of brackets.

Playing-Field Design

Section 2: Flowchart Description of Code

Page 3: Final Lab, Arduino Robot Challenge

Overall Flowchart

Pictured above is a simplified view of how the code we produced operates. What occurs is, upon beginning the code, infrared sensors are read from the bottom of the robot, relaying information regarding the color value of the material underneath the robot. From these values, it can be attained how the robot must travel in order to follow the black tape lines of the playing-field; adjusting to continue on the line, or continuing straight. This information also tells us whether or not the robot has reached an intersection in the tape path. With this information, the robot proceeds to complete a specified task in order to fulfill the objectives of the lab. This includes, but is not limited to, turning left at a four-way or ‘T’ intersection, deciding to turn right or continue straight, or deciding whether to turn left or continue straight. Upon the completion of any of these tasks, including the tasks required to follow the line, the code returns to the beginning of the void loop, in order to scan the infrared sensors once again to receive up to date information about where on the course the robot lies.

Page 4: Final Lab, Arduino Robot Challenge

In-Depth Flowchart

In more detail, the code begins; proceeds to read infrared sensors on the bottom of the robot, of which there are five. This information is then relayed back to the Arduino, where these long values are rounded into Boolean values, in order to make them more easily usable. With this information, we can tell where on the line the robot lies, for example if the robot has drifted to the right of the line, then the sensors may read that the center and right sensors are on the black line, while the rest of the sensors remain above the white playing field. With this information, the robot enters an if statement that slows down the opposite motor in order to allow the robot to readjust, centering the robot once again.

The information gathered by the infrared sensors also tells us when the robot has entered an intersection, reading all blacks during a four-way or ‘T’ intersection for example. Using this information, the type of intersection can be conceived, allowing the code to enter specific if statements. If the robot detects that it has entered a four-way or ‘T’ intersection, then the robot enters an if statement, with a for loop in order to move the forward slightly, becoming perpendicular to the turn it is about to navigate; entering another for loop that spins the robot roughly 90o to the left. When the robot enters an intersection for a straight or right turn, then initially the robot proceeds straight. However, the robot then adds a unit of 1 to a counter for these types of intersections which forces the robot to take a right whenever it encounters another intersection of this type. When the sensors tell us that the robot has entered a left or straight intersection, it will initially turn left. After completing this left, a value of 1 is added to another separate counter, forcing the robot to travel straight during the its next encounter. Once the robot has traveled straight through this intersection, a value of 1 is subtracted from this counter, allowing the robot to turn left during the next intersection of this type. After any of these commands have been completed, the code returns to the beginning, where it once again scans the infrared sensors, and relays to the robot how to further proceed.

Section 3: Explanation of Code

This robot relies on an array of five infrared sensors in order to navigate a tape playing-field, pictured above in section 1. These infrared sensors tell the robot where it is in relation to the lines of the playing field, and is organized using if statements to decide how the engines should move. In the act of following these playing-field lines, the infrared sensors tell the robot where it is on the playing-field, allowing the programming of the motors to react differently depending on the binary combination of these infrared sensors. When the robot has drifted right or left, the opposite motor is slowed slightly in order to compensate for this, and recenter the robot on the playing-field lines. These line following statements only occur during times of none to two sensors registering a black line beneath it. This allows the robot to distinguish when it is simply readjusting to the line, compared to when it enters an intersection.

When a robot enters and intersection, the infrared sensors will identify that the entirety of one side of the array is reading a black line, with at least the far, opposite side sensor seeing the white playing field, or both the far side sensor and the sensor just opposite the side of the intersection. What this means is that, for example, when an intersection on the right occurs, either only the far left sensor will detect the white playing field, or both the left and far left sensor will. It was also clear when a four-way or ‘T’ intersection was reached, because both of the outside sensors would detect a black tape line. Upon entering these if statements, for loops are employed in order to direct the robot to complete tasks

Page 5: Final Lab, Arduino Robot Challenge

that we dictate it to do. These include turning left during a four-way or ‘T’ intersection, alternating between turning left and proceeding straight during a left side intersection, or running straight for the first right intersection, and turning right during the rest of them.

The goal of these intersection commands was to knock over the water bottle objectives as quickly and consistently as possible. In order to complete this goal, these intersections were strategically coded. The first intersection was a right sided intersection, which the robot continued straight through. The next was also a right sided intersection, which the robot turned right and proceeded toward the center bottle. Upon hitting the bottle, the robot entered a four-way intersection, which it turned left and proceeded to the top of the playing field. This led the robot to a ‘T’ intersection, which forced the robot to once again turn left and proceed to the bottle directly in front of it, during the start of the competition. After hitting this bottle, the robot turns left and begins alternating between straight and left across the remaining left sided intersections. This allowed the robot to begin circumnavigating the playing-field, keeping it from reentering the middle of the field, and eventually reaching the third bottle.

Section 4: Process and Creation of Code

The development of this code was not a linear path between beginning to end. Originally, for around three weeks, the objective of the lab and the progression of the code relied on solving a maze designed in tape, which contained a few obstacles in order to maintain a sense of randomness and chaos. This employed the use of infrared sensors, as well as an ultrasonic sensor which was able to detect the distance of an obstacle in front of it using sonar technology. As time continued, we began to flesh out the workings of both types of sensors, as well as an algorithm to complete a maze as quickly as possible, using ideas from the Trémaux's algorithm in order to mark intersections the robot has previously visited in order to find the exit quickly as well as avoid traveling through the same intersection twice unless there were no other options present. This algorithm was scrapped upon the redefining of the lab objectives, changing the goal of the lab from completing a maze, to knocking over bottles in a 2x2 square tile playing field.

Initially, much of the time was spent tweaking the robot in order to have enough information to complete the course as quickly as possible. This required adding more sensors to the robot, adding two more sensors to the bottom of the robot; the array now includes five infrared sensors as compared to the original three. A photovoltaic sensor was also added to the front of the robot in order to distinguish when an intersection was a corner of the playing field, or when the intersection also contained the option for the robot to continue straight. The use of this photovoltaic sensors was dumped when consistency of the reading values became a problem due to shadows it, among other things, cast below this sensor, causing it to read a black line being there regardless of whether it was there or not. The ultrasonic sensor was also placed out of commission when the robot no longer needed to detect whether or not an obstacle stood in its path, and the objectives where known prior to the start of the competition.

After successfully getting the sensors to work fairly consistently in tandem with one another, we began to attempt to get the robot to follow a black line, experimenting with many stiles of loops and statements. Starting with while loops, which seemed like an obvious choice at the time, but having little success without large amounts or redundancy, reading the sensors within most loops in order to exit the current while loop and enter another. This method was toyed with for a long time, making progress and minor breakthroughs, but after a long time with slow progress, the entire code was scrapped and begun

Page 6: Final Lab, Arduino Robot Challenge

again with if statements. This allowed for the reading of the sensors to occur initially in the code, and be returned to upon the completion of the if statement. During testing, an else statement was present in order to detect when the robot entered an area, or received a combination of sensor readings that it had no combination for. This else statement caused the robot print the phrase “Error!”, in order to mark the combination that was previously unaccounted for, and spin in a circle in place until the robot was shut off. This strategy allowed for the fleshing out of all possible combinations in order to follow a line very robustly, and was tested under considerably rough circumstances.

After the robot could accurately and consistently follow a line, a strategy was fleshed out as to how the robot was intended to proceed in order to knock down all three bottles as quickly as we found possible. Using combinations of binary inputs, we were able to go through all possible combinations of sensor readings, from 0.0.0.0.0 to 1.1.1.1.1, deciding what each combination would mean to the robot, and what combinations were unlikely or impossible to occur under these circumstances. For loops were used in order to have the robot react to the intersections consistently, requiring only tweaking to allow the robot to turn 90o, as well as fall centered on the next line and continue straight. The else statement remained, allowing us to include combinations that occurred which we did not fully consider likely, but occurred occasionally, for example running into an intersection at the same time that the robot was attempting to readjust to follow the line. These methods were used for roughly three different generations of the same code, becoming both more robust and more efficient with each new iteration.

Section 5: Code Limitations

The limitations of the code include minor glitches, lack of any type of defense from a hunting robot, as well as a predictability as to where the robot is going to travel during the course. Much of the limitations also came from the hardware of the robot, where during the competition, a sensor was disconnected from its grounding and no longer read properly. The code itself also acted strangely, stuttering occasionally during times of steep adjustment to following the line, which doesn’t occur while the robot it turning during an intersection nor when it is following the line straight forward, a bug which I could not explain nor could I fix in time for the competition. Of these minor glitches included rare occasions where the robot was very off track from the line while it entered an intersection, providing false information to the robot and allowing it to act erratically in response to these inputs. Because these glitches happened very rarely, the only thing to be done in order to correct them is make the line following algorithm more rigorous in order to not become very off track often. Another limitation of our code was originally the code detecting a four-way or ‘T’ intersection required all the sensors to detect a black line, causing problems when the robot came in at an angle, this was fixed however when the parameters for these intersections were changed in order to detect when at least both the outside detectors recognize the black line, instead of all of them recognizing them.

Page 7: Final Lab, Arduino Robot Challenge

Section 6: Photo Documentation of Process

Step 1: Travel through Step 2: Turn Left to Attack Step 3: Turn Towards the Primary Intersection First Bottle Outside of the Playing-Field

Step 4: Turn Left to Attack Step 5: Continue Towards Corner Step 6: Begin Circumnavigating Second Bottle Of the Playing-Field the Outside of the Playing-Field

Section 7: Skills Developed and Tested

This lab challenged my ability to think both objectively and creatively in order to complete the assigned task with as much efficiency and consistency as possible within a limited time span. Along with testing my ability to design and create working code, I was also tasked with coming up with a strategy for completing the competition faster and more consistently than my fellow classmates. In order to compete with my fellow classmates, I needed to employ the use of sensors as efficiently and effectively as possible. This allowed me to experiment with coding new types of sensors including the ultrasonic sensor, the photovoltaic sensor, and the infrared sensors which became the backbone of the code’s design. During the original portion of the lab also allowed me to learn about some of the many strategies employed in order to solve a maze, as well as how to us sensors in order to make the robot react predictably to stimuli which included following a line, as well as executing commands depending on the orientation of said line. By implementing all of the skills learned in this course and through other coding projects, I was able to manipulate motors and sensors, as well as organize the code into efficient and understandable pieces.

Other skills tried during this process was organizational, including writing down the thought process that I undertook in order to try and solidify the code and minimize redundancy and unnecessary

Page 8: Final Lab, Arduino Robot Challenge

pieces within the code that would otherwise provide glitches, and inefficiencies within the code. This tried my ability as an engineer to define the problem as accurately and effectively as possible in order to approach an applicable solution.

Section 8: Closing Notes

This lab proved to be incredibly difficult for a number of reasons, of which the greatest enemy for me was time. Being presented with unique and challenging problems, like this lab, only works to improve my abilities as an engineer. I look forward to being presented with other challenges in the future and believe that some of the lessons that I have gathered throughout this assignment will help to shape unique and effective solutions to engineering problems in the future.

Appendix A: Developmental Diary

Date Time Things Accomplished Things Remaining11/4 13:00 Attach three infrared sensors to the

robot.Attach ultrasonic sensor to the robot.Write code to read infrared sensors.

Write code to read ultrasonic sensor.Round Sensors into usable data.Code motors to move when a black line is detected.Design maze solving algorithm.

11/9 12:00 Coded Ultrasonic Sensor to detect objects in front of robot.

Round Sensors into usable data.Code motors to move when a black line is detected.Design maze solving algorithm.

11/11 13:00 Add additional infrared sensors, making the total number of infrared sensors 5.Add a photocell and write code to include it.Round sensor values to Boolean or int in centimeters for the ultrasonic sensors.

Code motors in order to follow a line.Design an algorithm to solve a maze.

11/16 12:00 Attempt to follow a line using while loops, little success.Reevaluate new goals based on new lab parameters.

Decide on a strategy to knock down bottles, or hunt down opposing robots.Code motors to follow a line.

11/18 13:00 Decide on how to proceed with new lab parameters, begin to discuss

Successfully follow a line.

Page 9: Final Lab, Arduino Robot Challenge

Date Time Things Accomplished Things Remainingpossible options for completing assignment.Redesign strategy employed in following a line, began working with if statements opposed to while loops.Discontinue the use of both the Ultrasonic sensor as well as the photovoltaic cell.

Solidify a plan of action for knocking down bottles, hunting opposing robots deemed too inefficient and unreliable.

11/22 12:00 Begin to follow a line, and flesh out sum total of all possibilities of adjusting to follow the line.Develop a strategy for knocking down bottles.

Effectively and consistently follow a line.Code decision making into the robot to take turns that knock over all the bottles.

11/23 12:00 Successfully follow a line 9/10 times and under moderately tough circumstances.

Coding the ability to turn at intersections in order to knock over bottles.

11/25 19:00 Write down objectives of the code, allowing for a more solidified idea of what the robot has to accomplish and how it may be able to do that.Scrap original code, saving some pieces from the line following algorithm.

Coding the ability to turn at intersections in order to knock over bottles.Effectively and consistently follow a line.

11/26 18:30 Write code that allows for the turning of the robot at intersections using for loops.

Tweak for loops to turn 90o and remain on the targeted line.Effectively and consistently follow a line.

11/27 15:00 Write algorithm that relies on a counter to decide whether or not to take the turn presented to the robot, a key part in the actual functioning of the robot.

Test the code on the robot and continue to flesh out if statements as the robot detects errors.

11/29 12:00 Add more if statements and tweak statement conditions in order to have a robust algorithm that rarely ran into problems that would otherwise stop the robot.Tweak for loops to become more precise during turns.

Complete the course with consistency.Tweak code to try and work past minor glitches that occurred, including reaching an intersection while also adjusting to the line.

12/1 12:00 Complete the course with the robot knocking down all the bottles a majority of the time.

Minor tweaking to the code to fix minor bugs and make turns more precise

Page 10: Final Lab, Arduino Robot Challenge

Date Time Things Accomplished Things Remaining12/2 7:00 Solidify for loops and confirm the

proper functioning of the robot prior to the competition’s opening.

Compete.

Appendix B-1: Code on November 5, 2016

#include <SoftwareSerial.h>

SoftwareSerial BTserial(2, 3); //TX, RX

int left;

int center;

int right;

long result=0;

void setup(void)

{

Serial.begin(9600);

pinMode(13, OUTPUT); //Right Wheel

pinMode(12, OUTPUT); //Left Wheel

pinMode(11, INPUT); //Ultrasonic Sensor

}

void loop()

{

left=IRScan(6);

center=IRScan(7);

right=IRScan(8);

delay(10);

Serial.print("Left ");

Serial.print(left);

Serial.print(" Center ");

Serial.print(center);

Serial.print(" Right ");

Page 11: Final Lab, Arduino Robot Challenge

Serial.print(right);

Serial.println("");

}

long IRScan(int sensPin)

{

long result = 0;

pinMode(sensPin, OUTPUT); // make pin OUTPUT

digitalWrite(sensPin, HIGH); // make pin HIGH to discharge capacitor - study the schematic

delay(1); // wait a ms to make sure cap is discharged

pinMode(sensPin, INPUT); // turn pin into an input and time till pin goes low

digitalWrite(sensPin, LOW); // turn pullups off - or it won't work

while(digitalRead(sensPin)){ // wait for pin to go low

result++;

}

return result; //With Light: Black= White= Blue=

}

Appendix B-2: Code on November 12, 2016

#include <SoftwareSerial.h>

SoftwareSerial BTserial(2, 3); //TX, RX

int left;

int center;

int right;

int i = 0;

long result=0;

unsigned long Distance = 0;

int USsensor = 11; // Ultrasound signal pin

unsigned long USvalue = 0;

void setup(void)

{

Page 12: Final Lab, Arduino Robot Challenge

Serial.begin(9600);

pinMode(13, OUTPUT); //Right Wheel

pinMode(12, OUTPUT); //Left Wheel

pinMode(USsensor, OUTPUT); //Ultrasonic Sensor

}

void loop()

{

left=IRScan(6);

center=IRScan(7);

right=IRScan(8);

delay(10);

Serial.print("Left ");

Serial.print(left);

Serial.print(" Center ");

Serial.print(center);

Serial.print(" Right ");

Serial.print(right);

Serial.println("");

i = US();

Serial.print(i);

Serial.println(" CM");

delay(250); //delay 1/4 seconds.

}

long IRScan(int sensPin)

{

long result = 0;

pinMode(sensPin, OUTPUT); // make pin OUTPUT

digitalWrite(sensPin, HIGH); // make pin HIGH to discharge capacitor - study the schematic

delay(1); // wait a ms to make sure cap is discharged

Page 13: Final Lab, Arduino Robot Challenge

pinMode(sensPin, INPUT); // turn pin into an input and time till pin goes low

digitalWrite(sensPin, LOW); // turn pullups off - or it won't work

while(digitalRead(sensPin)){ // wait for pin to go low

result++;

}

return result; //With Light: Black= White= Blue=

}

unsigned long US()

{

pinMode(USsensor, OUTPUT); // Switch signalpin to output

digitalWrite(USsensor, LOW); // Send low pulse

delayMicroseconds(2); // Wait for 2 microseconds

digitalWrite(USsensor, HIGH); // Send high pulse

delayMicroseconds(5); // Wait for 5 microseconds

digitalWrite(USsensor, LOW); // Holdoff

pinMode(USsensor, INPUT); // Switch signalpin to input

digitalWrite(USsensor, HIGH); // Turn on pullup resistor

// please note that pulseIn has a 1sec timeout, which may

// not be desirable. Depending on your sensor specs, you

// can likely bound the time like this -- marcmerlin

// Distance = pulseIn(USsensor, HIGH, 38000)

Distance = pulseIn(USsensor, HIGH); //Listen for Distance

USvalue = (Distance / 58.138); //convert to CM

return USvalue;

}

Appendix B-3: Code on November 19, 2016

int Fleft, left, Fright, right, center, reach, i=0, pivot=0;

boolean FL, L, C, R, FR;

long result=0;

Page 14: Final Lab, Arduino Robot Challenge

unsigned long Distance = 0;

int USsensor = 11; // Ultrasound signal pin

unsigned long USvalue = 0;

void setup(void)

{

Serial.begin(9600);

pinMode(13, OUTPUT); //Right Wheel

pinMode(12, OUTPUT); //Left Wheel

pinMode(USsensor, OUTPUT); //Ultrasonic Sensor

}

void loop()

{

// Scanning Underbelly, IR Scanners

Fright=IRScan(8);

right=IRScan(5);

center=IRScan(7);

left=IRScan(9);

Fleft=IRScan(6);

if(Fright > 200){

FR=1;

}

else FR=0;

if(right > 200){

R=1;

}

else R=0;

if(center > 200){

C=1;

}

Page 15: Final Lab, Arduino Robot Challenge

else C=0;

if(left > 200){

L=1;

}

else L=0;

if(Fleft > 200){

FL=1;

}

else FL=0;

Serial.print ("Far Left ");

Serial.print(FL);

Serial.print(" Left ");

Serial.print(L);

Serial.print(" Center ");

Serial.print(C);

Serial.print(" Right ");

Serial.print(R);

Serial.print(" Far Right ");

Serial.print(FR);

Serial.println("");

if( FL==0 && L==0 && C==1 && R==0 && FR==0){//Straight Forward

digitalWrite(13, HIGH);

delayMicroseconds(1400);

digitalWrite(13, LOW);

delay(10);

digitalWrite(12, HIGH);

delayMicroseconds(1600);

digitalWrite(12, LOW);

delay(10);

Page 16: Final Lab, Arduino Robot Challenge

}

else if( FL==0 && L==0 && C==1 && R==1 && FR==0){//Adjust Left

digitalWrite(13, HIGH);

delayMicroseconds(1350);

digitalWrite(13, LOW);

delay(10);

digitalWrite(12, HIGH);

delayMicroseconds(1600);

delay(10);

}

else if( FL==0 && L==0 && C==1 && R==1 && FR==1){

for (pivot=0; pivot<60; pivot++)//Pivot the Robot right, approximately 90 degrees

{

digitalWrite(13, HIGH);

delayMicroseconds(1300);//Rigth Motor Standstill

digitalWrite(13, LOW);

delay(10);

digitalWrite(12, HIGH);

delayMicroseconds(1300);//Left Motor Backwards

digitalWrite(12, LOW);

delay(10);

}

}

else if( FL==0 && L==0 && C==1 && R==1 && FR==1){//Rigth Turn or Straight

}

else if( FL==0 && L==1 && C==1 && R==0 && FR==0){//Adjust Right

digitalWrite(13, HIGH);

delayMicroseconds(1300);

digitalWrite(13, LOW);

Page 17: Final Lab, Arduino Robot Challenge

delay(10);

digitalWrite(12, HIGH);

delayMicroseconds(1550);

delay(10);

}

else if( FL==1 && L==1 && C==1 && R==0 && FR==0){

for (pivot=0; pivot<60; pivot++)//Pivot the Robot left, approximately 90 degrees

{

digitalWrite(13, HIGH);

delayMicroseconds(1700);

digitalWrite(13, LOW);

delay(10);

digitalWrite(12, HIGH);

delayMicroseconds(1700);

digitalWrite(12, LOW);

delay(10);

}

}

else if( FL==1 && L==1 && C==1 && R==0 && FR==0){//Left Turn or Straight

}

else if( FL==1 && L==1 && C==1 && R==1 && FR==1){//T-Intersection

}

else if( FL==0 && L==0 && C==1 && R==0 && FR==0){//4-Way Intersection

}

}

long IRScan(int sensPin)

{

long result = 0;

pinMode(sensPin, OUTPUT);

Page 18: Final Lab, Arduino Robot Challenge

digitalWrite(sensPin, HIGH);

delay(1);

pinMode(sensPin, INPUT);

digitalWrite(sensPin, LOW);

while(digitalRead(sensPin))

{

result++;

}

return result;

}

Appendix C: Final Code

int Fright, right, center, left, Fleft, motion, d=0, a=0;

boolean FL, L, C, R, FR;

long result=0;

void setup(void)

{

Serial.begin(9600);

pinMode(13, OUTPUT); //Right Wheel Forward == 1700

pinMode(12, OUTPUT); //Left Wheel Forward == 1300

}

void loop() {

// Scanning Underbelly, IR Scanners ------------------------------------------------------------------

right=IRScan(5);

Fleft=IRScan(6);

center=IRScan(7);

Fright=IRScan(8);

left=IRScan(9);

Page 19: Final Lab, Arduino Robot Challenge

//Rounding IR Sensors to Binary -----------------------------------------------------------------------

if(Fright > 200){

FR=1;}

else FR=0;

if(right > 200){

R=1;}

else R=0;

if(center > 200){

C=1;}

else C=0;

if(left > 200){

L=1;}

else L=0;

if(Fleft > 200){

FL=1;}

else FL=0;

//Print Sensor Values for Troubleshooting ------------------------------------------------------------

Serial.print ("Far Left ");

Serial.print(FL);

Serial.print(" Left ");

Serial.print(L);

Serial.print(" Center ");

Serial.print(C);

Serial.print(" Right ");

Serial.print(R);

Serial.print(" Far Right ");

Serial.print(FR);

Page 20: Final Lab, Arduino Robot Challenge

Serial.println("");

//Basic Line Following Commands ------------------------------------------------------------------------

if( FL==0 && L==0 && C==1 && R==0 && FR==0){//Straight Forward

digitalWrite(13, HIGH);

delayMicroseconds(1700);//Right Wheel

digitalWrite(13, LOW);

delay(5);

digitalWrite(12, HIGH);

delayMicroseconds(1300);//Left Wheel

digitalWrite(12, LOW);

delay(5);

}

else if( FL==0 && L==0 && C==0 && R==0 && FR==0){//Straight Forward

digitalWrite(13, HIGH);

delayMicroseconds(1700);//Right Wheel

digitalWrite(13, LOW);

delay(5);

digitalWrite(12, HIGH);

delayMicroseconds(1300);//Left Wheel

digitalWrite(12, LOW);

delay(5);

}

else if( FL==0 && L==0 && C==1 && R==1 && FR==0){//Adjust Left

digitalWrite(13, HIGH);

delayMicroseconds(1700);//Right Wheel

digitalWrite(13, LOW);

delay(5);

digitalWrite(12, HIGH);

Page 21: Final Lab, Arduino Robot Challenge

delayMicroseconds(1475);//Left Wheel

digitalWrite(12, LOW);

delay(5);

}

else if( FL==0 && L==1 && C==1 && R==0 && FR==0){//Adjust Right

digitalWrite(13, HIGH);

delayMicroseconds(1525);//Right Wheel

digitalWrite(13, LOW);

delay(5);

digitalWrite(12, HIGH);

delayMicroseconds(1300);//Left Wheel

digitalWrite(12, LOW);

delay(5);

}

else if( FL==0 && L==1 && C==0 && R==0 && FR==0){//Adjust Right

digitalWrite(13, HIGH);

delayMicroseconds(1525);//Right Wheel

digitalWrite(13, LOW);

delay(5);

digitalWrite(12, HIGH);

delayMicroseconds(1300);//Left Wheel

digitalWrite(12, LOW);

delay(5);

}

else if( FL==1 && L==1 && C==0 && R==0 && FR==0){//Adjust Right

digitalWrite(13, HIGH);

delayMicroseconds(1525);//Right Wheel

digitalWrite(13, LOW);

delay(5);

Page 22: Final Lab, Arduino Robot Challenge

digitalWrite(12, HIGH);

delayMicroseconds(1300);//Left Wheel

digitalWrite(12, LOW);

delay(5);

}

else if( FL==1 && L==0 && C==0 && R==0 && FR==0){//Adjust Right

digitalWrite(13, HIGH);

delayMicroseconds(1500);//Right Wheel

digitalWrite(13, LOW);

delay(5);

digitalWrite(12, HIGH);

delayMicroseconds(1300);//Left Wheel

digitalWrite(12, LOW);

delay(5);

}

else if( FL==0 && L==0 && C==0 && R==1 && FR==0){//Adjust Left

digitalWrite(13, HIGH);

delayMicroseconds(1700);//Right Wheel

digitalWrite(13, LOW);

delay(5);

digitalWrite(12, HIGH);

delayMicroseconds(1475);//Left Wheel

digitalWrite(12, LOW);

delay(5);

}

else if( FL==0 && L==0 && C==0 && R==1 && FR==1){//Adjust Left

digitalWrite(13, HIGH);

delayMicroseconds(1700);//Right Wheel

digitalWrite(13, LOW);

Page 23: Final Lab, Arduino Robot Challenge

delay(5);

digitalWrite(12, HIGH);

delayMicroseconds(1475);//Left Wheel

digitalWrite(12, LOW);

delay(5);

}

else if( FL==0 && L==0 && C==0 && R==0 && FR==1){//Adjust Left

digitalWrite(13, HIGH);

delayMicroseconds(1700);//Right Wheel

digitalWrite(13, LOW);

delay(5);

digitalWrite(12, HIGH);

delayMicroseconds(1500);//Left Wheel

digitalWrite(12, LOW);

delay(5);

}

//Intersection Commands

//Straight Right -------------------------------------------------------------------------------------

else if((FL==0 && L==0 && C==1 && R==1 && FR==1)||(FL==0 && L==1 && C==1 && R==1 && FR==1)){//Straight Right

if(d==0){//First Intersection, Straight

for(motion=0; motion<20; motion++){//Straight to Exit Intersection

digitalWrite(13,HIGH);

delayMicroseconds(1700); //Right Motor Forward

digitalWrite(13,LOW);

delay(5);

digitalWrite(12,HIGH);

delayMicroseconds(1300); //Left Motor Forward

Page 24: Final Lab, Arduino Robot Challenge

digitalWrite(12,LOW);

delay(5);

}

d++;

}

else if (d==1){//Second Intersection, Right

for(motion=0; motion<13; motion++){//Inch Forward Slightly

digitalWrite(13,HIGH);

delayMicroseconds(1700); //Right Motor Forward

digitalWrite(13,LOW);

delay(5);

digitalWrite(12,HIGH);

delayMicroseconds(1300); //Left Motor Forward

digitalWrite(12,LOW);

delay(5);

}

for(motion=0; motion<42; motion++){//Turn Right

digitalWrite(13,HIGH);

delayMicroseconds(1700); //Right Motor Forward

digitalWrite(13,LOW);

delay(5);

digitalWrite(12,HIGH);

delayMicroseconds(1700); //Left Motor Backward

digitalWrite(12,LOW);

delay(5);

}

}

}

Page 25: Final Lab, Arduino Robot Challenge

//Left Anything --------------------------------------------------------------------------------------

else if((FL==1 && L==1 && C==1 && R==0 && FR==0)||(FL==1 && L==1 && C==1 && R==1 && FR==0)){//Left Anything

if(a==0){//Turn Left

for(motion=0; motion<13; motion++){//Inch Forward

digitalWrite(13,HIGH);

delayMicroseconds(1700); //Right Motor Forward

digitalWrite(13,LOW);

delay(5);

digitalWrite(12,HIGH);

delayMicroseconds(1300); //Left Motor Forward

digitalWrite(12,LOW);

delay(5);

}

for(motion=0; motion<42; motion++){//Turn Left

digitalWrite(13,HIGH);

delayMicroseconds(1300); //Right Motor Backward

digitalWrite(13,LOW);

delay(5);

digitalWrite(12,HIGH);

delayMicroseconds(1300); //Left Motor Forward

digitalWrite(12,LOW);

delay(5);

}

a++;

}

else if(a==1){//Move Straight

for(motion=0; motion<20; motion++){

digitalWrite(13,HIGH);

Page 26: Final Lab, Arduino Robot Challenge

delayMicroseconds(1700); //Right Motor Forward

digitalWrite(13,LOW);

delay(5);

digitalWrite(12,HIGH);

delayMicroseconds(1300); //Left Motor Forward

digitalWrite(12,LOW);

delay(5);

}

a--;

}

}

//T-Intersection / 4-Way -----------------------------------------------------------------------------

else if( FL==1 && FR==1){

for(motion=0; motion<13; motion++){//Inch Forward Slightly

digitalWrite(13,HIGH);

delayMicroseconds(1700); //Right Motor Forward

digitalWrite(13,LOW);

delay(5);

digitalWrite(12,HIGH);

delayMicroseconds(1300); //Left Motor Forward

digitalWrite(12,LOW);

delay(5);

}

for(motion=0; motion<42; motion++){//Turn Left

digitalWrite(13,HIGH);

delayMicroseconds(1300); //Right Motor Backward

digitalWrite(13,LOW);

delay(5);

Page 27: Final Lab, Arduino Robot Challenge

digitalWrite(12,HIGH);

delayMicroseconds(1300); //Left Motor Forward

digitalWrite(12,LOW);

delay(5);

}

}

}

//IR Scanner Statement ---------------------------------------------------------------------------------

long IRScan(int sensPin){

long result = 0;

pinMode(sensPin, OUTPUT);

digitalWrite(sensPin, HIGH);

delay(15);

pinMode(sensPin, INPUT);

digitalWrite(sensPin, LOW);

while(digitalRead(sensPin)){

result++;}

return result;

}