We will define a Led variable and set it to represent pin 13 of Arduino. By invoking the pinMode function, the Led variable is declared as the output pin and, hence, we will use it for connecting to an output device in order to perform a desired action. In this application, the output device will be an LED.
Because we want our PC to communicate with Arduino using serial communication, we need to set the data rate in terms of bits per second. So, we will call the Serial.begin function to set the serial data transmission speed at 9,600 bits per second (however, it can be any baud rate). Thereafter, we will display a message on the serial port in a human-readable format, informing the user that 0 can be pressed to switch off the connected LED and 1 can be pressed to switch on the LED.
Within the loop function of Arduino, we will invoke the Serial.available function to check whether any data is available in the serial port to read from. That is, the serial receive buffer will be checked to see whether some data or bytes are available there to be read. The data can be available in the serial receive buffer only when the user presses any key. It also means that no output will appear until the user presses any key. The moment the user presses any key, that byte will go into the serial receive buffer and the Serial.available function will return the Boolean value true. As a result of this, the if block will execute.
Within the if block, we will invoke the Serial.read function to read the serial data from the serial port. The data or byte that is read from the serial port will be assigned to the variable input. The byte read is always in ASCII format. The user is supposed to press either 0 or 1; their corresponding ASCII values are 48 and 49, respectively. So, if the user presses 0, its ASCII value, 48, will be assigned to the variable input. And if the user presses 1, its ASCII value, 49, will be assigned to the variable input.
To get the actual value of the number entered by the user, the value of 48 is subtracted from the variable input. If the user presses 0, a specified if block will execute. Within that if block, we will invoke the Serial.println function to display the message LED is OFF to inform the user. And we will invoke the digitalWrite method to send a LOW signal to output pin 13 where the LED is connected. Consequently, the LED, if it is glowing, will be switched off.
If the user presses 1, then another if block will execute; in this case, we will invoke the Serial.println function to display the message LED is On. And we will invoke the digitalWrite function to send a HIGH signal to output pin 13 to make the LED glow. If the user has not pressed either 0 or 1, we will display a message asking them to press either 0 or 1 only.
After uploading the program to Arduino, we can press Ctrl + Shift + M to open the serial monitor. In the serial monitor, we will get the following message: Enter 0 to switch off LED and 1 to switch it On (refer to the first dialog box in Figure 5.11). After pressing 0 in the serial monitor, we will get the message LED is OFF and, again, you will be prompted to enter either 0 or 1 (refer to the second dialog box in Figure 5.11). Besides the message in the serial monitor, the LED that is connected to the 13th pin of the Arduino board will also get switched off (if it was glowing). On pressing 1, the message LED is ON will be displayed in the serial monitor. Additionally, a message will appear prompting us to enter either 0 or 1 (refer to the third dialog box in Figure 5.11). Also, the LED connected to the Arduino board will glow:
Voilà! We've successfully made an LED switch on and off on the basis of the input made to the serial port using Arduino.
Now, let's move on to the next recipe!