CDI tester project

Hi, I have tested as far as I can without creating some more circuits and for that I want to have a few boards made. I have a error or about 40us which gives an error of +-2° at higher rpms. I think that may go away with the final circuit. I have my old tester to compare to and actual engines with timing light for final check of my PIC programming. The simulated sine out works well so that is an accomplishment!
So, I will say Thank you many times and I will go work on learning KiCad! I just use Eagle now but it is the small version.
Very Best, Tom

Great progress. :slight_smile:

I learned a few things from playing around with the simulated response the other day.

  1. Depending on the output from the CDI the return pulse interrupt on the tester will need to be either RISING or FALLING. Currently he interrupt pin is set as INPUT_PULLUP and the interrupt FALLING so the pin will respond to a grounding signal from the module. If the module puts out a high pulse, I think the interrupt will need to be changed. The interrupt pin might need an external pull down resistor.

2). When using FALLING mode for the trigger pulse, the time delay of the response pulse needs to be longer the trigger pulse width or else you see strange results as you are getting a negative value from the subtraction of start time from end time.

3).When using first pulse negative, the delayed response simulator needs to see a pulse from pin 8 and not pin 9.

  1. When the charge pulses are active, I see some additional delay (approx 30us) at 0 delay which I don't understand. The charge pulses should not be interactive with the trigger pulse or the return pulse interrupt but I see something. At longer delays, the effect seems reduced. I would keep my eye on the AC mode.

Good luck going forward. Please report back with how this project turns out.

OK
re #1, the spark generated by the CDI will always produce a grounding signal to the tester. I use a small SCR to do this and that is triggered by an inductive pickup. This keeps annoying rfi from getting back to the Arduino, they hate that stuff!

re #2, it is VERY likely that I will always use the rising edge of the first sine and will call that point rising whether that first sine is pos or neg. That is where the CDI starts its timing to create a pulse to trigger the SCR for the spark.

re #3, this may not apply? I am using the output after the op-amp that is driven by the DAC.

re #4, I have long been looking for a good way to simulate the HV from the magneto and the 5 pulses per rev seemed to be a workable solution. I have some small transformers with powdered iron pole pieces so frequency response should be much better that the lines transformer I had been using. I needed a transformer with a fairly low primary resistance and now have a few to try. I also have a DC - DC converter that is adjustable and can output 100 to 300 vdc. It may be a viable choice and that would allow the charge pulses to be completely removed. This is after all just for testing and measurement and not long term operation.

Best, Tom

Also I can code the Arduino to drive more bits of the DAC to make a better sine wave.
The simulated sine out works well so that is an accomplishment!

I don't understand the hardware and how the two A/B pulses (00,11,01) to the DAC creating -12, +12, and 0 volts generate a sine wave to the cdi. Is it created by the op amp and RC after the DAC?

Are you planning to separate some the the tied inputs on PinA to generate something different from the DAC?

Well I call it a sine wave, it is really just the most basic of such, a pos followed by a neg, really all I need to make a CDI see for triggering purposes.
Tom

Cattledog,
Hi, I am now making circuit board as I think I have everything else worked out. Have adjustable HV output switchable to 2 levels so once I get everything on one board I can see how the timing and such works. Seems to be good.
Again many thanks for your help, Tom

Cattledog, Hi, I find that I need to be able to have just the pos or just the neg pulse from the DAC and I don't understand where in the code that control is done. Ideally there would be 2 digital pins that one or the other pulled low would control this.
Thanks, Tom

Welcome back.

I find that I need to be able to have just the pos or just the neg pulse from the DAC

Do you need to have two modes--one where the two pulses pos/neg follow each other, and another with just one pulse or do you want the program to now only have the single pulse mode?

The selection of polarity switch will continue to control whether or not the single pulse is pos or neg, and we can treat the single pulse as the first pulse.

If you only want the one pulse mode, the code can be simplified to remove the bits needed to calculate the interpulse separation and the division of the timing cycle into two piece.

For now, here is quick revision which leaves the code pretty much intact, maintains the two pulse structure, but removes the second pulse. See if this gives the one pulse you want.

