Adding a timing function to Arduino

Hello,

I am working on my first Arduino project. I have most of the code finished but need to add some kind of timer to the system. Essentially, I have an IR sensor, and if it triggers, I want it to wait 5 minutes, then carry out the function, if the ir sensor gets interrupted again during the functions, then I want it to stop and wait a couple minutes then continue the function until completion then wait for next IR trigger and repeat. I want this to be an automated system so for it to work on its own without having to reset the timer or any issues like that. I need advice on how I would go about setting the timer up and which timer would be best to use for this scenario.

Any help would be greatly appreciated!

Thanks,
M

Do you also have most of the code tested? Don't add more until you do.
Paul

Not yet, I am just waiting for a sensor to arrive so I can finish the build and get to testing

Without seeing your code in code tags, I’d suggest going over it and removing the delay() calls. (You can use them in specific cases)

I’m not trying to be rude, but I’ll guess you might gain something by reading about millis(), and looking at the blink without delay example.

Hi, Arduino does support timers and interrupts. Here is a link to how to setup timers and interrupts.

Arduino-Timer-Interrupts

thanks for the reply, my account is too new to post a file so I'll copy my code below:

#define IR_CAT 7
#define LS_FWD 9
#define LS_BWD 10
#define IN_1 11
#define IN_2 12
#define HE_HOME 13

#define HOME 100
#define START_HOME_BWD 200
#define CHANGE_BWD 300
#define START_BWD_HOME 400
#define PASS_HOME_FWD 500
#define START_HOME_FWD 600
#define CHANGE_FWD 700
#define START_FWD_HOME 800
#define ERROR 900

int state = HOME; // assume program starts with robot at home
bool catishere = 0; // assume program starts with cat not here

void setup() {
Serial.begin(9600);

// inputs
pinMode(IR_CAT, INPUT_PULLUP);
pinMode(LS_FWD, INPUT_PULLUP);
pinMode(LS_BWD, INPUT_PULLUP);
pinMode(HE_HOME, INPUT_PULLUP);
// outputs
pinMode(IN_1, OUTPUT);
pinMode(IN_2, OUTPUT);
}

void loop() {
if (digitalRead(IR_CAT) == LOW) {
catishere = 1;
// restart last seen timer to 0
}
else {
catishere = 0;
// start last seen timer
}

switch(state) {
case HOME:
digitalWrite(IN_1, HIGH);
digitalWrite(IN_2, HIGH);

 if (catishere) // cat is probably here so don't move //define function for clock, this function should check if 5 minutes are up, if time is ended then it proceeds. How to do that
    {
    state = HOME; // stay here
  }
  else { // cat was last seen 5 minutes ago
    state = START_HOME_BWD; // start home to backward
  
  
  break;
  }
case START_HOME_BWD:; 
  if (catishere)  // cat is probably here so don't move 
    {
    digitalWrite(IN_1, HIGH);
    digitalWrite(IN_2, HIGH);

    state = START_HOME_BWD; // stay here
 
    break;
    }
    

  digitalWrite(IN_1, HIGH); // move the motor backward
  digitalWrite(IN_2, LOW);

  if (LS_BWD == HIGH) { // hit backward limit switch 
    state = CHANGE_BWD; // change at backward
  }
  else { // otherwise stay here
    state = START_HOME_BWD;
  }
  break;
  
case CHANGE_BWD:;
  if (catishere)  // cat is probably here so don't move 
    {
    digitalWrite(IN_1, HIGH);
    digitalWrite(IN_2, HIGH);
    
    state = CHANGE_BWD; // stay here
    break;
  

  digitalWrite(IN_1, LOW); // change the motor direction
  digitalWrite(IN_2, HIGH);
  
  state = START_BWD_HOME; // start backward to home
  break;

}
case START_BWD_HOME:;
if (catishere) // cat is probably here so don't move
{
digitalWrite(IN_1, HIGH);
digitalWrite(IN_2, HIGH);

    state = START_BWD_HOME; // stay here

    break;
  }

  if (analogRead(HE_HOME) >= 500) { // hit home 1st time
    state = PASS_HOME_FWD; // pass home to forward
  }
  else {
    state = START_BWD_HOME; // otherwise stay here
  }
  break;
  
case PASS_HOME_FWD:;
  state = START_HOME_FWD; // start home to forward
  break;
  
case START_HOME_FWD:;
  if (digitalRead(LS_FWD) == HIGH) { // hit forward limit switch
    state = CHANGE_FWD; // change at forward
  }
  else {
    state = START_HOME_FWD; // otherwise stay here
  }
  break;

case CHANGE_FWD:;
  if (catishere)  // cat is probably here so don't move 
   {
    digitalWrite(IN_1, HIGH);
    digitalWrite(IN_2, HIGH);
    
    state = CHANGE_FWD; // stay here
    break;
  }

  digitalWrite(IN_1, HIGH); // change the motor direction
  digitalWrite(IN_2, LOW);

  state = START_FWD_HOME; // start forward to home
  break;
  
case START_FWD_HOME:;
  if (digitalRead(HE_HOME) == HIGH) { // hit home 2nd time
      state = HOME; // stay home
  }
  else {
      state = START_FWD_HOME; // otherwise stay here
  }
  break;

case ERROR:

{
Serial.print("ERROR: program is in an undefined state");
state = ERROR; // stay here and spam the serial console

  break;

}
}
}

