Sending numbers from 0 to 255 through serial monitor

Hello,
I am trying to have the arduino control the brightness of an LED (connected to pin 11) based on a serial input (0 to 255) from the "Serial monitor".
Here is my code:

byte x=0;

void setup() {

Serial.begin(9600);

pinMode (11, OUTPUT);


}

void loop() {
if (Serial.available()) {   
 
 byte x = Serial.read();
 Serial.println(x);
 delay(1);
 
analogWrite (11, x);
delay(1);
}

}

I am running into two problems:

  1. The arduino can only read 1 byte at a time.
  2. It reads the ASCII value of the number I enter.

How can I have the arduino read say "200" as a single byte?

  1. The arduino can only read 1 byte at a time.

Not true. The method you are using only reads one byte. So, call it over and over, saving the data in a NULL terminated char array, until the end of packet marker arrives. Don't know what an end of packet marker is? Learn!

  1. It reads the ASCII value of the number I enter.

It would be a different story if you stored the value in a char, like you should.

Its not clear whether you want to send three characters representing 255 or whether you want to send a single byte that has the value 255?

Both are possible, though you would probably need to write a small program on the PC to send single byte values that don't correspond with the non-ascii characters as you can't easily type them in the Serial Monitor.

You might also consider whether you really need 255 different brightness levels. If (say) 26 levels are sufficient you can easily get that by sending one of the A-Z or a-z characters. And use both groups if you want 52 brightness levels.

...R

You want to set up Serial Monitor to add a newline or carriage return or both at the end of every line sent just to tell you when the line ended.

Please do not go by using a delay and assuming the line will be in by then. It works when used "right" but it's a really bad habit.

If I receive ASCII '2' '5' '\n' ( \n is newline, ASCII 10 ) then my usual routine...

-> has a variable to serve as the accumulator, always starting at zero
--> reads the '2' on the first Serial.available() --- note that loop may go around 1000x between available serial
----> multiplies the accumulator by 10; here 10 x 0 = 0
----> adds '2' - '0' which equals 2; 0 + 2 = 2
--> reads '5' on the next Serial.available()
----> does the same 2 steps; 2 x 10 = 20, 20 + 5 = 25
--> reads the '\n' on the next Serial.available()
----> exits the conversion process after setting another variable from 0 to 1 to indicate a good number entered.
=== yes, I check to make sure I don't turn garbage into garbage!
Other things I do check for include bounds/length, it's no good trying to jam 8 digits into an int, for instance.
Often the safety net error check code can equal or exceed the when everything goes right code.

The actual code for that bit is shorter. The idea is that it only runs as chars come in and that when newline is received there is very little left to be done.
Buffering a line and processing after newline:

  1. requires a buffer
  2. don't forget to check/catch errors. "25O\n" is not the same as "250\n", simple conversion will give you 25 for the first and 250 for the second without a peep that someone may have mistyped O for 0.

The same routine can be used to loop through buffered text, but then you need the buffer and wait.
When using a terminal that sends every character immediately (Arduino Serial Monitor only sends lines) your code has the option to catch typing errors the moment they happen. User I/O can have instant interaction when you process a char at a time, as opposed to enter data, get an error message, re-enter the complete line again. With buffer then process you can only do the latter.
I learned to process keystrokes 33-34 years ago. IMO standard input methods are clunky and suck.