// THE AWESOME OBSTACLE AVOIDING CAR
//By Dominick Lee for Purdue Mechatronics Club

int ENA = 6; //PWM pin for Left Motor
int IN1 = 7; //Left motor direction
int IN2 = 8; //Left motor direction
int IN3 = 9; //Right motor direction
int IN4 = 10; //Right motor direction
int ENB = 11; //PWM pin for Right Motor
int SENSOR = 12; //Input pin for Arduino

//Function to control motors
//mtr – 0 = left, 1 = right
//dir – 0 = forward, 1 = backward
//spd – Motor speed from 0 to 255
void setMotor(int mtr, int dir, int spd)
{
if (mtr == 0) //Left motor
{
if (dir == 0)
{
//Move forward
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
}
else
{
//Move backward
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
}
//Set speed
analogWrite(ENA, constrain(spd, 0, 255));
}
else if (mtr == 1) //Right motor
{
if (dir == 0)
{
//Move forward
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
else
{
//Move backward
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
//Set speed
analogWrite(ENB, constrain(spd, 0, 255));
}
}

void setup()
{
//Set all pins to output
pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(ENB, OUTPUT);

//Set sensor input pin
pinMode(SENSOR, INPUT); //If 0, light is reflected – obstacle
}

void loop()
{
int state = digitalRead(SENSOR);

while (state == 0) //obstacle detected, loop
{
state = digitalRead(SENSOR); //obstacle still detected, loop
while (state == 0)
{
state = digitalRead(SENSOR); //obstacle still detected, loop
setMotor(0, 0, 0); //Set left motor, forward, 0 speed
setMotor(1, 0, 0); //Set right motor, forward, 0 speed
}
delay(800);
}

if (state)
{
//Move forward
setMotor(0, 0, 200); //Set left motor, forward, 200 speed
setMotor(1, 0, 200); //Set right motor, forward, 200 speed
delay(10);
}

}

dominickleeArduino Robotic Obstacle Avoidance