This is what your final program should look like:
#include <SmartInventor.h>
int speedPercent = 50;
int backTime = 0;
int turnTime = 0;
#define trigPin 29
#define echoPin 30
void setup()
{
//for the trig pin and echo pin
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//<-LEFT □ □ □ □ □ □ □ □ RIGHT->
// 11,12,13,14,15,16,17,18
//BOTTOM SENSOR USE
pinMode(11, INPUT);
pinMode(12,INPUT);
pinMode(13,INPUT);
pinMode(14,INPUT);
pinMode(15,INPUT);
pinMode(16,INPUT);
pinMode(17,INPUT);
pinMode(18, INPUT);
//This initiates the use of the motor
SmartInventor.DCMotorUse();
//Start of the motors to move forwards
SmartInventor.DCMove(forward, speedPercent);
}
void loop()
{
//grab the distance and save it as an integer
int currentDistance = getPingDistance();
//if the distance is less than 50cm
if(currentDistance<50)
{
//make the buzzer buzz
SmartInventor.Buzz(2000-currentDistance*25, 18);
}
backTime++;
turnTime++;
if (backTime >= 200) backTime = 100;
if (turnTime >= 500) turnTime = 100;
//If the IR sensors on the left side detect anything
if (digitalRead(11) == LOW||
digitalRead(12) == LOW||
digitalRead(13) == LOW)
{
//move backwards and buzz
SmartInventor.DCMove(backward, speedPercent);
SmartInventor.Buzz(1160, 12);
delay(100 + backTime);
//move to the right
SmartInventor.DCMove(right, speedPercent);
delay(100 + turnTime);
//then move forward again
SmartInventor.DCMove(forward, speedPercent);
}
else if (digitalRead(17) == LOW||
digitalRead(16) == LOW||
digitalRead(18) == LOW)
{
//move backwards for some time
SmartInventor.DCMove(backward, speedPercent);
SmartInventor.Buzz(1160, 12);
delay(100 + BackTime);
//move to the left
SmartInventor.DCMove(left, speedPercent);
delay(100 + TurnTime);
//then go forward
SmartInventor.DCMove(forward, speedPercent);
SmartInventor.Buzz(1900, 12);
}
//If all IR sensors do not detect anyline on the floor
//and the distance of whatever object is less than 6cm
//drive forward
if(
digitalRead(11) == HIGH &&
digitalRead(12) == HIGH &&
digitalRead(13) == HIGH &&
digitalRead(16) == HIGH &&
digitalRead(17) == HIGH &&
digitalRead(18) == HIGH &&
currentDistance<6
)
{
SmartInventor.DCMove(forward, 100);
//drive at full speedPercent
}
}
int getPingDistance()
{
long duration, distance;//create these two numbers
digitalWrite(trigPin, LOW);
//get the trigger pin set to low
delayMicroseconds(2); //
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); //
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
return distance = (duration/2) / 29.1;
//so the speedPercent of sound at sea level is 340.29 m / s
}