Electronic Differential and Brake System Plausibility Device

Hello Everyone

I'm working on an Electric Vehicle Project, and I'm designing an Arduino circuit to make the 2 functions displayed in the subject. I'm made the code, but whenever I test the throttle or brake in the Proteus (using potentiometers and leds), the function of the BSPD is working, but when the brakes is off and I try to test the throttle, the led doesn't work until the potentiometer reaches 100% only, which is wrong. The led should turn on whenever I move the potentiomter from 0% .

What is wrong with the code exactly? Thank you in advanced.

(Also, a dead man's switch protection circuit is also included in the code but it's working just fine)

int Tr = A1; //Throttle Signal
int Br = A0; //Brakes Signal
int St = A3; //Stearing Singal
int SA;
int dw;
#define pi 3.1415926535897932384626433832795
int a1 = 5; //Throttle of Right Motor
int a2 = 6; //Throttle of Left Motor
#define b1 10
#define b2 11
const int Buzz = 9;
const int SW2 = 8; // BJT for Battery
const int DMS = 7; // Dead Man's Switch

void setup() {
pinMode(Tr, INPUT);
pinMode(Br, INPUT);
pinMode(St, INPUT);
pinMode(a1, OUTPUT);
pinMode(a2, OUTPUT);
pinMode(b1, OUTPUT);
pinMode(b2, OUTPUT);
pinMode (Buzz, OUTPUT);
pinMode (SW2, OUTPUT);
pinMode (DMS, INPUT);
}

void loop() {
int x = analogRead(Tr);
int y = analogRead(St);
int z = analogRead(Br);
x = map(x, 0, 1023, 0, 255);
z = map(z, 0, 1023, 0, 255);
SA = y * pi / 1023; // stearing agnle in radian
dw = 50 * x * tan(SA) / 100; //50 and 100 are width and length of vehicle (Dw and Lw)
int dead_sw_status = digitalRead(DMS);

if (dead_sw_status == 1) {
digitalWrite(Buzz, LOW);
digitalWrite(SW2, HIGH);

if (x > 0 && z> 0) { //Two pedals operating together
analogWrite(a1, 0); //Trottle will disconnect
analogWrite(a2, 0);
analogWrite(b1, z);
analogWrite(b2, z);
}
else if ( x>0 ){
analogWrite(a1, x); //Trottle will still be connected
analogWrite(a2, x);
analogWrite(b1, z);
analogWrite(b2, z);
}
} else {
delay (5000);
digitalWrite(Buzz, HIGH);
delay(5000);
digitalWrite(SW2, LOW);
}
}

welcome to the forums. Please read the sticky post at the top to learn how to properly post your code using code tags. It helps others help you.

As for your code, get in the habit of using better variable names. Things like b1 don't really tell anybody what it is or what it should be doing.

As for your logic, the first if() statement demands that both throttle and brake are >0 which seems odd since the action associated with that is to turn off the throttle and apply the brake. The else if() clause only tests throttle and that can only succeed of brake was 0. If you map it out

throttle brake desire
0 0 nothing
0 >0 apply brake

0 0 apply throttle
0 >0 not sure what you want to do here

This code should get you going...

const int throttlePin = A1; //Throttle Signal
const int breakPin = A0; //Brakes Signal
const int stearingPin = A3; //Stearing Singal
const int rightMotorPin = 5; //Throttle of Right Motor
const int leftMotorPin = 6; //Throttle of Left Motor
const int rightBrakePin = 10;
const int lefttBrakePin = 11;

const int deadmanSwitchPin = 7; // Dead Man's Switch
const int batterySwitchPin = 8; // BJT for Battery
const int buzzerPin = 9;

const float pi = 3.1415926535897932384626433832795;   // you will never get this much precision


void setup() {
  pinMode(throttlePin, INPUT);
  pinMode(breakPin, INPUT);
  pinMode(stearingPin, INPUT);
  pinMode(rightMotorPin, OUTPUT);
  pinMode(leftMotorPin, OUTPUT);
  pinMode(rightBrakePin, OUTPUT);
  pinMode(lefttBrakePin, OUTPUT);
  pinMode (buzzerPin, OUTPUT);
  pinMode (batterySwitchPin, OUTPUT);
  pinMode (deadmanSwitchPin, INPUT);
}

void loop() {
  int throttle = analogRead(throttlePin);
  int stearing = analogRead(stearingPin);
  int brake = analogRead(breakPin);
  throttle = map(throttle, 0, 1023, 0, 255);
  brake = map(brake, 0, 1023, 0, 255);

  float stearingAngle = stearing * pi / 1023; // stearing agnle in radian
  float dw = 50.0 * throttle * tan(stearingAngle) / 100; //50 and 100 are width and length of vehicle (Dw and Lw)
  int deadmanSwitch = digitalRead(deadmanSwitchPin);

  if (deadmanSwitch == HIGH) {
    // good to go
    digitalWrite(buzzerPin, LOW);
    digitalWrite(batterySwitchPin, HIGH);

    if (throttle > 0 and brake > 0) {
      // both throttle and brake
      // do something?
    }
    else if ( brake > 0 ) {
      analogWrite(rightMotorPin, 0); //Trottle will disconnect
      analogWrite(leftMotorPin, 0);
      analogWrite(rightBrakePin, brake);
      analogWrite(lefttBrakePin, brake);
    }
    else if ( throttle > 0 ) {
      analogWrite(rightMotorPin, throttle);
      analogWrite(leftMotorPin, throttle);
      analogWrite(rightBrakePin, 0);
      analogWrite(lefttBrakePin, 0);
    }
    else {
      // no throttle and no brake
      // do something?
    }
  }
  else {
    // deadman switch, power down
    digitalWrite(batterySwitchPin, LOW);
    digitalWrite(buzzerPin, HIGH);
    delay(5000);
  }
}

Thank you very much Sir, I didn't really know the right way to post a code. Also thank you for your advice and for editing the code, I'll test it and hopefully it will work, thanks to you :slight_smile:

Hmmmmm, interesting concept. Using hobbyist-grade hardware running software written by an inexperienced, amateur programmer to control a vehicle's brakes. What could go wrong?