pulse for X seconds, after Y period of time, (with X and Y variable)

hello forum. i want to make a program that the code would look like that:

void setup() {
attachInterrupt(0,interupt,RISING) ;
}


void loop() {
 // constantly read some sensors, and determine Y and X 
}

void interupt() {
set a "timer" and after Y millis, pulse the digital pin 5 , for X millis
(Y and X, of the time, at which the interrupt occurred )
}

i think the code speaks for itself... the main goal of the code, would be to pulse the pin 5, at the correct time, and for the correct duration. BUT to do so, it must wait for an external signal, AND do constant calculations based on sensor values...

i have read the timer libraries, but i dont really know what i am looking for.. any help , or a point to start, would be appriciated!!

something like this, not tested,

volatile boolean flag = false;
int countDown = 10;
int highTime = 50;

void setup()
{
  attachInterrupt(0, irq, RISING) ;
  pinMode(5, OUTPUT);
  digitalWrite(5, LOW);
}

void loop() 
{
  if (flag == true )
  {
    flag = false;
    delay(countDown); 
    digitalWrite(5, HIGH);
    delay(highTime );
    digtalWrite(5, LOW):
  }  
}

void irq() {
set a "timer" and after Y millis, pulse the digital pin 5 , for X millis
(Y and X, of the time, at which the interrupt occurred )
}

You can remove the delay's in the code and make it more responsive with the "blink without delay" technique. That is left as "homework" :wink:

ii get the part about "blin without delay". i suspected that would be the answer to the X pulse...
but what about the Y ?? (which is the harder :smiley: ) how is the condition "flag" ever become true??

actually, something like that wouldn't work at all... the Pulse, has to be delivered, Exactly Y millis, after the interupt... i cant afford to check only once per software cycle, cause it will have many analog reads, which will make every software cycle, at least 4-5 millis...

i was thinking, something like internal timer, which causes an interupt...

settra:
ii get the part about "blin without delay". i suspected that would be the answer to the X pulse...
but what about the Y ?? (which is the harder :smiley: ) how is the condition "flag" ever become true??

what about set the flag in the irq() ?

You must define three states

  1. WAITFORIRQ
  2. COUNTDOWN
  3. LEDHIGH

The system goes from 1 -> 2 -> 3 -> 1 etc

i dont understand what you mean... how is , any sort of flag method, not cheking every one software cycle??

volatile boolean flag = false;
int countDown = 10;
int highTime = 50;

void setup()
{
  attachInterrupt(0, irq, RISING) ;
  pinMode(5, OUTPUT);
  digitalWrite(5, LOW);
}

void loop() 
{
  if (flag == true )
  {
    flag = false;
    delay(countDown); 
    digitalWrite(5, HIGH);
    delay(highTime );
    digtalWrite(5, LOW):
  }  

  // do something else

}

void irq()
{
  flag = true;
}

when the interrupt occurs the flag is set
in the main loop() the flag is tested, if not set it can do something else
if the flag is set, it starts the line HIGH thingie.

that is what i am trying to tell you... the code you posted, checks to see if the flag is set, once per software cycle...
BUT, the softwarre may contain , 6-7 analog reads (meaning 700uS), where Y , can very well, as little as 500uS....

imagine this : the interupt arrives, just after the loop , has passed the IF statment. the interupt sets the flag, to 500us, BUT the program retruns to where it paused... so, it will take 7 analogreads (700us), before it reaches the next IF statment....
THAT is why i have to use some kind of interupt....

so you want to do this in the background?

Then you need a timer that calls IRQ1() to dirve the state machine
and an IRQ0() that is triggered by e.g. en external interrupt.

void IRQ0()
{
  state = 1;  // starts the state machine
  IRQ1();
}

void IRQ1()
{
  if (state == 1)
  {
    startTime = micros();
    state = 2;
   }
   else if (state == 2)
  {
    if (micros() - startTime >= 500)
    {
      state = 3;
      startTime = micros();
      // pin high;
    }
  }
  else if (state == 3)
  {
    if (micros() - startTime >= 100)
    {
      state = 0;
      // pin LOW;
    }
  }
}

this should get you started I think