Measure the timing between pulses and if the time between pulses differ then.

Hello

I hope someone can help me with this.

Pulses on pin AI0, value > 500 is considered high, measure the timing between pulses and if the time between pulses differ pin 13 is low. If time between pulses are same, pin 13 pulse 8 times with same pulse frequency as the input from AI0, after the 8 pulses the pin 13 go to low again. If then time between pulses on Ai0 differ again the loop starts over again.

Regards
Andreas

500 what? Seconds, ms or us?

Which Arduino has 11 analog pins?

500, as in 0 to 1023.

Where does 11 analog pins come from? I only see A0 mentioned (as AI0, just a little funny).

Hello

Correct 0-1023 value should be > 500

Ai0

CrossRoads:
500, as in 0 to 1023.

Where does 11 analog pins come from? I only see A0 mentioned (as AI0, just a little funny).

Oh, the l and 1 look a lot the same on my monitor so I read A10.

What is Ai0?

The time between how many pulses is the same? Two consecutive?

Analog input 0

andreasst:
Analog input 0

Why not call it, and everything else that you name, by the name that is printed on the board and used in all the code and documentation? Would that not be clearer?

With respect to your original question, within what tolerance must the pulses be measured? Because, there may be noise in the original signal and there is some inherent noise in the A/D that will skew the measurement of the interval between pulses. Also your frequency range is open ended, as you are currently defining it. So it’s possible that a pulse that is 235234 us long would be considered different from one that is 235233 us long. Obviously that would exceed the noise thresholds and the pulse train would be triggered rarely, and essentially by accident. :slight_smile:

You probably may want to code something like this:

unsigned long oldtime = 0, deltatime = 0;
uint8_t Ai0 = 0;
void setup() {
  // put your setup code here, to run once:
  pinMode(13, LOW);
  digitalWrite(13, LOW);
}

void loop() {
  uint16_t val;
  // put your main code here, to run repeatedly:
  val = analogRead(0);    // read A0

  if (Ai0 == 1) {
    deltatime = micros() - oldtime;

    for (uint8_t i = 0; i < 16; ++i) { //8 HIGH pulse and 8 LOW pulse
      if (i % 2 == 0) digitalWrite(13, HIGH);
      else digitalWrite(13, HIGH);
      delayMicroseconds(deltatime);
    }

    Ai0 = 0;
  }
  else if (val > 500) {
    oldtime = micros();
    Ai0 = 1;
  }
  else {
    Ai0 = 0;
  }
}

I don’t know how time critical those ouput pulses need to be(digitalwrite adds a few microsecond to the delay) or how often you want to check AI0 but that should give you a starting point

Hello

const int BPM = 0 // The analog input on Ai0 comes from an opt coupler metronome on/off pulse and should not have any noise, so if I scale the input 0-1023 and threshold on >500, should be good.
Const int LED13 = 13
Const int LED12 = 12

Then measure the time between the BPM in ms with deadband of a few ms.
If the time is in the deadband limit the counter should counts to 8 and resets and wait for a change of time.
When counter reaches 4 the LED13 should go high and low with same frequency as BPM 4 times until counter reach 8.
Else LED13 should be low.

LED12 should be inverted always to LED13.

Thanks Sherzaad,

Taking your time for the code, i if you check the latest simpe description, this should be great if you have time to check it.

Regards
Andreas

andreasst:
Thanks Sherzaad,

Taking your time for the code, i if you check the latest simpe description, this should be great if you have time to check it.

Regards
Andreas

hello Andreasst,

I'm sorry but I'm finding it difficult to understand the description you provided.

If you could post a timing diagram or could make one to illustrate what you mean and what you expect as output, that would help me help you! :slight_smile:

Thanks i will make an diagram :slight_smile:

Hello

This is what i try to get out from the Arduino :slight_smile:

the picture does make it clearer what u want to do.

u can try and get that output by tweaking the code I posted. might be a bit tricky since your your pulse duration (ON time) and OFF time are not the same meaning both durations need to be measured (unless the ON time is fixed)

then whenever the periods match u just increment a counter. when that come to 4 you then output using the for loop with some adjustments.

Have a go at coding that in and post in here with your results!

good luck! :slight_smile:

Hello

I belive that might be the perfect world, and i guess a bit over engineering, to measure both.

I have now changed my configuration on the input and use digital input, and my code is as follow below.

If i count int val time between when its go to HIGH from HIGH and compare the time 4 times
The time in ms saved as
int Time1
int Time2
Int Time3
Int time4
int Counter
int val1

if all int Times they are same then it will activate the outputs same as input pulse 4 times, if not wait until all 4 int times are the same value.

My code for now,

int ledPin13 = 13; // Summer / Led BPM
int ledPin12 = 12;// XR18
int ledPin11 = 11; // Spare
int ledPin10 = 10;// Spare
int ledPin9 = 9; // Boss1
int ledPin8 = 8;// Boss2

int inPin = 2; // Input from Metronome
int val = 0; // variable for reading the pin status

void setup() {
pinMode(ledPin13, OUTPUT);
pinMode(ledPin12, OUTPUT);
pinMode(ledPin11, OUTPUT);
pinMode(ledPin10, OUTPUT);
pinMode(ledPin9, OUTPUT);
pinMode(ledPin8, OUTPUT);
pinMode(inPin, INPUT);
}

void loop(){

val = digitalRead(inPin); // read input value
if (val == HIGH) {
digitalWrite(ledPin13, HIGH);
digitalWrite(ledPin12, HIGH);
digitalWrite(ledPin11, HIGH);
digitalWrite(ledPin10, HIGH);
digitalWrite(ledPin9, LOW);
digitalWrite(ledPin8, LOW);
} else {
digitalWrite(ledPin13, LOW);
digitalWrite(ledPin12, LOW);
digitalWrite(ledPin11, LOW);
digitalWrite(ledPin10, LOW);
digitalWrite(ledPin9, HIGH);
digitalWrite(ledPin8, HIGH);

}
}