I am working on a small code to work only when a certain character or number is passed. I have a script below where serial print HELLO only when enter or any character is pressed because it's checking for the serial read. I would like to modify it in such a way that this condition should be in setup and in setup it waits for certain character lets say 'x' to be pressed and when 'x' is pressed, the loop starts to run or any other function runs and if anything else is printed on serial the program does not proceed forward and wait until 'x' is pressed/passed to serial. I have tried a couple of hit and trial but couldn't find any possible solution.
Looking for some suggestion.
Thank you
void setup()
{
Serial.begin(9600);
while(!Serial)
{
;
}
}
void loop()
{
int num = 1234;
Serial.println("Press any key to continue...");
while (Serial.available())
{
char character = Serial.read();
if(character == 'x')
{
//Serial.println("good");
}
else
{
//Serial.println("Bad");
}
}
while (!Serial.available());
Serial.println("Hello");
Serial.println(num, HEX);
Serial.println(" ");
delay(500);
}
Serial.begin(115200);
while (!Serial)
{
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Press any key to continue...");
while (Serial.available() > 0)
{
char incom = Serial.read();
if (incom != 'x' || incom != 'X')
{
Serial.println("x is pressed");
}
else
{
Serial.println("Error");
}
}
while (!Serial.available());
}
While in the second code below, it waits for serial but went to loop whatever is passed in the serial terminal.
I want to start the loop function only with a specific character.
Please give me some kind of suggestion on how to overcome this issue.
void setup()
{
Serial.begin(9600);
//-keep checking until character x arrives from Serial Monitor
do
{
byte n = Serial.available(); //checking if FIFO buffer contains any data byte came via UART Port
if (n != 0)
{
char ch = Serial.read(); //a character has come; it may not be x
if ( ch == 'x')
{
break; //the arrived character is x; goto loop() function
}
}
}
while (1); //continue receiving characters and checking for x
}
void loop()
{
Serial.println("Hello");
delay(1000);
}
GolamMostafa:
Unfortunately, your nested code reads a data byte from the buffer without checking if the buffer really holds data.
Serial.read() doesn't read a data byte from the input buffer if there is no data in the buffer. It checks for that condition and returns -1 in that case. You can safely call Serial.read() without calling Serial.available() first.
The process has just started; no data has arrived from from the Serial Monitor. One makes a read operation on the buffer and gets FF in hex; is it 255 ot -1 -- depends on the interpretative mind of the user.
The phrase 'really holds' refers to the fact that the buffer has accumulated at least 1 data byte that has come over the Serial Port. It is safe that one executes 'byte n = Serial.available();' and gets 0 or non-zero to decide if performing read operation on the buffer.
The comment of @PaulS is not as simple as could be thought. One has to learn how to get rid of his nest that he very often creates to make fun/humor.
christop: Serial.read() doesn't read a data byte from the input buffer if there is no data in the buffer. It checks for that condition and returns -1 in that case. You can safely call Serial.read() without calling Serial.available() first.
The 'byte x = Serial.read();' instruction performs a read operation on the FIFO buffer and brings out the data byte from the top location of the buffer and stores int into the variable x -- that's what I have learnt, and I have been applying it all the times in my sketches. On the other hand, the 'byte n = Serial.available();' instruction does not bring out any data byte from the FIFO buffer. To me, .read() and .available() methods are different, and they do different things.
To me, .read() and .available() methods are different, and they do different things.
Yes, they are different and they do different things.
However, if nothing is available to be read then read() explicitly returns -1 so it is safe to use in the situation required by the OP
int HardwareSerial::read(void)
{
// if the head isn't ahead of the tail, we don't have any characters
if (_rx_buffer_head == _rx_buffer_tail) {
return -1;
} else {
unsigned char c = _rx_buffer[_rx_buffer_tail];
_rx_buffer_tail = (rx_buffer_index_t)(_rx_buffer_tail + 1) % SERIAL_RX_BUFFER_SIZE;
return c;
}
}
I appreciate the response and discussion you have made over my small question. That's how you learn by sharing knowledge but in the end, I would say that @UKHeliBob suggestion was small as my sketch is already doing a lot of things but I appreciate @GolamMustafa's thought too about mentioning small details that most of us skipped.
Anyways, it worked with both of your code seniors. Thank you and cheers !!