CDI Ignition for golf cart

This is a project i have wanted to do for some time now, i am a bit intimated with coding and the golf cart that is use so much was still running, albeit slowly, now the spark is dead and never coming back so i would like a bit of assistance from the forum in creating a cdi.

I have searched many times on ignition and it seems that many people have done it, but raw information is not there or information that i can understand is missing.

The stock hardware setup on the golf cart, yamaha g2 4 stroke, is a pickup coil assumed to be hall effect due to the window on the flywheel, and a TCI ignition and coil. I wish to use the stock pickup coil and flywheel window to input into the arduino, and have the arduino ouput into a simple built cdi, and use gy6 or other cdi coil.

stock flywheel, not my photo, pickup coil is three wires, i plan to put a scope on it this weekend.

In the summer my brother and I did some thinking and some coding and this is the start that we have come up with, there is adafruit lcd screen i have omitted.

Basic principle is to see the window and to time that to ignite on the next revolution with the correct amount of spark advance. Current formula is based on stock advance and should yield factory advance. Other relevant information is these golf carts will run with a gy6 DC cdi wired in place of factory ingnitor and people have also used Chrysler boxes from the 70’s, but with wrong advance.

Supplies i currently have is a arduino duo, adafruit lcd sheild, bread board, and some misc components. I am better than average with working with components, and used to be to code html, coding should not be a problem after i get over the first hill.

thanks

//Variables & Storage
int Htime = 0;                  // integer for storing high time
int Ltime = 0;                  // integer for storing low time
int pickUp = 8;                 // Input from Pickup Coil(hall sensor)
int trigOut = 3;                // Output to Ingition Ctrl
int ADV = 0;                    // Static advance = 0
float Ttime = 0;                // integer for storing total time of a cycle
float Tdegree = 0;              // integer for storing Time Per Degree of a revolution
float Tadvance = 0;             // integer for storing time need to advance per a revolution
float Twait = 0;                // integer for storing time need to wait until spark event
float frequency = 0;            // storing frequency
float RPM = 0;                  // storing RPM
unsigned long displayCount;     // display counter
unsigned long currentTime;      // current time, based on time sketch has been running



void setup() {
  currentTime = millis();                       //get the current time (milliseconds the unit has been running)
  displayCount = currentTime;                   //set the display time to the current time
  // Debugging output
  Serial.begin(9600);
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);

  pinMode(pickUp, INPUT); //pin 8 is input
  pinMode(trigOut, OUTPUT); //Output

  /*  coming adafruit
    // Print a message to the LCD. We track how long it takes since
    // this library has been optimized a bit and we're proud of it :)
    int time = millis();
    lcd.print("Ardruino Start");
    time = millis() - time;
    Serial.print("Took "); Serial.print(time); Serial.println(" ms");
  */

  lcd.setBacklight(WHITE);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Engine RPM");
  lcd.setCursor(0, 1);
  lcd.print("RPM ");
}

//  uint8_t i=0;     WTF?
void loop() {

  currentTime = millis();        //get the current time
  Htime = pulseIn(pickUp, HIGH);      //read high time
  Ltime = pulseIn(pickUp, LOW);       //read low time
  Ttime = Htime + Ltime;          //Figureing up time for one revolution

  //calcs

  Tdegree = Ttime / 360;         // Time per single degree

  // calc advance between 1000 - 2500 RPMS

  if (RPM <= 1000)
  {
    ADV = 0;                        // Starting Advance
  }
  else if (RPM >= 2500)
  {
    ADV = 30;                       // Max Advance over 2500
  }
  else
  {
    ADV = (RPM - 1000) / 50;         // Start vari advance
  }

  Tadvance = Tdegree * ADV;          // calc time needed for ignigtion advance
  Twait = Ttime - Tadvance;          // time waiting from last pulse input to spark advance

  if (currentTime - displayCount > 100) {     //see if we have been at least 100 milliseconds since the last polling cycle
    // set the cursor to column 0, line 1

    if (Ttime == 0) {
      RPM = 0;
    }
    else {
      RPM = 16667 / Ttime; //getting frequency with Ttime is in Micro seconds, convert to RPM
      RPM = (int)RPM/10;
      RPM = (int)RPM*10;
      
      //frequency=1000000/Ttime;    //getting frequency with Ttime is in Micro seconds
    }

    lcd.setCursor(5, 1);
    lcd.print( (int) RPM);    //convert to int
    lcd.print("    ");              // overwrite garbage digits
    Serial.println(RPM);
    displayCount = currentTime;
  }



  uint8_t buttons = lcd.readButtons();

  if (buttons) {
    lcd.clear();
    lcd.setCursor(0, 0);
    if (buttons & BUTTON_UP) {
      // lcd.print("UP ");
      lcd.setBacklight(WHITE);
    }
    if (buttons & BUTTON_DOWN) {
      // lcd.print("DOWN ");
      lcd.setBacklight(YELLOW);
    }
    if (buttons & BUTTON_LEFT) {
      // lcd.print("LEFT ");
      lcd.setBacklight(GREEN);
    }
    if (buttons & BUTTON_RIGHT) {
      // lcd.print("RIGHT ");
      lcd.setBacklight(TEAL);
    }
    if (buttons & BUTTON_SELECT) {
      // lcd.print("SELECT ");
      lcd.setBacklight(VIOLET);
    }
  }


}