// Rc car 2 inputs 1 output
// https://forum.arduino.cc/t/rc-car-2-inputs-1-output/1327657
/*Putting together electronics for kids Ride on
The Rc Radio Transmitter must overwrite the cars inputs for safety
The Rc Radio Reciver input must overwrite the cars inputs for safety
Arduino Mega 2560, three BTS7960 motor controllers (2 run the motors and 1 for steering(), HotRC radio reciver,
pwm foot pedal and a 3 position switch for forward and reverse selection.
Channel 3 to disable/enable the 2 BTS7960 motor controllers running the motors.
channel 5 and 6 to counter roatate the 2 motor controllers running the motors
channel 5 would counter roatate in one direction and channel 6 would counter roatate in the other direction.
channel 4 is the Governor for top speed setting setting 1 50% setting 2 70% setting 3 100%
*/
//RC Reciver CH2 Throttle settings
const int forwardMax = 1947; //RC Reciver ch2 Forward Throttle Max
const int forwardMin = 1470; //RC Reciver ch2 throttle Deadband start
const int neutral = 1453; //RC Reciver ch2 Center of neutral Deadband
const int reverseMin = 1440; //RC Reciver ch2 throttle Deadband end
const int reverseMax = 974; //RC Reciver ch2 Reverse Max
//Right Motor Controller BTS6960A connected to Front and Rear RIGHT Motors
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 R_ENA = 26; //Enable Pin Must Pulled High For Motors to Run
const int L_ENA = 27; //Enable Pin Must Pulled High For Motors to Run
//Left Motor Controller 2 BTS6960B connected to Front and Rear LEFT Motors
const int RPWMB = 7; //Arduino PWM output pin 7; connect to IBT-2 pin 1 (RPWM)
const int LPWMB = 8; //Arduino PWM output pin 8; connect to IBT-2 pin 2 (LPWM)
const int R_ENB = 28; //Enable Pin Must Pulled High For Motors to Run
const int L_ENB = 29; //Enable Pin Must Pulled High For Motors to Run
//Steering Controll
const int SteeringPin = 2; //RC Reciver ch1 Arduino Digital pin 2
const int SteeringLeft = 974; //RC Reciver ch1 Steer Left
const int SteeringCenter = 1453; //RC Reciver ch1
const int SteeringRight = 1947; //RC Reciver ch1 Steer right
//Steering Motor Controller BTS7960C Connected to Steering Shaft
const int RPWMC = 9; //Arduino PWM output pin 9; connect to IBT-2 pin 1 (RPWM)
const int LPWMC = 10; //Arduino PWM output pin 10; connect to IBT-2 pin 2 (LPWM)
//Roatating Controll
const int RotateLeftPin = 24; //RC Reciver ch6 Arduino Digital pin 6 when engaged Right Motor Controller Moves Forward/Left Motor Controller Moves in Reverse
const int RotateRightPin = 25; //RC Reciver ch5 Arduino Digital pin 5 when engaged Right Motor Controller Moves Reverse/Left Motor Controller Moves in Forward
const int RotateLeftMin = 973; //RC Reciver ch6 LOW
const int RotateLeftMax = 2000; //RC Reciver ch6 HIGH
const int RotateRightMin = 973; //RC Reciver ch5 LOW
const int RotateRightMax = 2000; //RC Reciver ch5 HIGH
//Remote & Governor Settings
const int remoteThrottlePin = 3; //RC Reciver ch2 Arduino Digital pin 3
const int remoteGovernorPin = 23; //RC Reciver ch4 Arduino Digital pin 23
const int framerate = 22000; //In Microseconds
const int remoteGovernorHigh = 1947; //Switch in Possition 3 Set Motor Controller Speed to %100
const int remoteGovernorMedium = 1453; //Switch in Possition 2 Set Motor Controller Speed to %75
const int remoteGovernorLow = 974; //Switch in Possition 1 Set Motor Controller Speed to %50
//Park Brake
const int parkeBrakePin = 22; //RC Reciver ch3 Arduino Digital pin 22
const int parkeBrakeON = 1947; //RC Reciver ch3 High, When High Disable BTS7960A and BTS9760B by Pull LOW R_EN pin 26 L_EN pin 27
const int parkeBrakeOFF = 974; //RC Reciver ch3 Low
//Car, Pedal & Forward/Neutral/Reverse switch settings
const int pedalMin = 200; //or176 //my min pedal raw input is about 176 so I set it slightly higer the reduce false pedal input
const int pedalMax = 870; //my max pedal input is about 870
const int pedalGovernor = 100; //percentage
const int pedalPin = A0; //Pedal Sensor pin Arduino Analog pin 0
#define simThrottlePin A1
#define simGovernorPin A2
const int forwardPin = 30; //Gearbox in Forward Possition Arduino Digital pin 30 "this is just a 3 way switch with one wire connected to ground one wire connected to pin 30 and the other to pin 31"
const int reversePin = 31; //Gearbox in Forward Possition Arduino Digital pin 31 "this is just a 3 way switch with one wire connected to ground one wire connected to pin 31 and the other to pin 30"
int finalpedalInput = 0;
int adjpedalInput = 0;
void setup() {
// Set up serial monitor
Serial.begin(115200);
// Set all pins as INPUTS
pinMode(parkeBrakePin, INPUT);
pinMode(remoteGovernorPin, INPUT);
pinMode(remoteThrottlePin, INPUT);
pinMode(SteeringPin, INPUT);
pinMode(RotateLeftPin, INPUT);
pinMode(RotateRightPin, INPUT);
//Set Car Pins as INPUT
pinMode(forwardPin, INPUT_PULLUP);
pinMode(reversePin, INPUT_PULLUP);
//Set Motor Pins as OUTPUT
pinMode(R_ENA, OUTPUT);
pinMode(L_ENA, OUTPUT);
pinMode(R_ENB, OUTPUT);
pinMode(L_ENB, 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, 30000); // TimeOut after 30ms
int remoteThrottleInput = analogRead(simThrottlePin);
remoteThrottleInput = map(remoteThrottleInput, 0, 1023, 500, 2500); // map remoteThrottleInput to 1-100
if (remoteThrottleInput != 0 &&
(remoteThrottleInput <= reverseMin || remoteThrottleInput >= forwardMin))
{
pwmOutput = map(remoteThrottleInput, 500, 2500, 0, 255);
}
// Is car disabled
else {
// int variableGovernor = pulseIn(remoteGovernorPin, HIGH, 30000); // TimeOut after 30ms
// int variablePedalGovernor = map(variableGovernor, remoteGovernorLow, remoteGovernorHigh, 0, 100); // map variableGovernor to 1-100
int variableGovernor = analogRead(simGovernorPin);
int variablePedalGovernor = map(variableGovernor, 0, 1023, 0, 100); // map variableGovernor to 1-100
variablePedalGovernor = constrain(variablePedalGovernor, 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 * variablePedalGovernor / 100; // Apply Pedal Governor 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);
pwmOutput = map(finalpedalInput, 0, 100, 0, 255);
}
// Then are we going backward?
else if (carReverse == 0) {
// pwmOutput = map(finalpedalInput, 0, 100, reverseMin, reverseMax);
pwmOutput = map(finalpedalInput, 0, 100, 0, 255);
}
}
// Then we should be in neutral
else {
// pwmOutput = neutral;
pwmOutput = 128;
}
}
Serial.println(pwmOutput);
analogWrite(LPWMA, pwmOutput);
analogWrite(RPWMA, pwmOutput);
analogWrite(LPWMB, pwmOutput);
analogWrite(RPWMB, pwmOutput);
analogWrite(LPWMC, pwmOutput);
analogWrite(RPWMC, pwmOutput);
/*
//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);
*/
}
Reverse Forward
A
B
C
Sim Throttle
Governor
Pedal