Programing pinewood derby car

Building an outlaw EDF pinewood derby car has gotten my son and I into programming. I am new to programming, learning from online videos and forums. So for my car I am using a code already written for the same application. Original programing was for an Arduino Trinket and I have a Nano Every. I've made adjustments accordingly.

The program seems to be running but the 20amp BEC ESC that controls the Ducted fan is not arming and I'm not quite sure how to program it. I've been looking at the manual but I'm not sure what to do.

I appreciate any help. I'll answer any questions you have as well. I'll share my program, the ESC manual and the original program for reference. Thanks!

ESC Manual

Original Code

My Code

#include <Servo.h>


volatile uint8_t counter = 0;
unsigned long stagedTime = 0;
unsigned long racingTime = 0;

Servo m1;                          
int SERVOPIN=10;                   // Servo control output pin  
int IRBEAMPIN=12;                  // IR beam input pin

int OFFSPEED=0;                          // Lowest ESC speed (range 0-180)
int STAGEDSPEED=70;                      // ESC speed when staged before race start
int RACINGSPEED=180;                     // Maximum ESC speed when race start detected 

int MINIMUMSTAGEDDURATION= 2000;          // Minimum duration car must be stage to enter race mode in milliseconds
int MAXIMUMRACEDURATION= 1500;            // Maximum duration of racing speed in milliseconds

enum raceState {
  OFF,
  STAGED,
  RACING,
  DONE                                      // Do not allow motor to turn back on after race until power reset
};
raceState currentState = OFF;

void setup() {
  
  // ESC 
  pinMode(SERVOPIN, OUTPUT);                // Set up ESC control pin as an output
  m1.attach(SERVOPIN);                     // Attach the pin to the software servo library
  
  // IR BEAM
  pinMode(IRBEAMPIN, INPUT);                // Set up IR beam pin as input
  Serial.begin(9600);

}

void loop() {
  
  // Use the IR beam sensor to determine the current state of the race
  if (digitalRead(IRBEAMPIN) == HIGH)       // IR beam NOT broken
  {
    if (currentState == STAGED) {           // If car is currently staged
      // If car has been staged for longer than the staged duration
      if ((millis() - stagedTime) > MINIMUMSTAGEDDURATION) {
        racingTime = millis();              // Start race duration timer
        currentState = RACING;              // ENTER RACE MODE
      }
      else
        currentState = OFF;                 // Off mode
    }
  }
  else {                                    // IR beam BROKEN
     (digitalRead(IRBEAMPIN) == LOW);
    {
    if (currentState == OFF) {
      stagedTime = millis() ;                // Start "Staged" timer
      currentState = STAGED;                // CAR IS STAGED
    }
    } 
  }

  // Turn off the motor after a specified race duration
  if (currentState == RACING) {             // If the car is currently racing
    // Measure the race duration and compare it to maximum race duration
    if ((millis() - racingTime) > MAXIMUMRACEDURATION) {
      currentState = DONE;                  // Turn ESC off
    }
  }
  // Set the ESC speed depending on the current state
  if (currentState == STAGED) {
    m1.write(STAGEDSPEED);
  }
  else if (currentState == RACING)
    m1.write(RACINGSPEED);
  else {
    m1.write(OFFSPEED);
  }
  delay(1);
}

From the ESC manual

Using the ESC

  1. Turn on your radio and keep the throttle stick to the lowest position
  2. Connect the battery pack to the ESC.
  3. Motor emits two sets of audible tones in succession means the ESC is armed and ready to
    use

Is the output to the ESC at its lowest position when the code starts. If not it will not arm

I believe so. Thats where I'm a little confused about the program. It's supposed to be at off speed which is angle (0) unless the IR sensor tells the program to enter stagedSpeed and then raceSpeed. Angle (0) should be all the way down.

I not actually certain it is at (0) when the program begins running

The above will set the ESC to 0 when it is attached. And, yes, it goes before the attach function.

i made the change but it still isnt arming

Please post your complete revised sketch

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.