Help with delays, or maybe interupts

JoePage2008x2

New Member
Ive been wondering for a while how do you do this type of delay, i know its something to do with interupt. Like if i sent 2 fast pulses into the input of a picaxe chip, i would want the chip to delay the 2 pulses by a variable. say w0 = 1234. i would input two fast pulses then wait 1234ms then output the two fast pulses. Any help please would be appreciated, certainly a bit of code
 

Jeremy Leach

Senior Member
Joe, it's good to solve a problem by breaking it down into it's different elements. Also it's important to specify your requirements as clearly as possible.

Reading this I assume that you are wanting the picaxe to output an exact replica of the two incoming pulses (ie the pulse widths are the same, the time between the pulses are the same) but delay this output by a variable time delay.

So there are two parts to this problem:

1. Picaxe 'recording' received pulses
How can a picaxe detect a pulse? Well, it depends on how fast you mean by fast pulses. There are two options I can see:
a) the pulsin command that can be used to detect relatively fast pulses. Or
b) you can just monitor the input pin using code and wait for the digital state of the input to change.

There are potential issues with both options, and it hinges on your requirements. For instance, if the pulses are too fast then b) might not work because the code might not be quick enough to detect the transitions. But with a) there might be a problem with measuring the time between pulses.

Also, there are diffrent ways of recording the pulses. Pulsin measures the pulsewidth of an incoming pulse, so if you went this route you would be measuring the widths of the pulses and the gap between the pulses.

You could do the same with approach b). However I can also see another option, and that's to take a stream of regular samples of the input pin.


2. Picaxe 'playing back' a delayed replica of the pulses
Well the first thing is to delay. What command does the picaxe use to delay?

Then the playback depends on what solution you used in 1.


I'm not going to give code because I think you need to look at the different elements, clarify your requirements, looking at the different design options, chew over the pro's and con's of each option.

Learning how to approach problems is a valuable skill ;)
 
Last edited:

JoePage2008x2

New Member
Thanks for replying, i explained it a little wrong

I didnt explain it too well, you've got most of what i thought right but i forgot to put some extra, say if they were random pulses going into the chip, like from every 5ms to 500ms random, i would want these pulses delayed by the value of w0. so say if w0 = 1000, and they were random pulses going into the chip, so all of these pulses are delayed for 1sec, i do know its something to do with interupt, but im not too good at some parts of coding. A bit of code showing and explaining how to do interupts would be very appreciated.
 

leftyretro

New Member
Joe;

Doesn't sound like too hard a task but need to know a little more. I understand about the delaying before outputing the input pulse but is it important that the delayed output pulse be the same width as the input pulse or can it be just a short fixed width sent out after the delay? Also you need to define if the input pulse is first low then going high or first high going low?

Lefty
 

JoePage2008x2

New Member
Trying to explain it

Say that the pulses start off low, go high for 1ms then low, the delay between pulses is random, this randomly inputs into a picaxe chip. Then these pulses are delayed by the value of w0, then ouputed by the chip.

These pulses can randomly occur at any time, but every pulse must be delayed by the value of w0 and then every output must outputed the same as they inputed, but must be delayed by w0 through the chip.
 

Mycroft2152

Senior Member
You say random pulses, but are more pulses arriving during the w0 millisecond delay. The issue is keeping track of the number of pulses and time between each pulse while waiting for the first pulse to be outputted.

If there is one pulse, delay, output, then receive next pulse, it is trivial to do.

Myc
 

hippy

Ex-Staff (retired)
So what you're hoping for is ...

Code:
         A     B           C        D
         _     _           _        _
In  : __| |___| |_________| |______| |____
        :
        :            _     _           _        _
Out : __:___________| |___| |_________| |______| |____
        :           :
        |<- Delay ->|
Two problems there; Pulse B occurring while delaying for outputting A, and C occurring at the same time as outputting delayed B.

I cannot think of any easy way to handle that with a single PICAXE. You could defer handling interrupts while outputting but then you could miss input pulse C entirely, you could allow interrupts and catch C but you'd have to delay the output of B or end up with it stretched.

It gets even more complicated if you have varying pulse widths you need to replicate.

At the very least you'll need some FIFO ( first-in, first out ) buffering to queue up how long a pulse was since the last and how long the pulse lasts and something to fill the queue and something to empty it, both running entirely asynchronously and independent of each other.

It is possible to do this by sampling and output on every tick. In pseudo-code -

Code:
samplePtr = 0
outputPtr = 0
delay     = N
Do
  Gosub Sample
  Gosub Output
Loop

Sample:
  If input is low Then
    If input was high Then
      samplePtr = samplePtr + 1
      If samplePtr > MAX_SAMPLES Then
        samplePtr = 0
      End If
      offPeriod[samplePtr] = 0
      onPeriod[samplePtr] = 0
    End If
    offPeriod[samplePtr] = offPeriod[samplePtr] + 1
  Else
    onPeriod[samplePtr] = onPeriod[samplePtr] + 1
  End If
  Return

Output:
  If delay > 0 Then
    delay = delay - 1
  Else
    If offPeriod[outputPtr] > 0 Then
      Low OUT_PIN
      offPerod[outputPtr] = offPeriod[outputPtr] - 1
    Else
      if onPeriod[outputPtr] > 0 Then
        High OUT_PIN
        onPerod[outputPtr] = onPeriod[outputPtr] - 1
        If onPeriod[outputPtr] = 0 Then
          outputPtr = outputPtr + 1
          If outputPtr > MAX_SAMPLES Then
            outputPtr = 0
          End If
        End If
      End If
    End If
  End If
  Return
There'd probably be a fair bit of tweaking required to make that work.
 
Last edited:

JoePage2008x2

New Member
this is what i mean

The first example that you give is just what i mean, can you think of any other cicuits that do this, i think i could do this with programing but i want to see if there is any other ways of doing this before i try and work it out. Thanks for the reply
 
Top