arduino interrupt

hello friends

I hoppe, my english is good enoughe, to explain my problem, which is an old problem.

attachinterrupt(0, function, RISING)

function() { digitalWrite(3,HIGH); delay(int y); digitalWrite(3,LOW); }

this is just an exampel of my Problem. so, delay inside of an interrupt is forbiden.

what I want to do, my problem: I get in Pin2 an external Triggerimpulse, which is just some mikroseconds long, but it is just one pulse. while I get the external Trigger, my Arduino-Duemilanove, should generate an new Pulse with width y (this is my own int number for miliseconds).

when I get the external Trogerpulse, my Arduino should generate a pulse, just one, with the width of y,

I tried also with digitalRead==HIGH, but I get timing-Problems.

my question: how can I write this code a little bit different, that during my interupt I generate a pulse with y width????

attachinterrupt(0, function, RISING)

function() { digitalWrite(3,HIGH); delay(int y); digitalWrite(3,LOW); }

thank you very much plopi

Hello, plopi. Your English is fine, I understand your problem.

Here is a possible solution, but it depends on what else you are doing in your sketch.

Uncompiled, Untested

volatile boolean triggerReceived = false;

void setup()
{
  . . . // some other code here ?
  attachinterrupt(0, function, RISING)
  . . . // some other code here ?
}

function()
{
  triggerReceived = true;
}

void loop
{
  . . . // some other code here ?

  if (triggerReceived)
    {
    triggerReceived = false;
    digitalWrite(3,HIGH);
    delay(int y);
    digitalWrite(3,LOW);
    }

  . . . // some other code here ?

}

The interrupt routine sets a variable and immediately returns. So you do not have a delay of 'y' inside your interrupt routine. That is a good thing.

Each time through your loop, test the variable to see if the interrupt routine was triggered by the external trigger impulse. If it was, reset the variable and generate the pulse with the width of 'y'.

The pulse you generate on pin 3 will not start immediately, there will be a delay after receiving the trigger impulse, so that may not be a good thing. The length of this delay will depend on what else you are doing in your loop(). If the only thing you are doing in the entire sketch is extending the trigger impulse (microseconds in length) into a longer pulse (time 'y' in length), then you may be successful with this method.

There is a side-effect here: if a second trigger impulse arrives on pin 2 while your generated pulse is active (that is, while pin 3 is HIGH), it will not be handled until after the first generated pulse finishes, but at least it will be handled then. (In your original code, a second trigger impulse would be lost.) If a third trigger impulse arrives while your first generated pulse is active, it will be lost in this solution. You can change this logic so that you "stack" enough variables for as many trigger impulses as you reasonably expect.

Maybe this side-effect is not a problem for you in your real world. If it could happen, what do you want to do with the generated pulse?

thank you very much for your idea. I tried and it works, but not so good how I expected.

how can I post my code, that you can see and I can explain, I'm sorry but I'm new in this forum.

regards plopi

how can I post my code, that you can see and I can explain, I'm sorry but I'm new in this forum.

the # button is for posting code. I just click it and cut and paste the code between the 2 bracketed bits that come up.