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 read1,read2,read3,read4,read5;

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()
{
  read1 = digitalRead(A0);
  read2 = digitalRead(A1);
  read3 = digitalRead(A2);
  read4 = digitalRead(A3); //signal inputs
  read5 = digitalRead(A4); //Runmode button
  
  digitalWrite(5, read1);
  digitalWrite(6, read2);
  digitalWrite(9, read3);
  digitalWrite(10, read4); //H-Module inputs
  void check();
}


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

  }

  
if(read5 == HIGH)
{
    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))
  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.
unsigned long timer,timef,timenow;
int read1,read2,read3,read4,read5;
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()
{
  read1 = digitalRead(A0);
  read2 = digitalRead(A1);
  read3 = digitalRead(A2);
  read4 = digitalRead(A3); //signal inputs
  read5 = digitalRead(A4); //Runmode button
  
  digitalWrite(5, read1);
  digitalWrite(6, read2);
  digitalWrite(9, read3);
  digitalWrite(10, read4); //H-Module inputs
  void check();
}


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

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