Hi! , i'm having a problem with a flip flop that I'm trying to do.
it's not a simple flip flop because it changes the output state only when the trigger pulse width is within particular values. In this case, the flip flop change to the next state when the trigger pulse is no longer than 3 seconds but also not shorter than 100ms, so , the flip flop should be running only when the pulse lenght falls in (0.1 s ; 3 s), out of this interval the output must keep the current logic state.
I tought in this code, but It does not run as expected.
Which is the error?
Please!!
const int start =3;
const int relay = 0;
const int comp = 1;
int compare; /* compare pin is conected directly to the relay ouptut pin, in order to read the output state in a new input*/
unsigned long duration;
void setup() {
pinMode(relay, OUTPUT);
pinMode(start, INPUT);
pinMode(comp, INPUT);
}
void loop()
{
compare = digitalRead(comp);
duration= pulseIn(start, HIGH); /* It's gonna drop the pulse high time, because it's connected by a pull-down resistor*/
while (100000<duration<3000000){ /* Only while this condition is true, the program below must run */
if( compare==HIGH ){
delay(200);
digitalWrite(relay, LOW); /If output is on high logic state, the next state must be low/
}
if(compare==LOW ){
delay(200);
digitalWrite(relay, HIGH); /If output is on low logic state, the next state must be high/
}
}}