# Sorting digital signals

Hello all,

I have two digital pulses and would like to generate an output based on some boolean logic.

Pulse 1 contains some digital pulses.

Pulse 2 contains some of the digital pulses contained in Pulse 1.

Means Pulse 2 is a subset of Pulse 1.

Now i would like to generate an output for the pulses which are exclusive to pulse 1.

Here is what i have done so far, but it doesnt not work because Pulse 1 and Pulse 2 have some time lag.
See attached picture, i would like to generate a digital output for the small red pulse. Not for the overlapping red and yellow.

``````const int  PinA = 2;            // the pin that receives all digital pulses
const int  PinB = 3;           // the pin that receives subset of PinA pulses
const int  PinC = 5;

int PinAstate;
int PinBstate;

void setup() {
pinMode(PinA, INPUT);         // initialize the  pins as a input:
pinMode(PinB, INPUT);
pinMode(PinC, OUTPUT);    // initialize the Pin as an output:
}

void loop() {

if (PinBstate == LOW) {
if (PinAstate  == HIGH){
digitalWrite(PinC,HIGH);
delayMicroseconds(100);
digitalWrite(PinC,LOW);
}
}
}
``````

come guys, any pointers?

Netiquette states that you should wait 24 hours before kicking your post.

Give it some time to go around the world.

your code has pinA et pinB which one is pulse1 and pulse2 → use consistent naming

why do you do this?

``````     delayMicroseconds(100);
digitalWrite(PinC,LOW);
``````

if you want to mirror one of the signal, based on the truth value of the other (if I understood you correctly), why do you arbitrarily get to LOW after an arbitrary duration?

fill out this table: what do you want to see for PinC value based on the state of pinA and pinB?

PinC PinB LOW HIGH
PinA
LOW ? ?[/td][/tr]
HIGH ?[/td] ?[/td][/tr]
[/table]

bitoff_arduino: Pulse 1 contains some digital pulses.

Pulse 2 contains some of the digital pulses contained in Pulse 1.

Means Pulse 2 is a subset of Pulse 1.

Now i would like to generate an output for the pulses which are exclusive to pulse 1.

What does all that mean? Based on ordinary english Pulse 1 is a single pulse and cannot contain "some digital pulses"

Are you talking about a long stream of pulses and a subset of that stream and whenever the pattern in the subset re-occurs in the main stream you want something to happen?

Or what?

If you are talking about streams of pulses it would help if you provide a diagram showing what you mean. And tell us what is generating the stream of pulses and why you need to match parts of it. And what is the pulse frequency - is an Arduino fast enough?

...R

Do the waveforms consistently look like this repeating pattern?

If so I'd look for a way to discriminate the width of the narrow pulse. Like, A on for x millibleeps while B is low.

.02

To OP so you want feedback quick and when you get some you disappear?

Now i would like to generate an output for the pulses which are exclusive to pulse 1.

That's what we don't get really... filling up the small truth table above would help. You know the values of pinA and pinB if from that you can explain what 'exclusive' means?

Guesswork:

• In the picture yellow is a subset of red so pulse2 is yellow and pulse1 is red by OP definition
• Assuming PIN letters are in same order pinA would be red and pinB would be yellow.

Thus In the initial code the attempt was to say if yellow is low and red is high the output is HIGH so it seems OP wants all the red only when yellow is off.

--> The OP code would then need to set the output at whatever red is when yellow is low and low otherwise? Then the code would simply be

``````if (PinBstate == LOW) digitalWrite(PinC, PinAstate);
else digitalWrite(PinC, LOW);
``````

This could also be achieved by calculating the output with Boolean arithmetic (the truth table value)

J-M-L:
your code has pinA et pinB which one is pulse1 and pulse2 → use consistent naming

why do you do this?

``````     delayMicroseconds(100);
``````

digitalWrite(PinC,LOW);

``````

if you want to mirror one of the signal, based on the truth value of the other (if I understood you correctly), why do you arbitrarily get to LOW after an arbitrary duration?

fill out this table: what do you want to see for PinC value based on the state of pinA and pinB?

| __**PinC**__ **PinB** | LOW | HIGH |
| - | - | - |
| **PinA** | | |
| LOW | **?** | **?[/td][/tr]**<br><strong>HIGH __?[/td] **?[/td][/tr]**__</strong><br><strong>__**[/table]**__</strong><br><strong>__**[/quote]**__</strong><br><strong>__**I want PinC to be HIGH for the following:**__</strong><br><strong>__**if PIN A is HIGH and PIN B is LOW: PIN C HIGH**__</strong><br><strong>__**This doesnt work in my case because although PIN B is a subset of PIN A, they are not in sync.**__</strong> |``````

See #7

Robin2:
What does all that mean? Based on ordinary english Pulse 1 is a single pulse and cannot contain “some digital pulses”

Are you talking about a long stream of pulses and a subset of that stream and whenever the pattern in the subset re-occurs in the main stream you want something to happen?

Or what?

If you are talking about streams of pulses it would help if you provide a diagram showing what you mean.
And tell us what is generating the stream of pulses and why you need to match parts of it.
And what is the pulse frequency - is an Arduino fast enough?

…R

