Pausing Serial.Read

Hello:

Would appreciate in help in coding the below code to introduce a “pause” when there is a serial.read. The project involves a remote control and sensor board that outputs various codes that can be captured and utilized. Each keyed output has a 0x20 header followed by a byte that can be used. in my case I am using it in a switch statement to call various routines.

If a user clicks multiple times on a key the input is read and multiple calls are made to my functions. I want the processing of a function to finish before the second click of a button takes place. Not sure if I need to throw out the serial reads or modify the functions. Unfortunately the remote code doesn’t send a end of line or return code.

So what happens if I repeatedly press the key to increment on the remote the function is called incrementing a counter and displaying the new value on the 7 seg for 4 seconds before turning off. But it repeats itself. I want the function to finish processing before a new keypress is acknowledged

void loop() {

 if (Serial.available() > 0) {    // is a character available?
     incomingByte = Serial.read(); // get the character
    
  
    // check if a number was received
    if (incomingByte!=0x20) {     // Throw out character header 32
       Serial.println(incomingByte);
      switch (incomingByte) {
              case 28: 
               Serial.println("Power");
               break;
              case 16: 
               Serial.println("Show Fan");
               ToggleFan();
               break;
              case 17: 
               Serial.println("Show temp");
               ShowTemp();
               break;
              case 25: 
               Serial.println("Show Eeprom");
               ShowEeprom();
               break;
              case 24: 
               Serial.println("Increment");
               Increment();
               break;
              case 21: 
               Serial.println("Decrement");
               Decrement();
               break;
             default: 
               Serial.println(incomingByte);
            }  
       }
    
   }


void Decrement()
{
 
 unsigned long start = millis();
 unsigned long finish = start;
 unsigned long counter;

  if(storedTemp>=MIN_THERMOSTAT){
      storedTemp--;
      EEPROM.write(EEPROM_THERMOSTAT_ADDRESS,storedTemp);
       Serial.println(storedTemp, DEC);
       
  }   
  
  sevseg.setNumber((byte)storedTemp, 1);
  while ((finish - start) <=5000) // do this loop for 5 seconds to display the sevseg
    {
      sevseg.refreshDisplay(); // Must run repeatedly
      counter = counter+1;
      finish = millis();
      
  }

   
}

I want the processing of a function to finish before the second click of a button takes place.

How would you know that a button has been clicked, if you don't read the state of the button (I assume that you mean switch, because buttons are usually plastic and lousy conductors)?

Buttons are on a remote control. The remote IR is sent to a board that does all the debouncing etc and outputs to the Arduino RX pin a 0x20 header followed by a command code denoting which button was pressed.

Well, I am certain it is bad programming and has pitfalls but a easy fix was to just close the serial connection thus flushing it and restarting at the end of each function

void Decrement()
{
 
 unsigned long start = millis();
 unsigned long finish = start;
 unsigned long counter;

  if(storedTemp>=MIN_THERMOSTAT){
      storedTemp--;
      EEPROM.write(EEPROM_THERMOSTAT_ADDRESS,storedTemp);
       Serial.println(storedTemp, DEC);
       
  }   
 
  sevseg.setNumber((byte)storedTemp, 1);
  while ((finish - start) <=5000) // do this loop for 5 seconds to display the sevseg
    {
      sevseg.refreshDisplay(); // Must run repeatedly
      counter = counter+1;
      finish = millis();
     
  }

     Serial.end();   
     Serial.begin(9600);

   
}

I suspect the last thing you should be doing is Pausing Serial.Read

You should read incoming data as it arrives. After it has arrived your program can decide what to do with it.

That is the basis of the examples in serial input basics. Have a look at them - either use one of them directly or adapt it to your situation.

...R