Crank pulse simulator..similar to Frequency generator..need help

If you can imagine a encoder wheel with 24 evenly space squares\valleys and 2 blank square\valley areas, this is what I am trying to simulate as an output from my Arduino.

I am trying to create a square wave currently from pin13 that will cycle High to Low 24 times then cycle Low, Low 2 times. During the first Low, Low cycle, I want to cycle pin 12 High to low 1 time. After this is complete I want to repeat the steps.

This issue that I am need help with is that my “for” loop is going to be “blocking” and also I need to vary the frequency of the loop. This would represent the possible speed of an engine from 800 rpm to 17,000 rpm.
I would like to use a pot for the speed adjust and be able to read from the pins if possible. I am willing to use additional boards if I must.

Here is my code thus far:

/*
//  
 800rpm = 13.33Hz = 75ms per RPM/26 = 2.88ms per pulse set... time between pulse state change is 1.44 ms
 17,000rpm =283.33Hz = 3.529ms per RPM/26 = .1357ms per pulse set... time between pulse state change is .06787ms

 
 */
int timer = 100;           // The higher the number, the slower the timing.

void setup() {

  pinMode(13, OUTPUT);   
  pinMode(12, OUTPUT);   

}

void loop() {
  // loop for 24 cycles
  for (int x = 0; x < 24; x++) { 
    // turn the pin on:
    digitalWrite(13, HIGH);   
    delay(timer);                  
    // turn the pin off:
    digitalWrite(13, LOW); 
    delay(timer);   
  }

  ////////////////////////////// keep pin 13 low for 2 cycles 
  digitalWrite(13, LOW);
  // pulse pin 12 one time for one cycle:
  digitalWrite(12, HIGH);
  delay(timer);
  digitalWrite(12, LOW);
  delay(timer);
  digitalWrite(13, LOW); 
  delay(timer);
  digitalWrite(13, LOW); 
  delay(timer); 

}

I have this part set this way to keep my mind clear on my task. I do want to improve the sketch where possible.

digitalWrite(13, LOW);
  // pulse pin 12 one time for one cycle:
  digitalWrite(12, HIGH);
  delay(timer);
  digitalWrite(12, LOW);
  delay(timer);
  digitalWrite(13, LOW); 
  delay(timer);
  digitalWrite(13, LOW); 
  delay(timer);

I changed my strategy I just needed to think in a new direction. Here is my code so far, I need to make the interval adjustable and use micros instead of millis.
It uses a modified version of blink with out delay. I also put in prints in to show progress threw the “if” statements.

/*

 800rpm = 13.33Hz = 75ms per RPM/26 = 2.88ms per pulse set... time between pulse state change is 1.44 ms
 17,000rpm =283.33Hz = 3.529ms per RPM/26 = .1357ms per pulse set... time between pulse state change is .06787ms
 
 future use 
 int sensorPin = A0;    // select the input pin for the potentiometer
 int sensorValue = 0;  // variable to store the value coming from the sensor
 int val = 0;
 sensorValue = analogRead(sensorPin);
 val = map(sensorValue, 0, 1023, 0, 255);
 
 */
const int crankpin =  13;      // the number of the LED pin //crank pulse
const int campin = 12;         // the number of the LED pin  // cam pulse
int ledState = LOW;             // ledState used to set the LED
int ledState2 = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated
long interval = 500;           // interval at which to blink (milliseconds
int i = 1;


void setup() {
  Serial.begin(9600);
  // set the digital pin as output:
  pinMode(crankpin, OUTPUT);  
  pinMode(campin, OUTPUT);  
}

void loop()
{
  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > interval) { 
    if( i < 24){  
      Serial.println(i); 
      i = i++;
      previousMillis = currentMillis;   

      if (ledState == LOW)
        ledState = HIGH;
      else
        ledState = LOW;
      digitalWrite(crankpin, ledState);
    }
  }

  if(currentMillis - previousMillis > interval) {
      if(i == 24 || i == 25){
      Serial.println(i);
      i = i++;
      previousMillis = currentMillis;   
      if (ledState2 == LOW)
        ledState2 = HIGH;
      else
        ledState2 = LOW;

      digitalWrite(campin, ledState2);
    }
  }

  if(currentMillis - previousMillis > interval) {
    if (i ==26) {
      Serial.println(i);
      previousMillis = currentMillis;  
      ///need some delay to keep cycles even
      i = 1;
    }
  }

}