ISR(TIMER1_OVF_vect)
{
  //alternate ICR1 values to generate two outputs over 360 degrees
  //360 degree cycle time broken into two pieces
  //timerTopValue adjusted with RPM pot
  //timerTicksPerDegree = timerTopValue / 360; //gets new value to update ICR1
  static byte pulse = firstPulse;
  if (pulse == firstPulse)
  {
    ICR1 = timerTicksPerDegree * (pulseWidth + barWidth); //first pulse and bar width
    digitalWrite(13, HIGH);
    digitalWrite(chargePin, LOW); //guarantee charge pin off at trigger
    if (firstPulsePos == true)
      setPulse(positive);
    else
      setPulse(negative);
    //set timing start at lead edge of first pulse
    if (risefall == 'R')
    {
      delayPeriodStart = micros();//start looking for response as pulse rises
      trigger = true;
    }
    else //risefall == 'F' set timing at trailing edge of first pulse
    {
      //convert pulseWidthTime in timer ticks to microseconds
      //prescaler 8 = 4us/timer tick
      delayPeriodStart = micros() + pulseWidthTime * 4;//start looking for response as pulse falls
      trigger = true;
    }
    //start Timer 2 for charge pulses
    if (chargePulse)
    {
      timeSliceCount = 0;
      TCNT2 = 0;
      OCR2A = timerTopValue / 96; //set 12 periods
      //start timer running
      TCCR2B |=  1 << CS22 | 1 << CS21; //prescaleer 256 16us/tick
    }

    pulse = secondPulse; //next pulse
  }
  else //second pulse
  {
    ICR1 = timerTicksPerDegree * (360 - (pulseWidth + barWidth)); //second pulse and dead band
    digitalWrite(13, HIGH);

    //block the second pulse
    //if (firstPulsePos)
      //setPulse(negative);
    //else
      //setPulse(positive);

    pulse = firstPulse; //next pulse
  }
}

OK, thanks, at first I thought it did not work but it is fine! I have an issue with the neg pulse triggering the CDI and have not found it yet. This is my first time using a DAC for this so I don't know if the pulse is to short. More later!
Best, Tom

Cattledog,
Hi my friend, well sometimes the best laid plans just are not the best! I had thought that using a DAC was a good way to have neg pulse from Arduino. It is for some uses perhaps but the CDI input circuit draws much more current that can be supplied.
So a change of plans is sadly in order. I find that I just need 1 pulse for the trigger plus the 5 pulses for the HV supply. The circuit can just sample from the Rise of the trigger pulse by default and the bar width and position can remain with the rpm pot. Also the D6 to turn on the output at D7 can stay.
Much like the one you did a couple of years ago but I am going to finally add the pots and make a proper circuit board. I feel like it was a good idea and was suggested to me by a EE friend. But it works better to keep it simple!
Again, many thanks, Tom

Cattledog,
I have worked with the code and with the hardware to get a pretty nice result I think. It is mostly a rework of the old version. What I am stumbling on is the DC output. I just want to turn off the HV pulse generator and keep everything else running. When I remove pin 6 from ground the output on pin 5 gets strange, going in cycles. I also want to keeo the reporting of degrees advance in the DC mode.
Thanks, Tom

// replaced on 7/22/18 mod on 4/18/2019 changed to go simple transformer control of pos / neg trigger
// update 7/14/2017 added correct math and setting for position of pickup
// revision 10 10/28/2016 adds charging pulse with Timer 2
// lowest RPM = 615 with charge pulse to keep OCR2A <= 255
// CDI Tester Pulse Generator Serial Output Arduino Code
// 8/17 added code for pulse out width

int pot1 = A1; // select the input pin for the pot for rpm
int pot2 = A2; // select the input pin for the pot for pickup location in degrees
// int pot3 = A3; // select the input pin for the pot for length of bar on flywheel in degrees
int potValue = 0; // variable to store the value coming from the sensor
int pickupValue = 0; // position of pickup in degrees
int potWidth = 0; // variable to set length of bar on flywheel
int timerTopValue = 12500; // changed from timerTopValue = 0
int outputPin = 4; // select the pin for the output for trigger pulse, changed from 8
int chargePin = 7; // select the pin for the output for charge pulses

