Jump to content

Transforming a signal from a servo motor to ESD arduino

Symbolls

So i have a esd motor thati wana put on an RC car that was working with gas and i wanted to trasle the signal from a servo that acted like a gas pedal for the old motor into an esd signal si i can make it work with a bldc motor here is the code i have made so far

 

the signal coming from the rc recever is for servos so that way can only get input like that

 

my signal is from -255 to 255 i need to make a pwm signal betwin 1 ms(stop) to 2 ms(full throtle) and i dont now how to translate the signla

 

At first i was making this whit a DC motor whith a h bridged

 

thx for your time btw 🙂

#include <Servo.h>
#include <ServoInput.h>
#define enA 3
#define in1 4
#define in2 5
int rotDirection = 0;
int pressed = false;
/* Signal pins for ServoInput MUST be interrupt-capable pins!
 *     Uno, Nano, Mini (328P): 2, 3
 *     Micro, Leonardo (32U4): 0, 1, 2, 3, 7
 *             Mega, Mega2560: 2, 3, 18, 19, 20, 21
 * Reference: https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/
 */

// Steering Setup
//const int SteeringSignalPin = 2;  // MUST be interrupt-capable!
//const int SteeringPulseMin = 1000;  // microseconds (us)
//const int SteeringPulseMax = 2000;  // Ideal values for your servo can be found with the "Calibration" example

//ServoInputPin<SteeringSignalPin> steering(SteeringPulseMin, SteeringPulseMax);
Servo ESC;     // create servo object to control the ESC
int potValue;  // value from the analog pin

// Throttle Setup
const int ThrottleSignalPin = 2;  // MUST be interrupt-capable!
const int ThrottlePulseMin = 1023;  // microseconds (us)
const int ThrottlePulseMax = 1898;  // Ideal values for your servo can be found with the "Calibration" example

ServoInputPin<2> throttle(1023, 1898);

void setup() {
  Serial.begin(115200);

    // Attach the ESC on pin 9
  ESC.attach(9,1000,2000); // (pin, min pulse width, max pulse width in microseconds)

  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);

  while (!ServoInput.available()) {  // wait for all signals to be ready
    Serial.println("Waiting for servo signals...");
    delay(500);
  }
}

void loop() {
  potValue = 2;   // reads the value of the potentiometer (value between 0 and 1023)
  potValue = map(potValue, 0, 1023, 0, 180);   // scale it to use it with the servo library (value between 0 and 180)
  ESC.write(potValue);    // Send the signal to the ESC
  Serial.print(potValue);
 
  Serial.print("RC - ");

//  float steeringAngle = 90.0 - steering.getAngle();  // returns 0 - 180, subtracting from 90 to center at "0" and invert for "normal" steering
//  Serial.print("Steering: ");
//  Serial.print(steeringAngle);
//  Serial.print("deg");

//  Serial.print(" | ");  // separator

  int throttlePercent = throttle.map(-255, 255);  // remap to a percentage both forward and reverse
  Serial.print("Throttle: ");
  Serial.print(throttlePercent);
  Serial.print("% ");
 
    analogWrite(enA, throttlePercent);
//  digitalWrite(in1, HIGH);
//  digitalWrite(in2, LOW);

  //  if (throttlePercent <= 0){
 //    analogWrite(enA, throttlePercent *-1);
//
//    }
    
    if (throttlePercent >= 0) {
    Serial.print("(Forward)");
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
  }
  else{
    //digitalWrite(enA, LOW);
  }
    if (throttlePercent <= 0) {
    Serial.print("(Reverse)");
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
  }
  else{
    //digitalWrite(enA, LOW);
  }

 
  if (throttlePercent >= 0) {
    Serial.print("(Forward)");
    
  }
  else {
    Serial.print("(Reverse)");
  }

  Serial.println();
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×