Hi all
First of all I know a little about arduino but i am pretty much a noob. I am trying to learn, but there is something that I want to do and I can't figure out how. I built an rc tank that runs from two half h bride drivers and an arduino nano. The nano is coded to take the input from a rc receiver and output it to the h bride driver. The issue is my little tank is so powerful that I want to add some ramping to the motors instead of them just going full power as soon as I give the full power signal and I don't want them to slow down so fast either. I am having trouble figuring out how to do this so I thought I would ask for help here. Here is the code I am working with.
I did not code most of this. I found it online and me and a friend did a couple changes.
//read PPM signals from 2 channels of an RC reciever and convert the values to PWM in either direction.
// digital pins 5 & 9 control motor1, digital pins 6 & 10 control motor2. DP 12 and 13 are neutral indicator lights. DP 2 and 3 are inputs from the R/C receiver. All analog pins are open. When motor pin is HIGH, bridge is open. All mosfets are pulled up/down to stay closed unless told to open.
int motor1_a = 5;
int motor1_b = 9;
int motor2_a = 6;
int motor2_b = 10;
//Neutral indicator LED's (FWD and REV LED's are on the motor driver board.
int ledPin1 = 12;
int ledPin2 = 13;
int ppm1 = 2; // connect the desired channel (PPM signal) from your RC receiver to digital pin 2 on Arduino.
int ppm2 = 3;
unsigned long rcval1 = 1500;
unsigned long rcval2 = 1500;
int deadband_high = 265; // set the desired deadband ceiling (ie. if adj_val is above this, you go forward)
int deadband_low = 245; // set deadband floor (ie. below this, go backward)
int pwm_ceiling = 256; // adjusts speed of PWM signal at turn-on
int pwm_floor = 254;
void setup() {
Serial.begin(9600);
// set all the other pins you're using as outputs:
//motor1 pins
pinMode(motor1_a, OUTPUT);
pinMode(motor1_b, OUTPUT);
pinMode(motor2_a, OUTPUT);
pinMode(motor2_b, OUTPUT);
//led's
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
//PPM inputs from RC receiver
pinMode(ppm1, INPUT); //Pin 2 as input
pinMode(ppm2, INPUT); //Pin 3 as input
}
void driveMotor(int thisMotor, int driveVal)
{
//Convert RC signal from 1000 range to 512 range (256 for each direction).
driveVal = map(constrain(driveVal, 1000, 2000), 1000, 2000, 0, 511);
// Default to motor 2
int motor_a = motor2_a;
int motor_b = motor2_b;
int ledPin = ledPin2;
// if motor is 1 change pins
if(thisMotor == 1) {
motor_a = motor1_a;
motor_b = motor1_b;
ledPin = ledPin1;
}
if (driveVal > deadband_high) {
//Forward
digitalWrite(motor_b, LOW);
analogWrite(motor_a, driveVal - pwm_ceiling);
digitalWrite(ledPin, LOW);
}
else if (driveVal < deadband_low) {
//REVERSE
digitalWrite(motor_a, LOW);
analogWrite(motor_b, pwm_floor - driveVal);
digitalWrite(ledPin, LOW);
}
else {
//STOP
digitalWrite(motor_a, LOW);
digitalWrite(motor_b, LOW);
digitalWrite(ledPin, HIGH); // turn on neutral light, turn motor pins off
}
}
void loop() {
rcval1 = pulseIn(ppm1, HIGH); // Listen for pulse on PPM1
rcval2 = pulseIn(ppm2, HIGH); // Listen for pulse on PPM2
driveMotor(1,rcval1);
driveMotor(2,rcval2);
/*
//print values
Serial.print("channel 1: ");
Serial.print(adj_val1);
Serial.print(" ");
Serial.print("rc1_val raw: ");
Serial.print(rc1_val);
Serial.print(" ");
Serial.print("channel 2: ");
Serial.print(adj_val2);
Serial.print(" ");
Serial.print("rc2_val raw: ");
Serial.print(rc2_val);
Serial.println(" ");
*/
}