Thank you all, guys. I considered your ideas, and then I code this solution. It works fine, right now I'm reading my sensor once every 30 minutes (1,800,000 miliseconds) and whenever I send the magic word. I hope it is useful to you:
void loop(){
if ((millis() - timer < 1800000) && (Serial.available() > 0)){
magicInput = Serial.readString();
magicInput.trim(); //Cleans input from \n at the end, otherwise never equals magicInput
if (magicInput.equals(magicWord)){ //Elegant form for "=="
Serial.println();
Serial.println(" === Your request ==="); //Shows requested measurement
Serial.println(millis() - timer); //Time since last measurement
measure(); //
}
}
if (millis() - timer >= 1800000){
Serial.println();
Serial.println("===== Normal mode ====="); //Indicates that measurement was not requested by user
Serial.println(millis() - timer); //Checks that 30 "exact" minutes have run
mide();
timer = millis();
}
}
Serial.available()
or the serialEvents can be used essentially like an interrupt.available
frequently or, if you are usingserialEvent
, you must make sure thatloop
doesn't block. However, in the OP's application, polling is sufficient (see gre_gor's answer).