//ESC & Throttle settings
const int forwardMax = 1947;
const int forwardMin = 1470; //throttle Deadband start
const int neutral = 1453; //Center of neutral Deadband
const int reverseMin = 1440; //throttle Deadband end
const int reverseMax = 974;
const int RPWMA = 5; // Arduino PWM output pin 5; connect to IBT-2 pin 1 (RPWM)
const int LPWMA = 6; // Arduino PWM output pin 6; connect to IBT-2 pin 2 (LPWM)
const int RPWMB = 7; // Arduino PWM output pin 5; connect to IBT-2 pin 1 (RPWM)
const int LPWMB = 8; // Arduino PWM output pin 6; connect to IBT-2 pin 2 (LPWM)
//Steering Controll
const int SteeinglPin = 2;
const int SteeringLeft = 974;
const int SrteeringCenter= 1453;
const int SteeringRight = 1947;
//Roatating Controll
const int RotateLeftPin = 24;
const int RotateRightPin = 25;
const int RotateLeftMin = 973;
const int RotateLeftMax = 2000;
const int RotateRightMin = 973;
const int RotateRightMax = 2000;
const int RPWMC = 9; // Arduino PWM output pin 5; connect to IBT-2 pin 1 (RPWM)
const int LPWMC = 10; // Arduino PWM output pin 6; connect to IBT-2 pin 2 (LPWM)
//Remote & Govenor Settings
const int remoteThrottlePin = 3;
const int remoteGovenorPin = 23;
const int framerate = 22000; //in microseconds
const int remoteGovenorHigh = 1947;
const int remoteGovenorMedium = 1453;
const int remoteGovenorLow = 974;
//Park Brake
const int parkeBrakePin = 22;
const int parkeBrakeON = 1947;
const int parkeBrakeOFF = 974;
//Car, Pedal & Forward/Neutral/Reverse switch settings
const int pedalMin = 200; //or176 //my min pedal raw input is about 200 so I set it slightly higer the reduce false pedal input
const int pedalMax = 870; //my max pedal input is about 378
const int pedalGovenor = 100; //percentage
const int pedalPin = A0;
const int forwardPin = 30;
const int reversePin = 31;
int finalpedalInput = 0;
int adjpedalInput = 0;
void setup() {
// Set up serial monitor
Serial.begin(115200);
// Set all pins as INPUTS
pinMode(parkeBrakePin, INPUT);
pinMode(remoteGovenorPin, INPUT);
pinMode(remoteThrottlePin, INPUT);
pinMode(SteeinglPin, INPUT);
pinMode(RotateLeftPin, INPUT);
pinMode(RotateRightPin, INPUT);
//Set Car Pins as INPUT
pinMode(forwardPin, INPUT);
digitalWrite(forwardPin, HIGH);
pinMode(reversePin, INPUT);
digitalWrite(reversePin, HIGH);
//Set Motor Pins as OUTPUT
pinMode(RPWMA, OUTPUT);
pinMode(LPWMA, OUTPUT);
pinMode(RPWMB, OUTPUT);
pinMode(LPWMB, OUTPUT);
pinMode(RPWMC, OUTPUT);
pinMode(LPWMC, OUTPUT);
}
void loop() {
int pwmOutput = neutral;
// Use the Remote input first
int remoteThrottleInput = pulseIn(remoteThrottlePin, HIGH);
if (remoteThrottleInput != 0 && remoteThrottleInput <= reverseMin|| remoteThrottleInput >= forwardMin) {
pwmOutput = remoteThrottleInput;
}
// Is car disabled
else {
int variableGovenor = pulseIn(remoteGovenorPin, HIGH);
int variablePedalGovenor = map (variableGovenor, remoteGovenorLow, remoteGovenorHigh, 0, 100); // map variableGovenor to 1-100
variablePedalGovenor = constrain(variablePedalGovenor, 0, 100);
int pedalInput = analogRead(pedalPin); // = 200-870
// Are we hitting the pedal?
if (pedalInput > pedalMin){
pedalInput = constrain (pedalInput, pedalMin, pedalMax);
adjpedalInput = map(pedalInput, pedalMin, pedalMax, 0, 100); // map pedal input to 1-100
finalpedalInput = adjpedalInput * variablePedalGovenor / 100; // Apply Pedal Govenor to adjpedalInput
finalpedalInput = constrain (finalpedalInput, 0, 100);
// What direction are we going?
bool carForward = digitalRead(forwardPin);
bool carReverse = digitalRead(reversePin);
// Are we going forward?
if (carForward == 0) {
pwmOutput = map(finalpedalInput, 0, 100, forwardMin, forwardMax);
}
// Then are we going backward?
else if (carReverse == 0) {
pwmOutput = map(finalpedalInput, 0, 100, reverseMin, reverseMax);
}
}
// Then we should be in neutral
else {
pwmOutput = neutral;
}
}
//Steering out as pwm
digitalWrite(LPWMC, HIGH);
digitalWrite(RPWMC, HIGH);
delayMicroseconds(pwmOutput);
digitalWrite(LPWMC, LOW);
digitalWrite(RPWMC, LOW);
delayMicroseconds(framerate - pwmOutput);
// Throttle out as pwm
digitalWrite(LPWMA, HIGH);
digitalWrite(RPWMA, HIGH);
digitalWrite(LPWMB, HIGH);
digitalWrite(RPWMB, HIGH);
delayMicroseconds(pwmOutput);
digitalWrite(LPWMA, LOW);
digitalWrite(RPWMA, LOW);
digitalWrite(LPWMB, LOW);
digitalWrite(RPWMB, LOW);
delayMicroseconds(framerate - pwmOutput);
}
As you can see i dont know what im doing, sorry if this is a repetitive post but i would really appreciate some help.
Its a rc ride on with overright function spin and a little more. Only acceleration and steering are pwm the rest is not, im using 3 bts7960 2 for the wheels and 1 for the steering.