volatile boolean trigger = false;
volatile unsigned long delayPeriod;
unsigned long copy_delayPeriod;
volatile unsigned long delayPeriodStart;
float delayDegrees; // changed from int to float for decimal place display
int RPM;
int pickup;
// int barLength;
int pulseWidth;                                            
volatile boolean interruptFlag;
unsigned long analogReadInterval = 500; // read pots and map
unsigned long lastAnalogRead;

// const byte setFallingSwitch = 5;
// char risefall = 'R'; // default rising mode

const byte setChargePulseSwitch = 6;
boolean chargePulse = false ;  // default dc powered mode

volatile byte timeSliceCount = 0; // TDC 0 degrees

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// set the LCD address to 0x3f for a 20 chars 4 line display
// Set the pins on the I2C chip used for LCD connections:
//                    addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x3f, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address

void setup() {
// Serial.begin(115200);
// Serial.println("starting...");
  lcd.begin(20, 4); // initialize the lcd for 20 chars 4 lines, turn on backlight
  lcd.setCursor(1, 0); // lcd display setup of unchanging headings
  lcd.print("RPM:"); // print fixed characters
  lcd.setCursor(12, 0);
  lcd.print("Deg"); 
  lcd.setCursor(1, 1);
  lcd.print("Pos:");
//  lcd.setCursor(12, 1);
//  lcd.print("Bar:");
  lcd.setCursor(1, 2);
  lcd.print("Us Delay:");
  lcd.setCursor(1, 3);
  lcd.print("Deg Advance:");

  pinMode(outputPin, OUTPUT); // declare the outputPin as an OUTPUT
  pinMode(chargePin, OUTPUT); // declare the chargePin as an OUTPUT
// pinMode (setFallingSwitch, INPUT_PULLUP); // check for a LOW input to indicate switch is closed to ground
  pinMode(setChargePulseSwitch, INPUT_PULLUP);

  if (digitalRead(setChargePulseSwitch) == LOW)
    chargePulse = true; // AC CDI

 // Timer1 default set up .5ms trigger pulse every 50 ms
  TCCR1A = 0;
  TCCR1B = (1 << WGM12); // CTC mode to OCR1A
  OCR1A = timerTopValue;
  TIMSK1 |= (1 << OCIE1A); // interrupt enable compareA
  TIMSK1 |= (1 << OCIE1B); // interrupt enable compareB
  OCR1B = 500; // sets trigger pulse width, 500 = 2Ms, 250 = 1Ms, 125 = .5Ms, set to 1250 for 5013 *********************************************
  TCCR1B |= (1 << CS11) | (1 << CS10); // prescaler 64 4us/tick

// Timer2 default setup charge pulse 12 periods 30 degrees each; only 5 get turned on
// charge pulse timing interval = Timer1 trigger period/12 = timerTopValue/96
  TCCR2A = 0;
  TCCR2B = 0;
  TCCR2A = 1 << WGM20; // lsb of mode 7 pwm to OCR2A
  TCCR2B = 1 << WGM22; // msb of mode 7 pwm to OCR2A;
  OCR2A = timerTopValue / 96; // 96 = 12*4*2 (#periods, prescaler difference, up/down timer mode )
  TIMSK2 = 0;
  TIMSK2 = 1 << TOIE2; // enable overflow interrupt
// actually start timer in ISR(TIMER1_COMPA_vect
// to prevent out of sync charge pulse, no prescaler set here

  attachInterrupt(digitalPinToInterrupt(3), delayPeriodTiming, FALLING);

}

