Code isn't surely even close to perfect but even above that, it doesn't work.
I want to measure for how long A0 goes high and then replicate A0's input / output on A0 by an external switch i.e. pin A4. i.e. I make it high for 5 secs and I turn it off. Afterwards I turn on A4 and it goes in runmode and runs the pin A0 for 5 seconds as it hard recorded.
On detecting a pin's change in status, that is when the pin goes from HIGH to LOW or LOW to HIGH you have to have previously recorded the value of millis() in a variable so that on the next transition, you can calculate the elapsed time.
To detect a change in status, you also have to store the previous status in a variable.
6v6gt:
On detecting a pin's change in status, that is when the pin goes from HIGH to LOW or LOW to HIGH you have to have previously recorded the value of millis() in a variable so that on the next transition, you can calculate the elapsed time.
To detect a change in status, you also have to store the previous status in a variable.
My apologies, could you have another look at the code?
Couldn’t you come up with better names for f and b, and what’s their purpose, really?
It seems you use them as flag - while it’s much easier to use a single boolean flag and just check whether it’s true or false. Like this:
bool f = false;
[...]
// if((digitalRead(A0) == HIGH) && f==b) // if(digitalRead(A0)) or if(!digitalRead(A0))
if((digitalRead(A0) == HIGH) && !f) // if(digitalRead(A0)) or if(!digitalRead(A0))
{
timer = millis();
// f = f+1;
f = true;
}
// if((!digitalRead(A0))&& f==b+1)
if((!digitalRead(A0)) && f)
{
timef = millis() - timer;
// f = f-1;
f = false;
timenow = millis();
}
Now give f a more descriptive name and your code has become a lot more readable. Also do a CTRL-T in the IDE to fix your indentation.
wvmarle:
Couldn't you come up with better names for f and b, and what's their purpose, really?
It seems you use them as flag - while it's much easier to use a single boolean flag and just check whether it's true or false. Like this:
bool f = false;
[...]
// if((digitalRead(A0) == HIGH) && f==b) // if(digitalRead(A0)) or if(!digitalRead(A0))
if((digitalRead(A0) == HIGH) && !f) // if(digitalRead(A0)) or if(!digitalRead(A0))
{
timer = millis();
// f = f+1;
f = true;
}
// if((!digitalRead(A0))&& f==b+1)
if((!digitalRead(A0)) && f)
{
timef = millis() - timer;
// f = f-1;
f = false;
timenow = millis();
}
Now give f a more descriptive name and your code has become a lot more readable. Also do a CTRL-T in the IDE to fix your indentation.