OK latest update. I have a pot that adjusts the interval that determines when cycles occur. I am using a analogread and a map function to make it work with micros() to adjust the interval. I have also added a Serial.print called Hz. to show the frequency of the 26 pulse run. It does not work yet it keeps showing 0.00, I am still trying to figure it out.

none working time code:

if (i == 2){
    time = (currentMillis - previoustime); 
    Hz = (1 / time);  
    Serial.print("Time ");                   //print current interval
    Serial.println(Hz);
    previoustime = currentMillis;
  }
/*

 800rpm = 13.33Hz = 75ms per RPM/26 = 2.88ms per pulse set... time between pulse state change is 1.44 ms
 17,000rpm =283.33Hz = 3.529ms per RPM/26 = .1357ms per pulse set... time between pulse state change is .06787ms
 
 
 */
int sensorPin = A0;              // select the input pin for the potentiometer
int sensorValue = 0;            // variable to store the value coming from the sensor
unsigned long val = 0;                    //value to be used with map
const int crankpin =  13;      // the number of the LED pin //crank pulse
const int campin = 12;         // the number of the LED pin  // cam pulse
int ledState = LOW;            // ledState used to set the LED
int ledState2 = LOW;           // ledState used to set the LED
unsigned long previousMillis = 0;      // will store last time LED was updated
unsigned long interval = 500;          // interval at which to blink (milliseconds
unsigned long time = 0;
unsigned long previoustime = 0;      // will store last time Time was updated
float Hz = 0;
int i = 1;


void setup() {
  Serial.begin(115200);
  // set the digital pin as output:
  pinMode(crankpin, OUTPUT);  
  pinMode(campin, OUTPUT);  
}

void loop()
{
  sensorValue = analogRead(sensorPin);
  val = map(sensorValue, 0, 1023, 65, 1440);   // interval adjuster from .065ms to 1.44ms
  interval = val;
  unsigned long currentMillis = micros();

  if (i == 2){
    time = (currentMillis - previoustime); 
    Hz = (1 / time);  
    Serial.print("Time ");                   //print current interval
    Serial.println(Hz);
    previoustime = currentMillis;
  }

  if(currentMillis - previousMillis > interval) { // 24 cycles of HIGH to LOW on pin 13 crankpin
    if( i < 24){  
      Serial.println(i); 
      i = i++;
      previousMillis = currentMillis;   

      if (ledState == LOW)
        ledState = HIGH;
      else
        ledState = LOW;
      digitalWrite(crankpin, ledState);
    }
  }

  if(currentMillis - previousMillis > interval) {  // 1st cycle of LOW, LOW on pin 13 crankpin
    if(i == 24 || i == 25){                        // cycle High, Low pin 12 cam pin
      Serial.println(i);
      i = i++;
      previousMillis = currentMillis;   
      if (ledState2 == LOW)
        ledState2 = HIGH;
      else
        ledState2 = LOW;

      digitalWrite(campin, ledState2);
    }
  }

  if(currentMillis - previousMillis > interval) { // 2nd cycle of LOW, LOW on pin 13 crankpin 
    if (i == 26) {
      Serial.println(i);
      previousMillis = currentMillis;  
      ///need some delay to keep cycles even
      i = 1;
    }
  }

}

Here is the latest code. I have the Hz serial print working in what looks to be a correct way. My Fluke meter says that I have frequency capability of 24Hz to 177Hz. My serial monitor shows 24 to 200. I can not run the loop faster than 200 at this point however. I need it to run up to 288Hz. I may have to use the new Fast write library that is on this forum.

/*

 800rpm = 13.33Hz = 75ms per RPM/26 = 2.88ms per pulse set... time between pulse state change is 1.44 ms
 17,000rpm =283.33Hz = 3.529ms per RPM/26 = .1357ms per pulse set... time between pulse state change is .06787ms
 
 
 */
