Page 1 of 1

Arduino controlled Brushless w/ ESC (lightning mcqueen)

Posted: Sat Oct 20, 2018 6:06 pm
by dave303
G'day - so I've started on all of this before discovering these forums, where I can see a wealth of knowledge.
My journey started with a Lightning McQueen from my sons grandparents who picked it up for a song at a garage sale.
Absolutely didn't work, diagnosed two motor failures and a broken steering servo. I found some great 15k motor and gearbox combos that lasted a few months with the existing controller, but the controller eventually melted and the cooling end of the motor colapsed on one side.. having a bit of RC experience, I decided it was time for a complete overhaul.

I've put in dual 120A ESC, sufficient battery to power it, 2x 3570 motors and replaced the controller with an arduino (considering all controls are now signal - all easy enough ) - and I'm just waiting on a pair of 5mm 10T pinion gears to arrive before I put it in. In the progress of doing all of this, it occurred to me that someone might benefit from my arduino code, or that someone might have made a better one already.. I'm super keen for any suggestions, I still need to wire up horn and headlights and all of that... and a bit smarter throttle control (if anyone knows a cheap way to make a linear throttle, the one I have in there is on/off ) and I still need to wire in RC control again.


Code: Select all
#include <Servo.h>
Servo firstESC;
int pos = 0; //Sets position variable
int current_throttle = 1500;
int current_reverse = 0;
int current_turbo = 0;
int last_throttlestate = LOW;
int throttlechangeMS = 0;
void setup() {
  firstESC.attach(5);
  firstESC.writeMicroseconds(1500);
  pinMode(7,INPUT_PULLUP);//throttle
  pinMode(6,INPUT_PULLUP);//reverse
  pinMode(8,INPUT_PULLUP);//turbo
    Serial.begin(9600);

 }

void loop() {
  // put your main code here, to run repeatedly:
  int throttlepin = digitalRead(7) ;
  int reversepin = digitalRead(6);
  int turbopin = digitalRead(8); 
  if ( turbopin == LOW ){
     current_turbo = 100;
  }else{
    current_turbo = 0;
  }
if (reversepin == LOW && current_reverse==0) {
        Serial.print("we're in reverse \n");
        current_reverse = 1;
}
if (reversepin == HIGH && current_reverse==1) {
        Serial.print("we're no longer in reveerse \n");
        current_reverse = 0;
}

  if (throttlepin == LOW ){
    if (last_throttlestate == 0 ){
      // we've just hit the gas
      Serial.print("we've just hit the gas \n");
      
      last_throttlestate = 1;
      throttlechangeMS = millis();
    }
    int msDelta = millis() - throttlechangeMS;
    //we can ramp up at 50 per 100ms, up to 1700MAX
    if (current_reverse == 0){
      if (current_throttle < 1700 + current_turbo){
        if (msDelta > 100){
            throttlechangeMS = millis();
            if (current_throttle == 1500){ current_throttle = 1580;}
            current_throttle = current_throttle + 5;
            Serial.print("speeding up to ");
            Serial.print(current_throttle);
            Serial.print("\n");
        }
      }
    }else{
      if (current_throttle > 1300 - current_turbo){
        if (msDelta > 100){
            throttlechangeMS = millis();
            if (current_throttle == 1500){ current_throttle = 1420;}
            current_throttle = current_throttle  - 5;
            Serial.print("reversing to ");
            Serial.print(current_throttle);
            Serial.print("\n");
        }
      }
    }
  }
  if (throttlepin  == HIGH){
    if (last_throttlestate == 1){
      // we've just taken our foot off.
      Serial.print("we've just taken our foot off the gas \n");
      last_throttlestate = 0;
      throttlechangeMS = millis();
    }
    int msDelta = millis() - throttlechangeMS;
    //we can ramp down at 50 per 100ms, up to 1500
    if (current_throttle > 1500){
      if (msDelta > 100){
          throttlechangeMS = millis();
          current_throttle = current_throttle - 20; // brake 4 times faster
          if (current_throttle - 1500 < 20 ) {current_throttle = 1500;}
          Serial.print("brakeing to ");
          Serial.print(current_throttle);
          Serial.print("\n");
      }
    }
    if (current_throttle < 1500){
          if (msDelta > 100){
          throttlechangeMS = millis();
          current_throttle = current_throttle + 20;
          if (1500 - current_throttle < 20 ) {current_throttle = 1500;}
          Serial.print("braking from reverse to ");
          Serial.print(current_throttle);
          Serial.print("\n");

      }
    }
    
  }
  
  firstESC.writeMicroseconds(current_throttle);
  delay(10);

  
}

Re: Arduino controlled Brushless w/ ESC (lightning mcqueen)

Posted: Sat Mar 30, 2019 8:12 pm
by RangeRover
Hi Dave303 how did you go with this? curious to know if you got the audrino setup how you wanted. im amazed with what you can do with the audrino setup...if you know what your doing!