// Pin assignment
const int fuelPumpPin = 9; // Pin to control fuel pump relay
const int injectorPin = 10; // Pin to control injector
const int ignitionPin = 11; // Pin to control TCI (ignition)
const int pulserPin = 2; // Pin to read pulser (RPM sensor)
const int tpsPin = A0; // Analog pin for TPS (Throttle Position Sensor)
volatile unsigned long lastPulserTime = 0;
volatile unsigned long currentPulserTime = 0;
volatile int rpm = 0;
// Timing parameters
unsigned long fuelPumpOnTime = 3000; // 3 seconds fuel pump on during startup
// Ignition timing table based on RPM and throttle position (TPS)
const int rpmBins[] = {1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000}; //
RPM points
const int tpsBins[] = {20, 40, 60, 80, 100}; // Throttle position percentage bins
// Ignition timing values (in degrees)
const int ignitionTable[5][9] = {
{10, 12, 14, 16, 18, 20, 22, 24, 26}, // 0-20% TPS
{12, 14, 16, 18, 20, 22, 24, 26, 28}, // 20-40% TPS
{14, 16, 18, 20, 22, 24, 26, 28, 30}, // 40-60% TPS
{16, 18, 20, 22, 24, 26, 28, 30, 32}, // 60-80% TPS
{18, 20, 22, 24, 26, 28, 30, 32, 34} // 80-100% TPS
};
void setup() {
// Set pin modes
pinMode(fuelPumpPin, OUTPUT);
pinMode(injectorPin, OUTPUT);
pinMode(ignitionPin, OUTPUT);
pinMode(pulserPin, INPUT_PULLUP);
// Attach interrupt for RPM counting
attachInterrupt(digitalPinToInterrupt(pulserPin), readPulser, RISING);
// Turn on fuel pump for 3 seconds during startup
digitalWrite(fuelPumpPin, HIGH);
delay(fuelPumpOnTime);
digitalWrite(fuelPumpPin, LOW);
}
void loop() {
int tpsValue = analogRead(tpsPin); // Read TPS value
int throttlePosition = map(tpsValue, 0, 1023, 0, 100); // Convert TPS to
percentage
if (rpm > 0) {
// If engine is running
digitalWrite(fuelPumpPin, HIGH); // Fuel pump ON
// Control injector based on throttle position
controlInjector(throttlePosition);
// Control ignition timing based on RPM and throttle position
controlIgnition(rpm, throttlePosition);
} else {
// If engine is off
digitalWrite(fuelPumpPin, LOW); // Fuel pump OFF
digitalWrite(injectorPin, LOW); // Injector OFF
digitalWrite(ignitionPin, LOW); // Ignition OFF
}
delay(100); // Small delay for stability
}
void readPulser() {
// Measure time between pulser signals to calculate RPM
currentPulserTime = micros();
rpm = 60000000 / (currentPulserTime - lastPulserTime);
lastPulserTime = currentPulserTime;
}
void controlInjector(int throttlePosition) {
// Simple control: Injector ON for a period based on throttle position
int injectorOnTime = map(throttlePosition, 0, 100, 500, 2000); // Adjust timing
as needed
digitalWrite(injectorPin, HIGH);
delayMicroseconds(injectorOnTime);
digitalWrite(injectorPin, LOW);
}
void controlIgnition(int currentRPM, int throttlePosition) {
// Find appropriate ignition timing from table
int rpmIndex = findClosestIndex(currentRPM, rpmBins, 9);
int tpsIndex = findClosestIndex(throttlePosition, tpsBins, 5);
// Get the ignition timing from the table
int ignitionTiming = ignitionTable[tpsIndex][rpmIndex];
// Apply ignition timing
if (ignitionTiming > 0) {
digitalWrite(ignitionPin, HIGH);
delayMicroseconds(ignitionTiming);
digitalWrite(ignitionPin, LOW);
}
}
int findClosestIndex(int value, const int *array, int arraySize) {
// Find the closest index in the array for a given value
int closestIndex = 0;
for (int i = 1; i < arraySize; i++) {
if (abs(value - array[i]) < abs(value - array[closestIndex])) {
closestIndex = i;
}
}
return closestIndex;
}