int sensorPin = A0;              // select the input pin for the potentiometer
int sensorValue = 0;            // variable to store the value coming from the sensor
unsigned long val = 0;                    //value to be used with map
const int crankpin =  13;      // the number of the LED pin //crank pulse
const int campin = 12;         // the number of the LED pin  // cam pulse
int ledState = LOW;            // ledState used to set the LED
int ledState2 = LOW;           // ledState used to set the LED
unsigned long previousMillis = 0;      // will store last time LED was updated
unsigned long interval = 500;          // interval at which to blink (milliseconds
unsigned long time = 0;
unsigned long previoustime = 0;      // will store last time Time was updated
float Hz = 0;
int i = 1;


void setup() {
  Serial.begin(115200);
  // set the digital pin as output:
  pinMode(crankpin, OUTPUT);  
  pinMode(campin, OUTPUT);  
}

void loop()
{
  sensorValue = analogRead(sensorPin);
  val = map(sensorValue, 0, 1023, 50, 1440);   // interval adjuster from .065ms to 1.44ms
  interval = val;
  unsigned long currentMillis = micros();

  

  if(currentMillis - previousMillis > interval) { // 24 cycles of HIGH to LOW on pin 13 crankpin
    if( i < 24){  
      i = i++;
      if (i == 2){
    time = (currentMillis - previoustime); 
    Hz = (1000 / (time/1000));  
    Serial.println(Hz);                 //print current interval
     
    previoustime = currentMillis;

  }
      previousMillis = currentMillis;   

      if (ledState == LOW)
        ledState = HIGH;
      else
        ledState = LOW;
      digitalWrite(crankpin, ledState);
    }
  }

  if(currentMillis - previousMillis > interval) {  // 1st cycle of LOW, LOW on pin 13 crankpin
    if(i == 24 || i == 25){                        // cycle High, Low pin 12 cam pin
      i = i++;
      previousMillis = currentMillis;   
      if (ledState2 == LOW)
        ledState2 = HIGH;
      else
        ledState2 = LOW;

      digitalWrite(campin, ledState2);
    }
  }

  if(currentMillis - previousMillis > interval) { // 2nd cycle of LOW, LOW on pin 13 crankpin 
    if (i == 26) {
      previousMillis = currentMillis;  
      ///need some delay to keep cycles even
      i = 1;
    }
  }

}

My first comment is that all your times are held as micros so the variable names are misleading.

Secondly, I would expect the logic to detect when one interval has elapsed to occur once, and then use some sort of 'tooth count' to work out whether your output should be high or low.

Thirdly, I'm unclear from your description whether you are trying to emulate a 'missing tooth' signal. For example a '25 -1' missing tooth signal would have [u]three[/u] consecutive 'lows' at the missing tooth, not two.

Fourthly, I suggest that whenever you have any conditional code (if, else, for, while, do etc) you always put a compound statement i.e. { and } pair, even when you only have a single statement in the conditional block. It is so easy to inadvertently tack an extra statement on, or use a macro that silently expands to more than one statement, and suddenly the logic you see doesn't match what actually happens.

val = map(sensorValue, 0, 1023, 50, 1440); // interval adjuster from .065ms to 1.44ms

As input and output range are almost equal in size you will get no nice transformation by the map function. The potmeter/ADC will fluctuate in its readings so that might give some unwanted effects. YOu can see this if you try to print interval every cycle…

refactored your code a bit :wink:

/*
 * FILE:
 * AUTHOR:
 * DATE:
 * PURPOSE:
 * VERSION:
 * HISTORY:
 *
 * NOTES
 *
 * 800rpm = 13.33Hz = 75ms per RPM/26 = 2.88ms per pulse set... time between pulse state change is 1.44 ms
 * 17,000rpm =283.33Hz = 3.529ms per RPM/26 = .1357ms per pulse set... time between pulse state change is .06787ms
 *
 */
 
int sensorPin = A0;              // select the input pin for the potentiometer
int sensorValue = 0;            // variable to store the value coming from the sensor

unsigned long val = 0;                    //value to be used with map
const int crankpin =  13;      // the number of the LED pin //crank pulse
const int campin = 12;         // the number of the LED pin  // cam pulse

int ledState = LOW;            // ledState used to set the LED
int ledState2 = LOW;           // ledState used to set the LED

unsigned long previousMicros = 0;      // will store last time LED was updated
unsigned long interval = 500;          // interval at which to blink (milliseconds
unsigned long time = 0;
unsigned long previousTime = 0;      // will store last time Time was updated
float Hz = 0;
int i = 1;