void loop()
{
  if (millis() - lastAnalogRead >= analogReadInterval)
  {
    lastAnalogRead += analogReadInterval;
    potValue = analogRead(pot1); // rpm
    pickupValue = analogRead(pot2); // pickup position
//  barLength = analogRead(pot3); // length of bar for pickup 
  
// Timer2 OCR2A requires max value 255 => 615 lowest RPM
    if (chargePulse)
    {
      RPM = map(potValue, 0, 1023, 615, 10000);
      pickup = map(pickupValue, 0, 1023, 10, 75); // so advance will read based on delay, 57 for yamaha 350 2 pu, 74 for 1 pu, 72 for tw200, 25 for 5013
      pulseWidth = (60000000/RPM)/360; // time for 1° in uS
//    barSize = map(barLength, 0, 1023, 10, 60);  // 10 for yamaha 350 2 pu, 60 for 1 pu, 60 for tw200
// RPM = 615; // for my serial test purposes 615-3800
    }
    else
    {
      RPM = map(potValue, 0, 1023, 500, 3800);
      pickupValue = map(pickupValue, 0, 1023, 0, 50); // to set position of pickup so advance will read
//    pulseWidth = map(barLength, 0, 1023, 71, 44); // add Width to non charging case
// RPM = 500; // for my serial test purposes 500-3800
    }

    timerTopValue = 15000000UL / RPM;
    OCR1B = pulseWidth;

// if (digitalRead(setFallingSwitch) == LOW) // set Falling
// {
// risefall = 'F';
// }
// else
// {
// risefall = 'R';
// }

    lcd.setCursor(6, 0);
    lcd.print("     "); // print blank spaces to clear old data
    lcd.setCursor(6, 0);
    lcd.print(RPM);  // print rpm

    lcd.setCursor(16, 0);
    lcd.print("  ");
    lcd.setCursor(16, 0);
    lcd.print(pulseWidth, 1); // print us per degree
    
    lcd.setCursor(6, 1);
    lcd.print("   ");
    lcd.setCursor(6, 1);
    lcd.print(pickup); // print pickup coil position

//    lcd.setCursor(16, 1);
//    lcd.print("   ");
//    lcd.setCursor(16, 1);
//    lcd.print(barLength); // length of bar

    lcd.setCursor(11, 2);
    lcd.print("       ");
    lcd.setCursor(11, 2);
    lcd.print(copy_delayPeriod);
   
    lcd.setCursor(14, 3);
    lcd.print("      ");
    lcd.setCursor(14, 3);
    lcd.print(delayDegrees, 1);   // delayDegrees, 1);
  }
 
  if (trigger == true && interruptFlag == true )
  {
    trigger = false;
    interruptFlag = false;
    noInterrupts();
    copy_delayPeriod = delayPeriod;
    interrupts();

    delayDegrees = pickup - (360.0 * (copy_delayPeriod) / (timerTopValue * 4.0)); // for decimal place in deg display.
    }
}

ISR(TIMER1_COMPA_vect) {
  OCR1A = (timerTopValue); // value to set delay between pulses to trigger cdi
  digitalWrite(chargePin, LOW); // guarantee off charge pin at trigger
  digitalWrite(outputPin, HIGH); // turn on pin  trigger

// if (risefall == 'R') // switch not set; default Rising trigger
  {
    delayPeriodStart = micros(); // start looking for response as pulse rises
    trigger = true;
  }
// start Timer 2 for charge pulses
  if (chargePulse)
  {
    timeSliceCount = 0;
    TCNT2 = 0;
    OCR2A = timerTopValue/96; // set 12 periods
    TCCR2B |=  1 << CS22 | 1 << CS21; //prescaleer 256 16us/tick
  }
}

ISR(TIMER1_COMPB_vect) {
  digitalWrite(outputPin, LOW);

// if (risefall == 'F') // switch set for Falling trigger
  {
    delayPeriodStart = micros(); // start looking for response as pulse falls
    trigger = true;
  }
}

void delayPeriodTiming()
{
  delayPeriod = micros() - delayPeriodStart;
  interruptFlag = true;
}

ISR(TIMER2_OVF_vect)
// 5 pulses of 30 degrees starting at 60 degrees
// ON at 60,120,180,240,300 = 2,4,6,8,10
// OFF at 90,150,210,270,330 = 3,5,7,9,11
{
  if (timeSliceCount != 0 && timeSliceCount % 2 == 0)
  {
    digitalWrite (chargePin, HIGH);
  }
  else // if (timeSliceCount == 0 || timeSliceCount % 2 == 1)
  {
    digitalWrite(chargePin, LOW);
  }
  timeSliceCount++;

  if (timeSliceCount == 12)
  {
    timeSliceCount = 0;
// stop Timer2 by clearing prescaler bits
    TCCR2B &= ~1<< CS22;
    TCCR2B &= ~1<< CS21;
  }
}