Hello
Try again and post your sketch in code tags "</>" as provided in this editor.

#define IR_CAT  7
#define LS_FWD  9
#define LS_BWD  10
#define IN_1    11
#define IN_2    12
#define HE_HOME 6
 
#define HOME 100
#define START_HOME_BWD 200
#define CHANGE_BWD 300
#define START_BWD_HOME 400
#define PASS_HOME_FWD 500
#define START_HOME_FWD 600
#define CHANGE_FWD 700
#define START_FWD_HOME 800
#define ERROR 900

int state = HOME; // assume program starts with robot at home
bool catishere = 0; // assume program starts with cat not here

void setup() {
  Serial.begin(9600);

  // inputs
  pinMode(IR_CAT, INPUT_PULLUP);
  pinMode(LS_FWD, INPUT_PULLUP);
  pinMode(LS_BWD, INPUT_PULLUP);
  pinMode(HE_HOME, INPUT_PULLUP);
  // outputs
  pinMode(IN_1, OUTPUT);
  pinMode(IN_2, OUTPUT); 
}

void loop() {
  if (digitalRead(IR_CAT) == LOW) {
    catishere = 1;
    // restart last seen timer to 0
  }
  else {
    catishere = 0;
    // start last seen timer
  }

  switch(state) {
    case HOME:
      digitalWrite(IN_1, HIGH);
      digitalWrite(IN_2, HIGH);

     //if (catishere) // cat is probably here so don't move //define function for clock, this function should check if 5 minutes are up, if time is ended then it proceeds. How to do that
        {
        state = HOME; // stay here
      }
      //else 
      { // cat was last seen 5 minutes ago
        state = START_HOME_BWD; // start home to backward
      
      
      break;
      }
    case START_HOME_BWD:; 
      //if (catishere)  // cat is probably here so don't move 
        {
        digitalWrite(IN_1, HIGH);
        digitalWrite(IN_2, HIGH);

        state = START_HOME_BWD; // stay here
     
        break;
        }
        

      digitalWrite(IN_1, HIGH); // move the motor backward
      digitalWrite(IN_2, LOW);

      if (LS_BWD == HIGH) { // hit backward limit switch 
        state = CHANGE_BWD; // change at backward
      }
      else { // otherwise stay here
        state = START_HOME_BWD;
      }
      break;
      
    case CHANGE_BWD:;
      //if (catishere)  // cat is probably here so don't move 
        {
        digitalWrite(IN_1, HIGH);
        digitalWrite(IN_2, HIGH);
        
        state = CHANGE_BWD; // stay here
        break;
      

      digitalWrite(IN_1, LOW); // change the motor direction
      digitalWrite(IN_2, HIGH);
      
      state = START_BWD_HOME; // start backward to home
      break;
}
    case START_BWD_HOME:;
     //if (catishere)  // cat is probably here so don't move 
        {
        digitalWrite(IN_1, HIGH);
        digitalWrite(IN_2, HIGH);
        
        state = START_BWD_HOME; // stay here

        break;
      }

      if (digitalRead(HE_HOME) >= 500) { // hit home 1st time
        state = PASS_HOME_FWD; // pass home to forward
      }
      else {
        state = START_BWD_HOME; // otherwise stay here
      }
      break;
      
    case PASS_HOME_FWD:;
      state = START_HOME_FWD; // start home to forward
      break;
      
    case START_HOME_FWD:;
      if (digitalRead(LS_FWD) == HIGH) { // hit forward limit switch
        state = CHANGE_FWD; // change at forward
      }
      else {
        state = START_HOME_FWD; // otherwise stay here
      }
      break;
    
    case CHANGE_FWD:;
      if (catishere)  // cat is probably here so don't move 
       {
        digitalWrite(IN_1, HIGH);
        digitalWrite(IN_2, HIGH);
        
        state = CHANGE_FWD; // stay here
        break;
      }

      digitalWrite(IN_1, HIGH); // change the motor direction
      digitalWrite(IN_2, LOW);

      state = START_FWD_HOME; // start forward to home
      break;
      
    case START_FWD_HOME:;
      if (digitalRead(HE_HOME) == HIGH) { // hit home 2nd time
          state = HOME; // stay home
      }
      else {
          state = START_FWD_HOME; // otherwise stay here
      }
      break;

    case ERROR:
{
      Serial.print("ERROR: program is in an undefined state");
      state = ERROR; // stay here and spam the serial console
      
      break;
  }
  }
}

Hello
Check this unuseally coding first:

Line 60: case START_HOME_BWD:;
Line 83: case CHANGE_BWD:;
Line 99: case START_BWD_HOME:;
Line 118: case PASS_HOME_FWD:;
Line 122: case START_HOME_FWD:;
Line 131: case CHANGE_FWD:;
Line 147: case START_FWD_HOME:;

Hey,

i get an error when trying to compile without the ":" in front, the error is: expected ':' before ';' token. It compiles properly after adding those

Hello
the ":" is needed, the ";" not.

Completely and utterly inappropriate for this purpose. :roll_eyes:

Here are the two * references you need to study.