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!!
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"
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 ) 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 ) how is the condition "flag" ever become true??
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....