What I am stumbling on is the DC output. I just want to turn off the HV pulse generator and keep everything else running. When I remove pin 6 from ground the output on pin 5 gets strange, going in cycles.

// const byte setFallingSwitch = 5;
// char risefall = 'R'; // default rising mode
//pinMode (setFallingSwitch, INPUT_PULLUP);

You have removed the pinMode for pin5 and it will be floating. I'm somewhat unclear about what you are trying to do with the R/F setting, and you have a conflict here as you are setting the start time for both R and F modes.

ISR(TIMER1_COMPA_vect) {
  OCR1A = (timerTopValue); // value to set delay between pulses to trigger cdi
  digitalWrite(chargePin, LOW); // guarantee off charge pin at trigger
  digitalWrite(outputPin, HIGH); // turn on pin  trigger

// if (risefall == 'R') // switch not set; default Rising trigger
  {
    delayPeriodStart = micros(); // start looking for response as pulse rises
    trigger = true;
  }
// start Timer 2 for charge pulses
  if (chargePulse)
  {
    timeSliceCount = 0;
    TCNT2 = 0;
    OCR2A = timerTopValue/96; // set 12 periods
    TCCR2B |=  1 << CS22 | 1 << CS21; //prescaleer 256 16us/tick
  }
}

ISR(TIMER1_COMPB_vect) {
  digitalWrite(outputPin, LOW);

// if (risefall == 'F') // switch set for Falling trigger
  {
    delayPeriodStart = micros(); // start looking for response as pulse falls
    trigger = true;
  }
}

I also want to keep the reporting of degrees advance in the DC mode.

I see in the last bi-polar pulse code we had

degreesAdvance = pickupValue - delayDegrees;

I think you can just add it here, and display it as you do in the bipolar sketch.

delayDegrees = pickup - (360.0 * (copy_delayPeriod) / (timerTopValue * 4.0)); // for decimal place in deg display.
degreesAdvance = pickupValue - delayDegrees;

Hi, I see I misspoke in last message. It should say "What I am stumbling on is the DC output. I just want to turn off the HV pulse generator and keep everything else running. When I remove pin 6 from ground the output on pin 4 gets strange, going in cycles."

OK let me clarify as it may make a difference. I don't think I need a switch to choose between Rise and Fall for the start of the timing operation. It will always be from the Rising edge so I wanted to remove that part of the code and have it operate in that mode only.
Also if it simplifies things there is no need for the code to know when the charge pulses are being used, just a way to turn them on or off by switch. The same switch will turn on the DC when it turns off the charge pulses.
I have had a hardware problem for a long time and did not realize it which is why I thought the DAC may be the answer, but once I found that problem it all became much simpler.

I should also note I am using the pins of the old system, A1 is rpm, A2 is pickup position, D3 is input from sensor for spark, D4 is pulse trigger out, D6 is charge pulse on, D7 is charge pulse out.
Tom

I should also note I am using the pins of the old system, A1 is rpm, A2 is pickup position, D3 is input from sensor for spark, D4 is pulse trigger out, D6 is charge pulse on, D7 is charge pulse out.

When I remove pin 6 from ground the output on pin 4 gets strange, going in cycles."

I can't see any software interaction between the pulse trigger and the charge pulses or charge pulse selector switch . Can you show the pulse trigger output on a scope? Are you looking at the Arduino output of pin 4, or the HV trigger?

D6 is charge pulse on, D7 is charge pulse out

Since you moved the switch to D6, are you certain of the wiring, and can confirm that the INPUT_PULLUP is active. Use a simple test with digitalRead() to check the output of the switch.

Once I'm more clear about what is going on, I can simplify the sketch to remove stuff that isn't needed.

I would also recommend that you go back to using the hd44780 library. It really is superior and will make life easy if you have to change a display.

When I open the switch for D6, it goes to 5v, I have to reset, then output on D7 stops. After reset, the output on D4 runs for several pulses then stops with just a very tiny noise pulse occasionally. I have to reset each time I change D6.
Tom

I have to reset each time I change D6.

That can be fixed with the changes we made in the Bipolar Pulse Code where the switch checking was moved out of setup and into loop(). It can be added in loop where you do the analogRead() every 500ms or els try and adopt the setSwichPresets function in the last Bipolar code.

