# Millis Issue

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.

``````unsigned long timer,timef,timenow;

int f=0,b=0,t=0;
void setup()
{
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
pinMode(A4, INPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
}

void loop()
{

digitalWrite(10, read4); //H-Module inputs
void check();
}

void check()
{
if((digitalRead(A0) == HIGH) && f==b) // if(digitalRead(A0))
{    timer = millis();
f = f+1;  }

{
timef = millis() - timer;
f = f-1;

}

{
if(t==0)
{   timenow = millis();
t = t+1;}
if(millis() - timenow >= timef)
{
digitalWrite(5,LOW);
t = t-1;
}
else
digitalWrite(5,HIGH); // or read1 = high;}
}
}
``````

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))
{
timer = millis();
//    f = f+1;
f = true;
}
{
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))
{
timer = millis();
//    f = f+1;
f = true;
}

{
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.
``````
``````unsigned long timer,timef,timenow;
bool flag = false,flagt = false;
void setup()
{
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
pinMode(A4, INPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
}

void loop()
{

digitalWrite(10, read4); //H-Module inputs
void check();
}

void check()
{
if((digitalRead(A0) == HIGH) && !flag) // if(digitalRead(A0))
{    timer = millis();
flag = true; }

{
timef = millis() - timer;
flag = false;
}

{
if(!flagt)
{   timenow = millis();
flagt = true;}
if(millis() - timenow >= timef)
{
digitalWrite(5,LOW);
flagt = false;
}
else
digitalWrite(5,HIGH); // or read1 = high;}
}
}
``````