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);
}