After reset, the output on D4 runs for several pulses then stops with just a very tiny noise pulse occasionally.

I am really confused. After reset with D6 changed, it should just look like a fresh start with D6 set for DC. Do you see normal trigger pulses on D4 in DC mode at all?

Just to be sure I didn't post a wrong sketch, But what happens is that I get 4 or 5 pulses from D4 when D6 is high. This happens when I reset or do a cold start and looking with scope.

// replaced on 7/22/18 mod on 4/18/2019 changed to go simple transformer control of pos / neg trigger
// update 7/14/2017 added correct math and setting for position of pickup
// revision 10 10/28/2016 adds charging pulse with Timer 2
// lowest RPM = 615 with charge pulse to keep OCR2A <= 255
// CDI Tester Pulse Generator Serial Output Arduino Code
// 8/17 added code for pulse out width

#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h> // include i/o class header
hd44780_I2Cexp lcd; // declare lcd object: auto locate & config display for hd44780 chip

int pot1 = A1; // select the input pin for the pot for rpm
int pot2 = A2; // select the input pin for the pot for pickup location in degrees
// int pot3 = A3; // select the input pin for the pot for length of bar on flywheel in degrees
int potValue = 0; // variable to store the value coming from the sensor
int pickupValue = 0; // position of pickup in degrees
int potWidth = 0; // variable to set length of bar on flywheel
int timerTopValue = 12500; // changed from timerTopValue = 0
int outputPin = 4; // select the pin for the output for trigger pulse, changed from 8
int chargePin = 7; // select the pin for the output for charge pulses

volatile boolean trigger = false;
volatile unsigned long delayPeriod;
unsigned long copy_delayPeriod;
volatile unsigned long delayPeriodStart;
float delayDegrees; // changed from int to float for decimal place display
int RPM;
int pickup;
// int barLength;
int pulseWidth;                                            
volatile boolean interruptFlag;
unsigned long analogReadInterval = 500; // read pots and map
unsigned long lastAnalogRead;

// const byte setFallingSwitch = 5;
// char risefall = 'R'; // default rising mode

const byte setChargePulseSwitch = 6;
boolean chargePulse = false ;  // default dc powered mode

volatile byte timeSliceCount = 0; // TDC 0 degrees

void setup() {
// Serial.begin(115200);
// Serial.println("starting...");
  lcd.begin(20, 4); // initialize the lcd for 20 chars 4 lines, turn on backlight
  lcd.setCursor(1, 0); // lcd display setup of unchanging headings
  lcd.print("RPM:"); // print fixed characters
  lcd.setCursor(12, 0);
  lcd.print("Deg"); 
  lcd.setCursor(1, 1);
  lcd.print("Pos:");
//  lcd.setCursor(12, 1);
//  lcd.print("Bar:");
  lcd.setCursor(1, 2);
  lcd.print("Us Delay:");
  lcd.setCursor(1, 3);
  lcd.print("Deg Advance:");

  pinMode(outputPin, OUTPUT); // declare the outputPin as an OUTPUT
  pinMode(chargePin, OUTPUT); // declare the chargePin as an OUTPUT
// pinMode (setFallingSwitch, INPUT_PULLUP); // check for a LOW input to indicate switch is closed to ground
  pinMode(setChargePulseSwitch, INPUT_PULLUP);

  if (digitalRead(setChargePulseSwitch) == LOW)
    chargePulse = true; // AC CDI

 // Timer1 default set up .5ms trigger pulse every 50 ms
  TCCR1A = 0;
  TCCR1B = (1 << WGM12); // CTC mode to OCR1A
  OCR1A = timerTopValue;
  TIMSK1 |= (1 << OCIE1A); // interrupt enable compareA
  TIMSK1 |= (1 << OCIE1B); // interrupt enable compareB
  OCR1B = 500; // sets trigger pulse width, 500 = 2Ms, 250 = 1Ms, 125 = .5Ms, set to 1250 for 5013 *********************************************
  TCCR1B |= (1 << CS11) | (1 << CS10); // prescaler 64 4us/tick

// Timer2 default setup charge pulse 12 periods 30 degrees each; only 5 get turned on
// charge pulse timing interval = Timer1 trigger period/12 = timerTopValue/96
  TCCR2A = 0;
  TCCR2B = 0;
  TCCR2A = 1 << WGM20; // lsb of mode 7 pwm to OCR2A
  TCCR2B = 1 << WGM22; // msb of mode 7 pwm to OCR2A;
  OCR2A = timerTopValue / 96; // 96 = 12*4*2 (#periods, prescaler difference, up/down timer mode )
  TIMSK2 = 0;
  TIMSK2 = 1 << TOIE2; // enable overflow interrupt
// actually start timer in ISR(TIMER1_COMPA_vect
// to prevent out of sync charge pulse, no prescaler set here

  attachInterrupt(digitalPinToInterrupt(3), delayPeriodTiming, FALLING);

}