Yes its a digital signals of train of Pulses. Diagram is attached with the post, it is also posted above. Frequency is about 500 Hz. The two signals are generated by a camera trigger system.

From the diagram i only need an output when the small red pulse appears. I dont want any output when the red and yellow pulses appear/overlap.

J-M-L:
To OP so you want feedback quick and when you get some you disappear?
That's what we don't get really... filling up the small truth table above would help. You know the values of pinA and pinB if from that you can explain what 'exclusive' means?

Guesswork:

• In the picture yellow is a subset of red so pulse2 is yellow and pulse1 is red by OP definition
• Assuming PIN letters are in same order pinA would be red and pinB would be yellow.

Thus In the initial code the attempt was to say if yellow is low and red is high the output is HIGH so it seems OP wants all the red only when yellow is off.

--> The OP code would then need to set the output at whatever red is when yellow is low and low otherwise? Then the code would simply be

``````if (PinBstate == LOW) digitalWrite(PinC, PinAstate);
``````

else digitalWrite(PinC, LOW);

``````

This could also be achieved by calculating the output with Boolean arithmetic (the truth table value)
``````

I am sorry for this ambiguity. I posted at EU evening time, and its morning now.

Yes the yellow is a subset of red. But they are out of sync in time. Thats why they dont overlap perfectly.

From the figure, i want output only when a small red pulse appears. Not when the bigger (wider pulse width) red and yellow appear.

Now since the bigger pulses are out of sync, my code doesnt work.

I hope this picture makes it easy to understand.

Then you need a bit of a dynamic wait before deciding of the output to see if yellow arrives or not

A simple state machine will let you achieve this

States are rest, waiting for trigger, false event, active

You start at rest, when you receive a red HIGH you go to waiting for trigger, if yellow appears in a given timeout and red stayed high then you go to active otherwise you go to false event and wait until red goes back down to go to rest again.

bitoff_arduino: I hope this picture makes it easy to understand

Not really. Or not at all.

First let's deal with "not at all"

If I go back to your Original Post you said that P2 is a subset of P1. But in your diagram you seem to be treating P2 as a single pulse. A single pulse will always be a subset of a stream of pulses so the concept of subset doesn't make much sense.

If you are using a single pulse in P2 as a simplification then I don't think that makes sense. What works for one pulse won't translate to matching a train of pulses.

And now let's deal with "not really" - assuming that your description in the Original Post was wrong and you really only need to make decisions based on a single P2 pulse.

Are you saying that there should be an output if P2 is high and P1 is low? If so that should be easy to program.

...R

This is definitely a job for a state machine, there is state and time in this decision process as the yellow pulses do not completely cover the red pulses, so combination logic cannot do the job.

Use micros(), not delay or delayMicroseconds, if you want to be responsive to changing inputs at all times.

I suspect you need at least these states:

not in a pulse in a yellow pulse only (is this valid?) in a red pulse (for a short time only) in a red pulse for a long enough time without seeing the yellow pulse - output HIGH in a red + yellow pulse in a red pulse after a yellow went away

You need to record the time when you transition from not in a pulse to in a red pulse, and then check the elapsed micros for deciding when to transition to the output HIGH state.

Robin2:
Not really. Or not at all.

First let's deal with "not at all"

If I go back to your Original Post you said that P2 is a subset of P1. But in your diagram you seem to be treating P2 as a single pulse. A single pulse will always be a subset of a stream of pulses so the concept of subset doesn't make much sense.

If you are using a single pulse in P2 as a simplification then I don't think that makes sense. What works for one pulse won't translate to matching a train of pulses.

And now let's deal with "not really" - assuming that your description in the Original Post was wrong and you really only need to make decisions based on a single P2 pulse.

Are you saying that there should be an output if P2 is high and P1 is low? If so that should be easy to program.

...R

I want to have an output HIGH when the pulse is shorter, i.e. at 2,5,8. Else output LOW.

MarkT: This is definitely a job for a state machine, there is state and time in this decision process as the yellow pulses do not completely cover the red pulses, so combination logic cannot do the job.

Use micros(), not delay or delayMicroseconds, if you want to be responsive to changing inputs at all times.

I suspect you need at least these states:

not in a pulse in a yellow pulse only (is this valid?) in a red pulse (for a short time only) in a red pulse for a long enough time without seeing the yellow pulse - output HIGH in a red + yellow pulse in a red pulse after a yellow went away

You need to record the time when you transition from not in a pulse to in a red pulse, and then check the elapsed micros for deciding when to transition to the output HIGH state.

Thank you this is the closest to a solution, combination solution definitely does not work here as they do not overlap perfectly.

I have never done state machine, could you write a short example of it directed towards this problem. I would be so much thankful to you.

bitoff_arduino: Yes the yellow is a subset of red. But they are out of sync in time. Thats why they dont overlap perfectly.

This complicates things.

How large is the offset? Is it consistent? What is the maximum and minimum mark and space times of the pulse train?

Jiggy-Ninja: This complicates things.

How large is the offset? Is it consistent? What is the maximum and minimum mark and space times of the pulse train?

The offset is always consistent. Space time of pulses vary according to the experiment.

...and mark time of pulses?