How do I read the enter key with scanf in c?

Hi Guys,

I'm a student doing a Uni project and for the project, we are using a Mega2560 and we are to write C code onto the Arduino. One of the requirements is to print some info only after the user has hit enter in the serial monitor. Now I know we can read the enter key using serial.read but for this project, we aren't allowed to use the basic Arduino functions such as serial.read and write. We are to only use scanf.

I looked around and saw that we can scan for '\n' or '\r' but I have been so far unsuccessful at reading these in the serial monitor.

Any help would be appreciated. My code for that bit is below:

char inputEnter;
static FILE uart00 = { 0 };

void setup() 
{

    USART0_init(); //initialise USART0 parameters
    fdev_setup_stream(&uart00, TX, RX, _FDEV_SETUP_RW); //Associate RX and TX functions with stdin/stdout
    stdin = stdout = &uart00; //the uart is defined as the standard inputa/output device
}

void loop() 
{

  inputOne();
}

void USART0_init(void)
{
    UCSR0A = B00000000; //single transmission speed, multiprocessor disabled
    UCSR0B = B00011000; //enable Rx & Tx
    UCSR0C = B00000110; //asynchronous, no parity, 1 stop, 8 bits
    UBRR0 = 103;        //load the value for 9600 bps baud rate into whole UBRR register
}

static int RX(FILE* stream)
{
    //do nothing until data is received and is ready to be read from UDR0; wait for USART RX Complete flag
    while ((UCSR0A & (1 << RXC0)) == 0) {};
    //when flag is set read data from USART UDR0 register and return it
    return(UDR0);
}

static int TX(char TXData, FILE* stream)
{
    //do nothing until UDR0 is ready for more data to be written to it; wait for USART UDRE flag
    while ((UCSR0A & (1 << UDRE0)) == 0) {};
    //when flag is set send data by placing the byte into UDR0
    UDR0 = TXData;
    return 0;
}

void inputOne()
{
    printf("Please press Enter:\n\r");
    scanf("%s", &inputEnter);

    if(inputEnter== 0B000001101)
    {
      printf("User has pressed ENter");     
    }

}

please edit and use code tags

What is it and where it is defined?

I've defined it at the beginning of the program and its just an char

My entire program is quite big but I'm just having this small issue which is why I've only provided a snippet of my program

I don't think you understand the assignment. sscanf() can't do I/O, it can only operate on data in memory. read() and write() or something like it, is absolutely necessary to move the data into memory, no matter how you need to process it after that.

I was under the impression that in the serial monitor, with every line there is a '\n' or a '\r' returned. So I was trying to store the input from the serial monitor into a defined character and read it? I understand scanf cannot read I/O but I am asking if there is a way we can read empty input from the serial input?

What do you mean, "read empty input"? Wouldn't that return nothing?

My mistake, yeah, sscanf() works in memory, scanf() does read from standard input. Need more coffee.

Anyway, you need to post a complete, compilable sketch

scanf not sscanf

I know, I finally realized... :slight_smile:

I've uploaded an example code of what im trying to do.

Again I know that scanf is not preferred in C and all but I'm very restricted when it comes to using basic Arduino functions. Not important to explain why but im just wondering if there is a way we can read the press of an enter key. I did get some help from another student who said that The serial monitor affixes a '\n', an '\r' or both to everything sent through the serial port. How would I read that?

Reason being is that I need the program to stop and wait for the user to make hardware changes to the Arduino by grounding some pins. When done the user will press enter to proceed further with the remaining of the program.

Isn't it the 'c' format specifier that you need, to read a character?

Would they get upset if you just used getc()?

Yeh lol! Pretty sure wouldn't get marked at all.

Not sure why. It's standard C, nothing to do with Arduino...

All G! Thanks for your help. I'll just YOLO it

You can use

scanf( "%c", rxChar);
if (rxChar == '\n') { ...

... not tested

Nah, dont work :frowning: keeps repeating the loop asking to press enter but doesnt print "user has pressed enter"

Some code you didn't post? Do you have line endings enabled in the serial monitor?

Yeh I do. Currently set to Newline. And no, that's the entire code.

I just tested it, it works.

char inputEnter;
static FILE uart00 = { 0 };

void setup() 
{

    USART0_init(); //initialise USART0 parameters
    fdev_setup_stream(&uart00, TX, RX, _FDEV_SETUP_RW); //Associate RX and TX functions with stdin/stdout
    stdin = stdout = &uart00; //the uart is defined as the standard inputa/output device
}

void loop() 
{

  inputOne();
}

void USART0_init(void)
{
    UCSR0A = B00000000; //single transmission speed, multiprocessor disabled
    UCSR0B = B00011000; //enable Rx & Tx
    UCSR0C = B00000110; //asynchronous, no parity, 1 stop, 8 bits
    UBRR0 = 103;        //load the value for 9600 bps baud rate into whole UBRR register
}

static int RX(FILE* stream)
{
    //do nothing until data is received and is ready to be read from UDR0; wait for USART RX Complete flag
    while ((UCSR0A & (1 << RXC0)) == 0) {};
    //when flag is set read data from USART UDR0 register and return it
    return(UDR0);
}

static int TX(char TXData, FILE* stream)
{
    //do nothing until UDR0 is ready for more data to be written to it; wait for USART UDRE flag
    while ((UCSR0A & (1 << UDRE0)) == 0) {};
    //when flag is set send data by placing the byte into UDR0
    UDR0 = TXData;
    return 0;
}

void inputOne()
{
    printf("Please press Enter:\n\r");
    scanf("%c", &inputEnter);

    if(inputEnter== '\n')
    {
      printf("User has pressed ENter");     
    }

}