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);
}
}