const int idle = A0; // 900 rpm = 133Ms loop / fuel load 3Ms + 1Ms dead time runsmooth sequential
const int torque = A1; // 3500 rpm = 34Ms loop / fuel load 8Ms + 1Ms dead time runsmooth sequential
const int reving = A2; // 6000 rpm = 20Ms loop / fuel load 12Ms + 1Ms dead time runsmooth sequential
const int test = A3; // run a 19Ms loop for 10 seconds at 85% duty on, all outputs simultaneously
const int pwmPin1 = 3;
const int pwmPin2 = 5;
const int pwmPin3 = 6;
const int pwmPin4 = 9;
const int pwmPin5 = 10;
const int pwmPin6 = 11;
const int off = 0;
const int on = 255;
void setup() {
pinMode(idle, INPUT_PULLUP);
pinMode(torque, INPUT_PULLUP);
pinMode(reving, INPUT_PULLUP);
pinMode(test, INPUT_PULLUP);
pinMode(pwmPin1, OUTPUT);
pinMode(pwmPin2, OUTPUT);
pinMode(pwmPin3, OUTPUT);
pinMode(pwmPin4, OUTPUT);
pinMode(pwmPin5, OUTPUT);
pinMode(pwmPin6, OUTPUT);
}
void loop() {
if (digitalRead(idle) == LOW && digitalRead(torque) == LOW) {
// A0 and A1 both held low
runHighDutyCycle();
} else if (digitalRead(idle) == LOW) { // idle 133
runSmooth(129, 4);
} else if (digitalRead(torque) == LOW) { // torque 34
runSmooth(25, 9);
} else if (digitalRead(reving) == LOW) { // reving 20
runSmooth(8, 12);
} else if (digitalRead(test) == LOW) { // test
runTest();
}
}
void runSmooth(int cycleDelay, int squirtDelay) {
int pins[6] = {pwmPin1, pwmPin2, pwmPin3, pwmPin4, pwmPin5, pwmPin6};
int numberOfPins = 6;
int delayBetweenPins = cycleDelay / numberOfPins; // calculate delay between each pin activation
for (int i = 0; i < numberOfPins; i++) {
analogWrite(pins, on);
delay(squirtDelay); // squirt delay
analogWrite(pins, off);
delay(delayBetweenPins); // delay between each pin activation
}
}
void runHighDutyCycle() {
int pins[6] = {pwmPin1, pwmPin2, pwmPin3, pwmPin4, pwmPin5, pwmPin6};
int numberOfPins = 6;
int onTime = 17; // 85% of 20ms (17ms on, 3ms off)
int offTime = 3;
while (digitalRead(idle) == LOW && digitalRead(torque) == LOW) {
for (int i = 0; i < numberOfPins; i++) {
analogWrite(pins, on);
}
delay(onTime);
for (int i = 0; i < numberOfPins; i++) {
analogWrite(pins, off);
}
delay(offTime);
}
}
void runTest() {
int pins[6] = {pwmPin1, pwmPin2, pwmPin3, pwmPin4, pwmPin5, pwmPin6};
int numberOfPins = 6;
int onTime = 16; // 85% of 19ms (16ms on, 3ms off)
int offTime = 3;
unsigned long startTime = millis();
while (millis() - startTime < 10000) { // run for 10 seconds
for (int i = 0; i < numberOfPins; i++) {
analogWrite(pins, on);
}
delay(onTime);
for (int i = 0; i < numberOfPins; i++) {
analogWrite(pins, off);
}
delay(offTime);
}
}