Trouble counting - recording pulses for analog device

Hello! First off, I only half know what I'm doing. I spend most of my time tinkering with audio circuits and every now then have an itch to take out the nano. I've been picking at a project that sends an imitation dual inverse clock signal to an analog delay device - in accordance to the devices datasheet. Or close enough :grin:. This code is successfully driving the device and showing a status LED when the analog comparator is triggered.

The way the analog delay device works is with each clock click the input signal (a 5v pulse in this case) is passed through a delay line and after so many stages it shows up at the output. I'm trying to report how many clicks it takes for a pulse to travel through the device.

I've been having a lot of trouble tracking the clock count and only displaying it when the comparator is initially triggered. I've taken out my amateur attempts to get it working and hope some fine folks here could point me in the right direction. I've been trying to show the total/end stage count through Serial.println(). My attempts would get results in the ballpark but was quite a mess so I'll save you from that.

volatile boolean triggered;
boolean clockState = true;
int CP1 = 9;
int CP2 = 10;
int clockCount = 0;
int statusLED = 15;
int pulseOut = 2;

  triggered = true;

void setup() {
  pinMode(CP1, OUTPUT);
  pinMode(CP2, OUTPUT);
  pinMode(pulseOut, OUTPUT);
  pinMode(statusLED, OUTPUT);
  // analog comparator interrupt, set voltage at pin 7, input at pin 6
  ADCSRB = 0;
  ACSR =  bit (ACI)
          | bit (ACIE)
          | bit (ACIS1);

void loop() {
  //clock pulse
  clockState = !clockState;
  digitalWrite(CP1, clockState);
  digitalWrite(CP2, !clockState);
  if (triggered) {
    digitalWrite(pulseOut, LOW);
    digitalWrite(statusLED, HIGH);
    triggered = false;
  } else {
    digitalWrite(statusLED, LOW);
    digitalWrite(pulseOut, HIGH);

I hope everyone is doing well!

You implemented a positive feedback, i.e. the next pulse starts immediately when the preceding pulse has rippled through the external device. That's a bit different from the approach for e.g. US sensors, where a pulse is sent (ping) and the time is recorded until its echo returns.

In your case you have to measure the time between the interrupts, i.e. in the if(triggered) true block. There you compute the time difference between the current time and last occurence, then set last occurence to the current time.

Thank you for your insight.

I'm having trouble understanding how logging the trigger time will help as it would update each time triggered is true in the loop. It would be redundant without knowing how to utilize the data to have better control of the parameters - which has been the struggle.

Please allow me to elaborate on what's been going on with my attempts. X is in reference to the clockCount or total stage count of the device that I'm trying to determine;
The best results I've had was logging the clockCount until triggered is true - where it would then set pulseOut LOW, report the clockCount, reset clockCount to 0 and repeat however many times until triggered is false (I don't need to repeat the statement but I'm trying to keep pulseOut low until triggered is false again).

Triggered is stuck true because once the pulseOut is LOW the device is still triggering the comparator for X amount of stages so I'll get 1 or 2 for X amounts of times while triggered is true. I believe this is also mucking things up as the when triggered is initially true it's reporting clockCount as higher than what would be expected (this is the "ballpark" data I have observed) I'm trying to figure out what X is efficiently (how many stages are in the device). I'm not necessarily looking for timing data to achieve this.

You implemented a positive feedback, i.e. the next pulse starts immediately when the preceding pulse has rippled through the external device.

Rereading what you've said - I don't believe that's right. The pulseOut is only HIGH when the trigger is false.

Let use 1024 stage devices for example with the code as is. The clockCount will go up for each click while there's nothing at the device's output. We send out a pulse to the input, start the clockCount and 1024 clicks later there's the signal! That means the status LED is off for ~1024 stages and then on for the same amount as the pulse was high for each click - leaving ~1024 HIGH stages. 1024 HIGH, 1024 LOW. Or at least it's behaving close to what was expected.

Any other thoughts?

Check the if(triggered) statement yourself, pulseOut is LOW only for one iteration of loop().

Check the if(triggered) statement yourself, pulseOut is LOW only for one iteration of loop().

And it would stay low for each loop until triggered is false.... unless I’m misunderstanding something?

I’ve just explained the results of my code so far. I’m only being further confused by your feedback, I apologize.

Elaborate if possible.

In your code triggered is set false immediately, once it was found true. This also terminates the output pulse in the next invocation of loop().

Hello. I now understand what you're getting at. Was over my head for a while as I was hoping for a detailed explanation. The difficulty now is figuring out how long it takes for my codes to execute.

Does the arduino timer freeze while executing delays and interrupts?

The millis() timer stops in an ISR, because it is incremented by another; now blocked; interrupt.
But delay() is based on proper timer operation, else it would never end.