How to execute a command after no data from serial after 3 minutes?

How to execute a command after no data from serial after 3 minutes?
here is the code for reference

#include "TinyIRSender.hpp"
#include <LiquidCrystal_I2C.h>
//A simple code to receive characters and turn outputs on/off
const int RelayOn = 0;
const int RelayOff = 1;
int value;
int counter;
const int OffSet=0;
const int OutputPins[] = {10,2, 3, 4, 5, 6, 7, 8, 9};
const int NumOutputs = sizeof(OutputPins) / sizeof(OutputPins[0]);
LiquidCrystal_I2C lcd(0x27,20,4);


void setup() {
  lcd.init();
  lcd.backlight();
  Serial.begin(9600);
  for (int n = 0;  n < NumOutputs; n++) {
    pinMode(OutputPins[n], OUTPUT);
    digitalWrite(OutputPins[n], RelayOff);
    lcd.setCursor(n, 0);
    lcd.write('a'+n);
  }
  lcd.setCursor(0,1);
  lcd.print("DISP ON ");
  lcd.setCursor(13,1);
  lcd.print("***");
  lcd.setCursor(15,0);
  lcd.print("*");
  Serial.print("Initialized ");
  Serial.print(NumOutputs);
  Serial.println(" outputs");
}
// isDigit(myChar)
void loop() {
  if (Serial.available()) {
    int ch = Serial.read();
    int relay;
    switch (ch) {
      case 'A'...('A'+NumOutputs-OffSet): //adjust character to array index value
        lcd.setCursor(15,0);
        lcd.print("R");
        relay = ch - 'A';
        // Serial.print("Turning on relay ");
        // Serial.write(ch);  Serial.println();
        digitalWrite(OutputPins[relay], RelayOn);
        for(counter=0;counter<NumOutputs;++counter){
          lcd.setCursor(relay,0);
          lcd.write(ch);
        }
        lcd.setCursor(15,0);
        lcd.print("*");
      break;
      case 'a'...('a'+NumOutputs-OffSet): //adjust character to array index value
        lcd.setCursor(15,0);
        lcd.print("r");
        relay = ch - 'a';
        digitalWrite(OutputPins[relay], RelayOff);
        for(counter=0;counter<NumOutputs;++counter){
          lcd.setCursor(relay,0);
          lcd.write(ch);
        }
        lcd.setCursor(15,0);
        lcd.print("*");
      break;
      default:
        lcd.setCursor(15,0);
        lcd.print("C");
        value=Serial.parseInt();
        if(value==565){
          lcd.noBacklight();
          lcd.setCursor(0,1);
          lcd.print("DISP OFF");
        }else if(value==566){
          lcd.backlight();
          lcd.setCursor(0,1);
          lcd.print("DISP ON ");
        }else{
          sendNEC(12,0,value,1);
          lcd.setCursor(13,1);
          lcd.print("   ");
          lcd.setCursor(13,1);
          lcd.print(value);
        }
        lcd.setCursor(15,0);
        lcd.print("*");
      break;
    }
  }
  //It would go here
  //It would go here
  //It would go here
  //It would go here
  //It would go here
  //It would go here
}

Start a timer when the last successful command is received, if the timer times out, do whatever it is you want to do on that condition.

still not working

#include "TinyIRSender.hpp"
#include <LiquidCrystal_I2C.h>
//A simple code to receive characters and turn outputs on/off
const int RelayOn = 0;
const int RelayOff = 1;
int value;
int counter;
int lastMessage=millis();
const int OffSet=0;
const int OutputPins[] = {10,2, 3, 4, 5, 6, 7, 8, 9};
const int NumOutputs = sizeof(OutputPins) / sizeof(OutputPins[0]);
LiquidCrystal_I2C lcd(0x27,20,4);


void setup() {
  lcd.init();
  lcd.backlight();
  Serial.begin(9600);
  for (int n = 0;  n < NumOutputs; n++) {
    pinMode(OutputPins[n], OUTPUT);
    digitalWrite(OutputPins[n], RelayOff);
    lcd.setCursor(n, 0);
    lcd.write('a'+n);
  }
  lcd.setCursor(0,1);
  lcd.print("DISP ON ");
  lcd.setCursor(13,1);
  lcd.print("***");
  lcd.setCursor(15,0);
  lcd.print("*");
  Serial.print("Initialized ");
  Serial.print(NumOutputs);
  Serial.println(" outputs");
}
// isDigit(myChar)
void loop() {
  if (Serial.available()) {
    int ch = Serial.read();
    int relay;
    switch (ch) {
      case 'A'...('A'+NumOutputs-OffSet): //adjust character to array index value
        lcd.setCursor(15,0);
        lcd.print("R");
        relay = ch - 'A';
        // Serial.print("Turning on relay ");
        // Serial.write(ch);  Serial.println();
        digitalWrite(OutputPins[relay], RelayOn);
        for(counter=0;counter<NumOutputs;++counter){
          lcd.setCursor(relay,0);
          lcd.write(ch);
        }
        lcd.setCursor(15,0);
        lcd.print("*");
      break;
      case 'a'...('a'+NumOutputs-OffSet): //adjust character to array index value
        lcd.setCursor(15,0);
        lcd.print("r");
        relay = ch - 'a';
        digitalWrite(OutputPins[relay], RelayOff);
        for(counter=0;counter<NumOutputs;++counter){
          lcd.setCursor(relay,0);
          lcd.write(ch);
        }
        lcd.setCursor(15,0);
        lcd.print("*");
      break;
      default:
        lcd.setCursor(15,0);
        lcd.print("C");
        value=Serial.parseInt();
        if(value==565){
          lcd.noBacklight();
          lcd.setCursor(0,1);
          lcd.print("DISP OFF");
        }else if(value==566){
          lcd.backlight();
          lcd.setCursor(0,1);
          lcd.print("DISP ON ");
        }else if(value==222){
          lastMessage=millis();
        }else{
          sendNEC(12,0,value,1);
          lcd.setCursor(13,1);
          lcd.print("   ");
          lcd.setCursor(13,1);
          lcd.print(value);
        }
        lcd.setCursor(15,0);
        lcd.print("*");
      break;
    }
  }
  if (millis() - lastMessage >= 180000){
    lcd.noBacklight();
    lcd.setCursor(0,1);
    lcd.print("DISP OFF");
  }
}

if (millis() - lastMessage >= 180000UL)

Make that a long unsigned int as well

how would I do that?

  • Do what ?

this.

unsigned long lastMessage = millis();