So I am trying to create a code for an outlaw pinewood derby car. It is using a brushless esc with reverse for a ducted fan. The car has a push button on the front along with headlight and brake light LED's.
My goal is to push the front button 5 times to activate the car, then place it against the gate (which will press the front button once again and keep it pressed until the gate falls and the race begins). Once the button is released I want the fan to turn on and push the car down the track. once a certain amount of time passes (enough time to cross the finish line, I will get this info once I get a chance to do some test runs, but for now let's say 1.5 secs) it will reverse the fan (to act as a brake) for another certain amount of time, as well as activate the brake lights. then turn off the fan completely.
A buddy and I have been trying to write a code for it but are at a loss.
Any suggestion or input would be greatly appreciated.
#include <Servo.h> //Use the Servo library for generating PWM
Servo ESC; //name the servo object, here ESC
int headLightLED = 7; // White LED headlights
int brakeLightLED = 15; // Red LED tailights
int throttle = analogRead(A0); //Read the voltage from POT
int DigitalPot = 6; //Digital potentiometer used to map ESC
int potValue = A0; // value from the analog pin
int MicroSwitch = 9; // Front momentary push button
void setup()
{
pinMode (headLightLED, OUTPUT); // set headlights to output
pinMode (brakeLightLED, OUTPUT); // set tailights to output
pinMode (DigitalPot, OUTPUT); // Set digital potentiometer to output
pinMode (MicroSwitch, INPUT); // set front switch to input
ESC.attach(10,1000,2000); // Generate PWM in pin 10 of Arduino
delay (100);
ESC.write(10); // Activate ESC
delay(3000); // Wait for ESC to boot
throttle = map(throttle, 0, 1023, 0, 180); //Map the values of 0-1023 from pot to 0-180 bcs servo works only from 0-180
delay(1000);
digitalWrite(DigitalPot, LOW); //Send mininum signal to A0
analogRead (potValue);
delay(1000);
digitalWrite(DigitalPot, HIGH); // Send Maximum signal to A0
analogRead (potValue);
delay(1000);
digitalWrite(DigitalPot, LOW); // Return to zero throttle
analogRead (potValue);
delay(1000);
}
void loop()
{
if (MicroSwitch == HIGH){
int timeLapsed = millis();
}else(MicroSwitch == LOW);
ESC.write(throttle); // based on the value of throttle generate PWM signal
digitalWrite(throttle, 180); // Full Speed
delay(1500);
digitalWrite(throttle, 0); // Full Reverse
digitalWrite (brakeLightLED, HIGH); // activate brake lights
delay(1500);
digitalWrite (throttle, 90); // STOP
}