void loop()
{
  if (millis() - lastAnalogRead >= analogReadInterval)
  {
    lastAnalogRead += analogReadInterval;
    potValue = analogRead(pot1); // rpm
    pickupValue = analogRead(pot2); // pickup position
    
//  barLength = analogRead(pot3); // length of bar for pickup 
  
// Timer2 OCR2A requires max value 255 => 615 lowest RPM
    if (chargePulse)
    {
      RPM = map(potValue, 0, 1023, 615, 10000);
      pickup = map(pickupValue, 0, 1023, 10, 75); // so advance will read based on delay, 57 for yamaha 350 2 pu, 74 for 1 pu, 72 for tw200, 25 for 5013
      pulseWidth = (60000000/RPM)/360; // time for 1° in uS
//    barSize = map(barLength, 0, 1023, 10, 60);  // 10 for yamaha 350 2 pu, 60 for 1 pu, 60 for tw200
// RPM = 615; // for my serial test purposes 615-3800
    }
    else
    {
      RPM = map(potValue, 0, 1023, 500, 3800);
      pickupValue = map(pickupValue, 0, 1023, 0, 50); // to set position of pickup so advance will read
//    pulseWidth = map(barLength, 0, 1023, 71, 44); // add Width to non charging case
// RPM = 500; // for my serial test purposes 500-3800
    }

    timerTopValue = 15000000UL / RPM;
    OCR1B = pulseWidth;

// if (digitalRead(setFallingSwitch) == LOW) // set Falling
// {
// risefall = 'F';
// }
// else
// {
// risefall = 'R';
// }

    lcd.setCursor(6, 0);
    lcd.print("     "); // print blank spaces to clear old data
    lcd.setCursor(6, 0);
    lcd.print(RPM);  // print rpm

    lcd.setCursor(16, 0);
    lcd.print("  ");
    lcd.setCursor(16, 0);
    lcd.print(pulseWidth, 1); // print us per degree
    
    lcd.setCursor(6, 1);
    lcd.print("   ");
    lcd.setCursor(6, 1);
    lcd.print(pickup); // print pickup coil position

//    lcd.setCursor(16, 1);
//    lcd.print("   ");
//    lcd.setCursor(16, 1);
//    lcd.print(barLength); // length of bar

    lcd.setCursor(11, 2);
    lcd.print("       ");
    lcd.setCursor(11, 2);
    lcd.print(copy_delayPeriod);
   
    lcd.setCursor(14, 3);
    lcd.print("      ");
    lcd.setCursor(14, 3);
    lcd.print(delayDegrees, 1);   // delayDegrees, 1);
  }
 
  if (trigger == true && interruptFlag == true )
  {
    trigger = false;
    interruptFlag = false;
    noInterrupts();
    copy_delayPeriod = delayPeriod;
    interrupts();

    delayDegrees = pickup - (360.0 * (copy_delayPeriod) / (timerTopValue * 4.0)); // for decimal place in deg display.
//    degreesAdvance = pickupValue - delayDegrees; // added
    }
}

