Read one char only from serial and clear buffer

(Apologies, just realised this is not the right sub-forum, but i cannot find an option to delete the post)

I have created a small sketch to control my boiler that takes input from the serial monitor in Arduino IDE.

The user can input 0 (OFF) or 1 (ON). Anything else does nothing and prompts a message.

This is my code:

#define rfSendPin 4
#define ledPin 13        //Onboard LED = digital pin 13

// These are the duration of the HIGHs and LOWs [1, 0, 1, 0, ...]
int times_on[55] = {667, 438, 531, 438, 542, 438, 531, 438, 531, 438, 500, 469, 1010, 1479, 1042, 1010, 969, 531, 531, 531, 531, 938, 573, 500, 938, 531, 531, 531, 531, 938, 531, 531, 531, 531, 531, 531, 531, 531, 948, 1010, 531, 531, 531, 531, 531, 531, 531, 531, 906, 531, 531, 969, 969, 969, 969};
int times_off[55] = {667, 438, 573, 417, 604, 417, 542, 448, 510, 479, 510, 479, 958, 1500, 1021, 1021, 1021, 573, 573, 479, 542, 875, 573, 448, 958, 542, 542, 510, 510, 875, 604, 542, 510, 479, 542, 479, 510, 479, 542, 479, 542, 479, 510, 479, 542, 510, 542, 510, 958, 990, 958, 1021, 958, 958, 958};

bool sign = HIGH;
bool sent = 0;


void setup() {
  pinMode(rfSendPin, OUTPUT);
  Serial.begin(115200);
}

void loop() {
  int command;

  if (Serial.available()) {
    command = receiveCommand();

    if (command != -999) {
      controlBoiler(command);
    } else
    {
      Serial.println("Please input 1 (ON) or 0 (OFF)");
    }
  }
}

int receiveCommand() {
  char receivedChar;

  receivedChar = Serial.read();
  if (receivedChar != 48 && receivedChar != 49) {
    return -999;
  } else
  {
    return receivedChar - '0';
  }
  return -1;
}


void controlBoiler(int command) {
  Serial.print("Turning boiler ");
  //  Serial.print(command);
  if (command == 1) {
    Serial.println("ON.");
  }
  else
  {
    Serial.println("OFF.");
  }

  int repeat = 1;

  while (repeat < 4) {

    Serial.print(".");

    // Start with a low signal for second
    digitalWrite(rfSendPin, LOW);
    delay(1000);

    // Send the raw signal
    for (int8_t i = 0; i < 55; i++) {
      digitalWrite(rfSendPin, sign);
      if (command == 1) {
        delayMicroseconds(times_on[i]);
      }
      else
      {
        delayMicroseconds(times_off[i]);
      }
      sign = !sign;
    }
    repeat += 1;
  }
  Serial.println();
}

The code works fine, but the message ‘Please input 1 (ON) or 0 (OFF)’ is repeated twice when 1 or 0 is input and three times when a wrong character is input.

I guess it depends on other characters being present in the serial buffer? Maybe Carriage Return (13)?

How can I read just one character and flush the buffer? Is using Serial.read() the right thing to do?

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

The second example might be most appropriate.

...R

Rojj:
The code works fine, but the message 'Please input 1 (ON) or 0 (OFF)' is repeated twice when 1 or 0 is input and three times when a wrong character is input.

I guess it depends on other characters being present in the serial buffer? Maybe Carriage Return (13)?

How can I read just one character and flush the buffer? Is using Serial.read() the right thing to do?

Please, be sure that the No line ending option is active in the Serial Monitor; if so, you will not receive repeated message.
sm-8.png

sm-8.png

Robin2:
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

The second example might be most appropriate.

…R

Example 2 works like a charm. Thanks

GolamMostafa:
Please, be sure that the No line ending option is active in the Serial Monitor; if so, you will not receive repeated message.
sm-8.png

Yep. It is also explained in Example 2. Thanks

You can simplify somewhat. This ignores line feed and return characters and prints the prompt on any but '0' or '1'

void loop() {
 	 if (Serial.available()) {
		char userCharacter = Serial.read();
		switch ( userCharacter ) {
			case ‘0’ : 
				controlBoiler ( 0 );
				break;
			case ‘1’ :
				controlBoiler ( 1 );
				break;
			case 10 : // Line feed
			case 13 : // carriage return
				break;
			default :       
				Serial.println("Please input 1 (ON) or 0 (OFF)”);
				break;
		}
	}
}

DannySwarzman:
You can simplify somewhat.

Neat.

...R