Analog clock to tap tempo. Help dereive rate for led blinking

Hi. I have been working on cloning the simplest functionality of the Disaster Area Designs Smart Clock pedal. It basically reads an incoming analog clock signal from a drum machine for instance and outputs a signal for tap tempo into delay pedals for instance.

This i have been able to do, but I need to some small adjustments. So here is what my attached script is supposed to do, but almost does:

read an analog clock sync signal, blink an led (and later output to a pin) 4 times in time with 1/24 of the incoming clock signal. After blinking 4 times it stops blinking. If the input has been stopped for more than a second, the counter for blinking 4 times should be reset, so that when i input a new clock rate (new song, new tempo) the led should blink 4 times again, and this time in time with the new tempo.

I have almost all of this working - execpt for the led blinking in time with the input signal divided by 24.

It would be great if someone would have a look at the part in the code about blinking the led and help make that relate to the input rate at any time.

Hope someone can help out:-)

const int analogClockPin = 2;  // Pin to which the analog clock signal is connected
const int onboardLEDPin = 13; // Onboard LED pin
int pulseCount = 0;           // Counter for the received pulses
int blinkCount = 0;           // Counter for the LED blinks
int lastSignalState = LOW;    // Variable to store the last signal state
unsigned long lastChangeTime = 0; // Time of the last change in signal state
const unsigned long pauseDuration = 1000; // Pause duration in milliseconds (1 second)

void setup() {
  pinMode(analogClockPin, INPUT);
  pinMode(onboardLEDPin, OUTPUT);
}

void loop() {
  // Read the analog clock signal
  int pulse = digitalRead(analogClockPin);

  // If the signal state has changed, update the last change time and last signal state
  if (pulse != lastSignalState) {
    lastChangeTime = millis();
    lastSignalState = pulse;
  }

  // If a pulse is detected, increment the pulse count
  if (pulse == HIGH) {
    pulseCount++;

    // Check if 24 pulses have been received
    if (pulseCount == 24) {
      // Blink the onboard LED
      digitalWrite(onboardLEDPin, HIGH);
      delay(100); // Adjust the delay as needed for the blink duration
      digitalWrite(onboardLEDPin, LOW);

      // Reset the pulse count
      pulseCount = 0;

      // Increment the blink count
      blinkCount++;

      // Check if 4 consecutive blinks have occurred
      if (blinkCount == 4) {
        // Pause for 1 second
        unsigned long elapsedTime = millis() - lastChangeTime;
        while (elapsedTime < pauseDuration) {
          pulse = digitalRead(analogClockPin); // Read the signal during the pause
          if (pulse != lastSignalState) {
            lastChangeTime = millis();
            lastSignalState = pulse;
          }
          delay(10); // Adjust the delay based on the clock signal frequency
          elapsedTime = millis() - lastChangeTime;
        }

        // Reset counters
        pulseCount = 0;
        blinkCount = 0;
      }
    }
  }

  delay(10); // Adjust the delay based on the clock signal frequency
}

type or paste code here

the code is confusing

  • what prevents the code for pulse == HIGH being called repeatedly for just one pulse while it is HIGH and the blinkCount == 4 condition being invoked after 4 occurrence
  • shouldn't there be a delay after the LED is set LOW
  • what happens inside the elapsedTIme < pauseDuration) besides changing the elapsedTime
  • ???
  • ?

shouldn't each iteration of loop() detect a pulse edge (change in level) and measure the duration of the pulse

and when the 24th pulse occurs, enable the processing to blink the LEDs using millis() to time the LED on/off periods without blocking and after the 4 (?) LED flashes reset the processing

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.