ISR(TIMER1_COMPA_vect) {
  OCR1A = (timerTopValue); // value to set delay between pulses to trigger cdi
  digitalWrite(chargePin, LOW); // guarantee off charge pin at trigger
  digitalWrite(outputPin, HIGH); // turn on pin  trigger

// if (risefall == 'R') // switch not set; default Rising trigger
  {
    delayPeriodStart = micros(); // start looking for response as pulse rises
    trigger = true;
  }
// start Timer 2 for charge pulses
  if (chargePulse)
  {
    timeSliceCount = 0;
    TCNT2 = 0;
    OCR2A = timerTopValue/96; // set 12 periods
    TCCR2B |=  1 << CS22 | 1 << CS21; //prescaleer 256 16us/tick
  }
}

ISR(TIMER1_COMPB_vect) {
  digitalWrite(outputPin, LOW);

// if (risefall == 'F') // switch set for Falling trigger
  {
    delayPeriodStart = micros(); // start looking for response as pulse falls
    trigger = true;
  }
}

void delayPeriodTiming()
{
  delayPeriod = micros() - delayPeriodStart;
  interruptFlag = true;
}

ISR(TIMER2_OVF_vect)
// 5 pulses of 30 degrees starting at 60 degrees
// ON at 60,120,180,240,300 = 2,4,6,8,10
// OFF at 90,150,210,270,330 = 3,5,7,9,11
{
  if (timeSliceCount != 0 && timeSliceCount % 2 == 0)
  {
    digitalWrite (chargePin, HIGH);
  }
  else // if (timeSliceCount == 0 || timeSliceCount % 2 == 1)
  {
    digitalWrite(chargePin, LOW);
  }
  timeSliceCount++;

  if (timeSliceCount == 12)
  {
    timeSliceCount = 0;
// stop Timer2 by clearing prescaler bits
    TCCR2B &= ~1<< CS22;
    TCCR2B &= ~1<< CS21;
  }
}

But what happens is that I get 4 or 5 pulses from D4 when D6 is high

Just to be clear. The DC mode is not working properly and all you see are a few trigger pulses on D4 and then they go away.

In AC mode, with the charge pulses, do you see correct trigger pulse output on D4?
Was DC mode working with the original mono pulse code we started with?
Was DC mode working in the Bipolar pulse sketch?

cattledog:
Just to be clear. The DC mode is not working properly and all you see are a few trigger pulses on D4 and then they go away.
Yes that is right

In AC mode, with the charge pulses, do you see correct trigger pulse output on D4?
Yes they are fine then.

Was DC mode working with the original mono pulse code we started with?
Yes

Was DC mode working in the Bipolar pulse sketch?
Sorry, not sure and that test bed is not usable right now.

What I tried to do was go back to the older sketch with the mono pulse output.

Just to be clear. The DC mode is not working properly and all you see are a few trigger pulses on D4 and then they go away.
Yes that is right

I can not confirm what you are seeing. I loaded the code that you posted in #246 and made the RPM fixed at 1500 with this line which I uncommented. The else refers to when there are no charge pulses. That is with pin6 pulled HIGH by its INPUT_PULLUP.

else
    {
      RPM = map(potValue, 0, 1023, 500, 3800);
      pickupValue = map(pickupValue, 0, 1023, 0, 50); // to set position of pickup so advance will read
//    pulseWidth = map(barLength, 0, 1023, 71, 44); // add Width to non charging case
 RPM = 1500; // for my serial test purposes 500-3800

I jumpered the output of pin 4 to a second Arduino (grounds connected) on pin 2 where I ran the following simple interrupt counting code. I saw 25 counts per second which the the 1500 rpm. Pin 4 was outputting when the charge pulses were disabled. I'm not clear about what is going on with the scope or your hardware, but see if you can see what I see with a second Arduino.

volatile unsigned long  count = 0;
unsigned long copyCount = 0;

unsigned long lastRead = 0;
unsigned long interval = 1000;//one second

void setup()
{
  Serial.begin(115200);
  Serial.println("start...");
  
  //interrupt on pin2
  pinMode(2,INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(2),isrCount,RISING);
}

void loop()
{
  if (millis() - lastRead >= interval) //read interrupt count every second
  {
    lastRead  += interval;
    // disable interrupts,make copy of count,reenable interrupts
    noInterrupts();
    copyCount = count;
    count = 0;
    interrupts();
    //use copyCount for all calulations and actions in loop
    Serial.println(copyCount);
  }
}

void isrCount()
{
  count++;
}