void setup()
{
  Serial.begin(115200);
  // Serial.println("Start...");
  // set the digital pin as output:
  pinMode(crankpin, OUTPUT);  
  pinMode(campin, OUTPUT);  
}

void loop()
{
  sensorValue = analogRead(sensorPin);
  interval = map(sensorValue, 0, 1023, 65, 1440);        // interval adjuster from .065ms to 1.44ms
  
  unsigned long currentMicros = micros();

  time = currentMicros - previousMicros; 
  if (time > interval)                             // 24 cycles of HIGH to LOW on pin 13 crankpin
  {
    previousMicros = currentMicros;
    i++;

    if (i == 2)
    {
      time = currentMicros - previousTime;
      previousTime = currentMicros;
      Hz = 1000000.0 / time;
      Serial.println(Hz, 2);                     // 2 digits
    }

    if (i < 24)
    { 
      ledState = !ledState;                      //  1 -> 0 -> 1 -> 0 etc  
      digitalWrite(crankpin, ledState);
    }

    if (i == 24 || i == 25)
    {     
      ledState2 == !ledState2;
      digitalWrite(campin, ledState2);
    }

    if (i == 26) 
    {
      /// need some delay to keep cycles even
      i = 1;
    }
  }
}

Note you could merge the cases i == 2 and i == 26

PeterH: 1) My first comment is that all your times are held as micros so the variable names are misleading.

2) Secondly, I would expect the logic to detect when one interval has elapsed to occur once, and then use some sort of 'tooth count' to work out whether your output should be high or low.

3) Thirdly, I'm unclear from your description whether you are trying to emulate a 'missing tooth' signal. For example a '25 -1' missing tooth signal would have [u]three[/u] consecutive 'lows' at the missing tooth, not two.

4) Fourthly, I suggest that whenever you have any conditional code (if, else, for, while, do etc) you always put a compound statement i.e. { and } pair, even when you only have a single statement in the conditional block. It is so easy to inadvertently tack an extra statement on, or use a macro that silently expands to more than one statement, and suddenly the logic you see doesn't match what actually happens.

Thank you for your interest and tips. PeterH.

1) Yes, I should have corrected my variable names. I started writing the program using millis() until I realized that I had to have micros(). I did not correct it after that.

2) You may be correct.

3) Yes, I am trying to emulate a 'missing tooth' signal. I thought it was '26 -2' but, I got some new information that it is '24 -2'. Based on your advice about a '25-1' wheel I think I need 5 consecutive 'lows' for my wheel at the missing tooth. Thanks for that tip.

4) I will have to look my code over more to see where the { } error is but, I will try to fix it.

robtillaart:

val = map(sensorValue, 0, 1023, 50, 1440); // interval adjuster from .065ms to 1.44ms

As input and output range are almost equal in size you will get no nice transformation by the map function. The potmeter/ADC will fluctuate in its readings so that might give some unwanted effects. YOu can see this if you try to print interval every cycle....

You are correct about the results of the map function that I wrote. It was not very linear and I did not like the way it was working.

Also, thank you for reworking my sketch! I was struggling to solve my Hz display. I am going to try you method right away.

As I said in the above post, I will have to correct the sketch from 26 cycles to 24. But that is easy enough.

Thank you for you help, I will keep working at it.

Here is my latest code with many improvements implemented from the suggestions given. I had to double the numbers in the “if” statements to get my ‘24-2’ wheel result because using the shorter code that Robtillart gave me cut everything in half. I am using a Parallax USB Oscilloscope to check the pulses and the crankpin and campin timing look very good.

My Hz display now works flawlessly but, the fastest my Uno can run is 106 cycles per second I am calling that Hz. I need to run at 288. My next version will have the new fastwrite library in it.

/*
 * FILE:Crank Pulse Simulator
 * AUTHOR:Mark M. with help from Robtillart
 * DATE:1/22/12
 * PURPOSE: Simulate the output of a '24-2' crank encoder wheel and camshaft timing pin
 * VERSION:1 
 * HISTORY:
 *
 * NOTES
 *
 * 800rpm = 13.33Hz = 75ms per RPM/26 = 2.88ms per pulse set... time between pulse state change is 1.44 ms
 * 17,000rpm =283.33Hz = 3.529ms per RPM/26 = .1357ms per pulse set... time between pulse state change is .06787ms
 *
 */

