Final Race Code w/ Ramped Acceleration and LED UI

The section Traction and Traction Control Coding discussed the need to increment the throttle from start to maximum torque. Remember this:

This code and the LED UI code are included in the program below. In the IDE, alter 'Top Speed' and 'Run Time Delay' for testing and to dial in your final race code.

This program is set to accelerate the car for approximately 0.25 seconds, then stop allowing the car to coast. FOR SAFTEY, the Top Speed is set to 1400. Set the for() loop end value to 2000 for full throttle. To increase the time under full throttle, change the delay at the end of the for() loop.


/*                             edrag_race-1
*                       Race w/ Accelerated Start     
*                                 Race 1
* 2 Motors, 1 signal wire, touch or magnetic start switch 
* with LED output. redLED pin 11 and greenLED pin 12
*  --------------------------------------------------------------------Dr.D. 101722 */ 
#include <Servo.h>
Servo BLDC1;
int BLDC1_pin = 5;        // 2-motors controlled w/ both ESC signal wires connected to pin 5 
int throttle=0;
int greenlight_pin=9;     // Switch Signal pin
byte greenlight=LOW;
int redLED=11;            // LED Output pins 11 & 12
int greenLED=12;
void setup() {
pinMode(greenlight_pin, INPUT_PULLUP);
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);  
Serial.begin(9600);
BLDC1.attach(BLDC1_pin,1000,2000);
BLDC1.writeMicroseconds(1000); // send "stop" signal to ESC.
delay(500); // delay to allow the ESC to recognize the stopped signal
}
void loop() {
greenlight=digitalRead(greenlight_pin);      // start reset
digitalWrite(redLED, HIGH); digitalWrite(greenLED, LOW);  
 while (greenlight == HIGH) {                // Loop until  greenlight = LOW
 greenlight=digitalRead(greenlight_pin);
 Serial.println("Yellow");
 }
digitalWrite(redLED, LOW);  digitalWrite(greenLED, HIGH);  
Serial.println("GREEN Go >>>");
 /*  -------------------------------------------------------- Ramped Acceleration */  
    for (throttle = 1040; throttle < 1400; throttle+=25) {     
    BLDC1.writeMicroseconds(throttle); // Send signal to ESC.
    Serial.print("throttle= ");
     Serial.println(throttle);
     delay(10);             // note - 25 increments * .01s =.25s
     }
/* ------------------------------------------------------------- */
  delay(0);                                           // Run Time Delay -- run full speed then STOP
  throttle=1000;
  BLDC1.writeMicroseconds(throttle);                  // Send STOP signal to ESC.
  Serial.println("throttle STOP");
  delay(1000);
}