store values from Serial monitor to a variable

How would I store data from the serial monitor into a variable?

I would probably use something like this:

Serial.read(); int a=Serial.read();

if (Serial.read == distance is 1) { Serial.println(a);

Note: Im a complete noob

I think there is an example of this built into the Arduino program.

-jim lee

Yup, that’s pretty nooby code. You might find this helpful:

Serial input basics

@OP

We need to review few basics of UART Serial Communication, and these are:

1. When we send A (for example) from the Serial Monitor, the ASCII code 0x41 (lower bit is transmitted first under 10-bit wide UART Frame) is transmitted to the ATmega328P MCU of UNO Board.

2. When the above frame enters into the Receiver Section of the MCU, the 8-bit ASCII code is extracted from the frame (after error checking if enabled); at the same time, an 8-bit Status Code is generated; these 2-byte data (lower byte is the ASCII Code) is automatically saved in an unseen int-type buffer/array.

3. By executing the Serial.read() command, we read the 8-bit ASCII code (the lower 8-bit of the 16-bit wide member) of the buffer and save into a byte oriented variable for further processing.

4. The Serial.available() command allows us to check if a data byte has already been saved into the buffer/array.

5. Example Sketch

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  if (Serial.available() > 0) //at least one data item is in the buffer/array
  {
    byte x = Serial.read(); //read 8-bit ASCII code of character from the 16-bit item of buffer
    Serial.println((char)x); //show the character on Serial Monitor
  }
}