int sensorPin = A0;              // select the input pin for the potentiometer
int sensorValue = 0;            // variable to store the value coming from the sensor

unsigned long val = 0;                    //value to be used with map
const int crankpin =  13;      // the number of the LED pin //crank pulse
const int campin = 12;         // the number of the LED pin  // cam pulse

int ledState = LOW;            // ledState used to set the LED
int ledState2 = LOW;           // ledState used to set the LED

unsigned long previousMicros = 0;      // will store last time LED was updated
unsigned long interval = 500;          // interval at which to blink (milliseconds
unsigned long time = 0;
unsigned long previousTime = 0;      // will store last time Time was updated
float Hz = 0;
int i = 0;


void setup()
{
  Serial.begin(115200);
  Serial.println("Start...");
  // set the digital pin as output:
  pinMode(crankpin, OUTPUT);  
  pinMode(campin, OUTPUT);  
  delay(1000);
}

void loop()
{
  sensorValue = analogRead(sensorPin);
  interval = map(sensorValue, 0, 1023, 65, 1595);        // interval adjuster from .065ms to 1.44ms

  unsigned long currentMicros = micros();

  time = currentMicros - previousMicros; 
  if (time > interval)                             // 22 cycles of HIGH to LOW on pin 13 crankpin
  {
    previousMicros = currentMicros;
    i++;
    if (i <= 44)
    { 
      ledState = !ledState;                      //  1 -> 0 -> 1 -> 0 etc  
      digitalWrite(crankpin, ledState);
    }

    if (i == 45 || i == 46)
    {                                        //first of two cycles of LOW, LOW on crankpin
      ledState2 = !ledState2;
      digitalWrite(campin, ledState2);
    
    }

    if (i == 47) 
    {                                       //second of two cycles of LOW, LOW on crankpin
      time = currentMicros - previousTime;        
      previousTime = currentMicros;                  
      Hz = 1000000.0 / time;
      Serial.println(Hz, 2);                     // 2 digits

      /// need some delay to keep cycles even

    }

    if (i == 47 || i == 48) 
    {
      /// need some delay to keep cycles even
      i = 0;
    }

  }
}

Here is my next revision of my code. I increased my loop cycles per second from 106 to 120 by using the new DigitalPin.h library and commenting out the Serial.Print. DigitalPin.h library gave me a 13% speed increase which is impressive but, sadly I need 288 loop cycles per second so, I may be at the limit of the 16MHz Arduino. :disappointed_relieved:

If anyone has a suggestions to increase my speed I am ready to hear it. I have enjoyed writing this code even though I have not reached my goal.

/*
 * FILE:Crank Pulse3
 * AUTHOR:Mark M. with help from Robtillart
 * DATE:1/22/12
 * PURPOSE: Simulate the output of a '24-2' crank encoder wheel and camshaft timing pin
 * VERSION:2 
 * HISTORY:
 *Old version would run up to 106 cycles per second
 *This version runs up to 120 cycles per second because faster Digital.Write and not using serail.
 *
 * NOTES
 * changing from standard digital.Write to DigitalPin.h methods
 *commented out Serial.print to maximize speed of loop
 *
 * 800rpm = 13.33Hz = 75ms per RPM/26 = 2.88ms per pulse set... time between pulse state change is 1.44 ms
 * 17,000rpm =283.33Hz = 3.529ms per RPM/26 = .1357ms per pulse set... time between pulse state change is .06787ms
 *
 */
#include <DigitalPin.h>

int sensorPin = A0;              // select the input pin for the potentiometer
int sensorValue = 0;            // variable to store the value coming from the sensor

DigitalPin<13> pin13;   // the number of the LED pin //crank pulse
DigitalPin<12> pin12;   // the number of the LED pin  // cam pulse
//const int crankpin =  13;      // the number of the LED pin //crank pulse
//const int campin = 12;         // the number of the LED pin  // cam pulse

int ledState = LOW;            // ledState used to set the LED
int ledState2 = LOW;           // ledState used to set the LED

unsigned long previousMicros = 0;      // will store last time LED was updated
unsigned long interval = 500;          // interval at which to blink (milliseconds
unsigned long time = 0;
unsigned long previousTime = 0;      // will store last time Time was updated
float Hz = 0;
int i = 0;


