This subsystem consisted of a linear actuator for the brake pedal, 5 ultrasonic sensors mounted on the from of the cart, and a microswitch to make sure the brake pedal doesn't go too far. If the ultrasonic sensors sense an object within 10 feet they will tell the Arduino to apply the brake and release the gas until the speed reaches 0mph. Then, once the object is moved, the go-kart will continue on its path.

This shows how the actuator is mounted




Microswitch for brake pedal



5 Sensors mounted on the front of the go-kart.

5 ultrasonics code... This is the final code for the go-kart with everything included. (highlighted in red is the ultrasonics code):
#define trigPin1 23
#define echoPin1 22
#define trigPin2 25
#define echoPin2 24
#define trigPin3 27
#define echoPin3 26
#define trigPin4 29
#define echoPin4 28
#define trigPin5 31
#define echoPin5 30
#define OB_detected 1
#define Kart_move 2
#define Kart_move2 3
unsigned long duration1;
int distance1;
unsigned long duration2;
int distance2;
unsigned long duration3;
int distance3;
unsigned long duration4;
int distance4;
unsigned long duration5;
int distance5;
float sum = 0;
float count=0;
float mph;
int oldtime=0;
int deltaT = 0;
float errorband = 0.5;
float targetspeed =5.0;
float Last_Time_Check;
float Biggest_Allowed_Interval = 620;
int state=0;
float error;
float error1 = 0;
float PWM;
int K = 150;
int Ki = 100;
float desired;
float Kp = 10;
float error_v = 0;
float count_600 = 0;
float offset = 0;
float error_v1 = 0;
float Kd = 1;
float sum2 = 0;
void isr()//interrupt service routine, used to do something without interrupting the main loop. Should be very simple instructions inside this function as to not hold up the main loop.
{
count++; //increasing revolution number
Last_Time_Check = millis(); //records time stamp for last pulse
}
void setup() {
Serial.begin(9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(trigPin3, OUTPUT);
pinMode(echoPin3, INPUT);
pinMode(trigPin4, OUTPUT);
pinMode(echoPin4, INPUT);
pinMode(trigPin5, OUTPUT);
pinMode(echoPin5, INPUT);
pinMode (2,INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(2),isr,RISING); //attaching the interrupt, used FALLING because my sensor is normally high and pulled low when activated
oldtime = millis();
}
void loop() {
if (count >= 10){
deltaT = millis() - oldtime;
mph = (3560.76)/(deltaT); //This is the calculation to get mph.
//Serial.println(mph);
oldtime = millis();//finds the time
count=0;
}
else if((millis()-Last_Time_Check) > Biggest_Allowed_Interval)
{ mph = 0;}
measure_distances();
if(((distance1<=250 && distance2<=250)||
(distance2<=250 && distance3<=250)||
(distance3<=250 && distance4<=250)||
(distance4<=250 && distance5<=250))&&
mph>0){
state = OB_detected;
}
if(distance1>250 && distance2>250 &&
distance3>250 && distance4>250 && distance5>250){
state = Kart_move2;
}
if(state==OB_detected){
Serial.println("OB_detected");
applying_brakes();
release_gas(255); //quick release of gas pedal
delay(2000);
}
else if(state==Kart_move2){
Serial.println("Kart_move2");
control_system();
}
delay(100);
}
void applying_brakes(void){//Appying brake
analogWrite(5, 0);
analogWrite(6, 255);
}
void releasing_brakes(void){//Releasing brake
analogWrite(5, 255);
analogWrite(6, 0);
}
void release_gas(int PWM){//Releasing gas
analogWrite(7,0);
analogWrite(8,PWM);
}
void applying_gas(int PWM){
analogWrite(7,PWM);
analogWrite(8,0);
}
void pause_at_stop(void){// pause to give time to apply brakes and release gas. Then checks for object and if there isnt one, goes to state kart move.
delay(5000);
}
void control_system(void){
releasing_brakes();
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
Serial.print(voltage);
Serial.print(", ");
if( error_v >= 4 )
{
PWM = 255;
offset = voltage;
}
else if( error_v <= -4 )
{
PWM = -255;
offset = voltage;
}
else if ( abs(error_v) >= 1 )
{
sum2 = error_v + sum2;
desired = 0.01* error_v + 1*(error_v - error_v1) + 0.01*sum2 + offset;
error = desired - voltage;
sum = error + error1;
PWM = K*(error) + Ki*sum;
}
else
{
PWM = 0;
}
if(PWM>255){
PWM = 255;
}
if(PWM < -255){
PWM = -255;
}
//Serial.println(PWM);
if(PWM<0){
PWM = -PWM;
analogWrite(7,PWM);
analogWrite(8,0);
}
else{
analogWrite(7,0);
analogWrite(8,PWM);
}
error1 = error;
count_600++;
if(count_600 >2 ){
error_v = 5 - count;
error_v1 = error_v;
count_600 = 0;
count = 0;
}
Serial.println(error_v);
delay(100);
}
void measure_distances(void){
digitalWrite(trigPin1, LOW);
delayMicroseconds(5);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH);
distance1 = duration1*0.034/2;
////////////////////////////////////////////////////////
digitalWrite(trigPin2, LOW);
delayMicroseconds(5);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
duration2 = pulseIn(echoPin2, HIGH);
distance2 = duration2*0.034/2;
////////////////////////////////////////////////////////
digitalWrite(trigPin3, LOW);
delayMicroseconds(5);
digitalWrite(trigPin3, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin3, LOW);
duration3 = pulseIn(echoPin3, HIGH);
distance3 = duration3*0.034/2;
///////////////////////////////////////////////////////
digitalWrite(trigPin4, LOW);
delayMicroseconds(5);
digitalWrite(trigPin4, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin4, LOW);
duration4 = pulseIn(echoPin4, HIGH);
distance4 = duration4*0.034/2;
///////////////////////////////////////////////////////
digitalWrite(trigPin5, LOW);
delayMicroseconds(5);
digitalWrite(trigPin5, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin5, LOW);
duration5 = pulseIn(echoPin5, HIGH);
distance5 = duration5*0.034/2;
///////////////////////////////////////////////////////
Serial.print(mph);
Serial.print("mph,");
Serial.print(distance1);
Serial.print("cm,");
Serial.print(distance2);
Serial.print("cm,");
Serial.print(distance3);
Serial.print("cm,");
Serial.print(distance4);
Serial.print("cm,");
Serial.print(distance5);
Serial.println("cm");
return;
}