As you might expect, the sketch is based on the sketch used in lesson 4. So, we will just cover the new bits here. You will find it useful to refer to the full sketch in your Arduino IDE.

Firstly, in the 'setup' function, there are three new lines on the end:
```void setup()
{
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
Serial.begin(9600);
while (! Serial); // Wait until Serial is ready - Leonardo
Serial.println("Enter LED Number 0 to 7 or 'x' to clear");
}```
Firstly, we have the command 'Serial.begin(9600)'. This starts serial communication, so that the Arduino can send out commands through the USB connection. The value 9600 is called the 'baud rate' of the connection. This is how fast the data is to be sent. You can change this to a higher value, but you will also have to change the Arduio Serial monitor to the same value. We will discuss this later, but for now leave it at 9600.

The line beginning with 'while' ensures that there is something at the other end of the USB connection for the Arduino to talk to before it starts sending messages. Otherwise, the message might be sent, but not displayed. This line is actually only necessary if you are using an Arduino Leonardo, because the Arduino Uno automatically resets the Arduino board when you open the Serial Monitor, whereas this does not happen with the Leonardo.

The last of the new lines in 'setup' sends out the message that we see at the top of the serial monitor.

The 'loop' function is where all the action happens:
```void loop()
{
if (Serial.available())
{
if (ch >= '0' && ch <= '7')
{
int led = ch - '0';
bitSet(leds, led);
Serial.print("Turned on LED ");
Serial.println(led);
}
if (ch == 'x')
{
leds = 0;
Serial.println("Cleared");
}
}
}
```

Everything that happens inside the loop is contained within an 'if' statement. So unless the call to the built-in Arduino function 'Serial.available()' is 'true' then nothing else will happen.

Serial.available() will return 'true' if data has been sent to the Arduino and is there ready to be processed. Incoming messages are held in what is called a buffer and Serial.available() returns true if that buffer is Not empty.

If a message has been received, then its on to the next line of code:

`char ch = Serial.read();`
This reads the next character from the buffer, and removes it from the buffer. It also assigns it to the variable 'ch'. The variable 'ch' is of type 'char' which stands for 'character' and as the name suggests, holds a single character.
If you have followed the instructions in the prompt at the top of the Serial Monitor, then this character will either be a single digit number between 0 and 7 or the letter 'x'.

The 'if' statement on the next line checks to see if it is a single digit by seeing if 'ch' is greater than or equal to the character '0' and less than or equal to the character '7'. It looks a little strange comparing characters in this way, but is perfectly acceptable.

Each character is represented by a unique number, called its ASCII value. This means that when we compare characters using <= and >= it is actually the ASCII values that were being compared.

If the test passes, then we come to the next line:
`int led = ch – '0';`
Now we are performing arithmetic on characters! We are subtracting the digit '0' from whatever digit was entered. So, if you typed '0' then '0' – '0' will equal 0. If you typed '7' then '7' – '0' will equal the number 7 because it is actually the ASCII values that are being used in the subtraction.

Since that we know the number of the LED that we want to turn on, we just need to set that bit in the variable 'leds' and update the shift register.
```bitSet(leds, led);
The next two lines write back a confirmation message to the Serial Monitor.
```      Serial.print("Turned on LED ");
Serial.println(led);```
The first line uses Serial.print rather than Serial.println. The different between the two is that Serial.print does not start a new line after printing whatever is in its parameter. We use this in the first line, because we are printing the message in two parts. Firstly the general bit: 'Turned on LED ' and then the number of the LED.

The number of the LED is held in an 'int' variable rather than being a text string. Serial.print can take either a text string enclosed in double-quotes, or an 'int' or for that matter pretty much any type of variable.

After the 'if' statement that handles the case, when a single digit has been handled, there is a second 'if' statement that checks to see if 'ch' is the letter 'x'.
```    if (ch == 'x')
{
leds = 0;
{"ft_lang_label":"__label__en","ft_lang_prob":0.834299,"math_prob":0.85136926,"size":3161,"snap":"2021-43-2021-49","text_gpt3_token_len":736,"char_repetition_ratio":0.116249606,"word_repetition_ratio":0.05204461,"special_character_ratio":0.2543499,"punctuation_ratio":0.13162118,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95976895,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-20T20:58:48Z\",\"WARC-Record-ID\":\"<urn:uuid:bfc757a6-e380-416a-a599-6f2719fb7141>\",\"Content-Length\":\"65339\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:5e23539f-8adb-4c3f-95dd-55c3672592f2>\",\"WARC-Concurrent-To\":\"<urn:uuid:17cc18fb-eefe-424b-a8bd-e8ce7643f9b9>\",\"WARC-IP-Address\":\"104.20.39.240\",\"WARC-Target-URI\":\"https://learn.adafruit.com/adafruit-arduino-lesson-5-the-serial-monitor/arduino-code\",\"WARC-Payload-Digest\":\"sha1:LVW3K25E2IJSFSFKEVFYTX7GWTPVTLXM\",\"WARC-Block-Digest\":\"sha1:VZIT7VUMZCPDHLT7Y5MWWFYWRTPMMRJZ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323585348.66_warc_CC-MAIN-20211020183354-20211020213354-00263.warc.gz\"}"}