void setup()
{
  Serial.begin(115200);
  Serial.println("Start...");
  // set the digital pin as output:
  //pinMode(crankpin, OUTPUT);  
  //pinMode(campin, OUTPUT);  
  pin13.outputMode();
  pin12.outputMode();
  delay(1000);
}

void loop()
{
  sensorValue = analogRead(sensorPin);
  interval = map(sensorValue, 0, 1023, 65, 1595);        // interval adjuster from .065ms to 1.44ms

  unsigned long currentMicros = micros();

  time = currentMicros - previousMicros; 
  if (time > interval)                             // 22 cycles of HIGH to LOW on pin 13 crankpin
  {
    previousMicros = currentMicros;
    i++;
    if (i <= 44)
    { 
      ledState = !ledState;                      //  1 -> 0 -> 1 -> 0 etc 
      if (ledState == LOW) {
        pin13.low();
      } 
      else {
        pin13.high(); 
      } 

    }


    if (i == 45 || i == 46)
    {                                        //first of two cycles of LOW, LOW on crankpin
      ledState2 = !ledState2;
      if (ledState2 == LOW) {
        pin12.low();
      } 
      else {
        pin12.high(); 
      } 

    }


    if (i == 47) 
    {                                       //second of two cycles of LOW, LOW on crankpin
      time = currentMicros - previousTime;        
      previousTime = currentMicros;                  
      Hz = 1000000.0 / time;
      //Serial.println(Hz, 2);                     // 2 digits

      /// need some delay to keep cycles even

    }

    if (i == 47 || i == 48) 
    {
      /// need some delay to keep cycles even
      i = 0;
    }

  }
}

Here is a sketch I made using DigitalPin.h library and delayMicroseconds() to get my pulse interval correct. It works well except the Analog.read takes too long it seems to be 130us seconds and makes an uneven gap during the read. The map function takes about 70us. Maybe I can somehow relocate there spot in the loop and still use them. I will have to think about it.

// scope test for write timing
#include <DigitalPin.h>

// class with compile time pin number
DigitalPin<13> pin13;
DigitalPin<12> pin12;
int time = 0;
int sensorPin = A0;              // select the input pin for the potentiometer
int sensorValue = 0;            // variable to store the value coming from the sensor

void setup() {
  // set mode to OUTPUT
  pin13.outputMode();
  pin12.outputMode();
}
void loop() {
   sensorValue = analogRead(sensorPin);
  time = map(sensorValue, 0, 1023, 65, 1595);        // interval adjuster from .065ms to 1.44ms
  pin13.high();  
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();  
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();  
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();  
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();  
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();  
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();  
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();  
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();  
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();  
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();  
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();
  pin12.high();
  delayMicroseconds(time);
  pin13.low();
  pin12.low();
  delayMicroseconds(time);
  pin13.low();  
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  
  
}

This sketch provides even spacing but, only with 65us delays. If the delay is reduced spacing will not be equal.

// scope test for write timing
#include <DigitalPin.h>

// class with compile time pin number
DigitalPin<13> pin13;
DigitalPin<12> pin12;
int time = 0;
int sensorPin = A0;              // select the input pin for the potentiometer
int sensorValue = 0;            // variable to store the value coming from the sensor

void setup() {
  // set mode to OUTPUT
  pin13.outputMode();
  pin12.outputMode();
}
void loop() {
  
  pin13.high();  
  delayMicroseconds(time);
  pin13.low();
   delayMicroseconds(time);
  pin13.high();
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();  
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();  
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();  
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();  
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();  
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();  
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();  
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();  
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();  
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();  
  delayMicroseconds(time);
  pin13.low();
  delayMicroseconds(time);
  pin13.high();
  pin12.high();
  delayMicroseconds(time);
  pin13.low();
  pin12.low();
  delayMicroseconds(time);
  pin13.low();  
  sensorValue = analogRead(sensorPin);
  //delayMicroseconds(time - 65);
  pin13.low();
  //delayMicroseconds(time - 65);
  pin13.low();
  time = map(sensorValue, 0, 1023, 65, 1595);        // interval adjuster from .065ms to 1.44ms
  //delayMicroseconds(time - 65);
  pin13.low();
  delayMicroseconds(time);
    
}

possible project for simulated warm analog simulated sensor maghetic RPM old CAR