Overview
In addition to reading data from the electronic components, the Mega2560 board can read the data input in the serial port monitor, and you can use Serial.read() as the controller of the circuit experiment. Then we use LED to experiment with the Serial. Read() statement to control LED to turn on and off.
Components Required
Note: Refer to Part 2 to check details of hardware.
Fritzing Circuit
In this example, we use digital pin 9 to drive LED. When 1 is entered in serial monitor, the LED lights up. When 0 is entered, the LED turns off.
Schematic Diagram
Code
const int ledPin = 9;
int incomingByte = 0;
void setup()
{
pinMode(ledPin,OUTPUT);
Serial.begin(9600);
}
void loop()
{
if (Serial.available() > 0)
{
incomingByte = Serial.read();
if(incomingByte=='1')
{
digitalWrite(ledPin,HIGH);
}
else if(incomingByte=='0')
{
digitalWrite(ledPin,LOW);
}
}
}
After the codes are uploaded to the Mega2560 board, please turn on the serial port monitor. Typing in ”1” can make LED turn on and typing in ”0” can make it turn off.
Code Analysis
Declare digital pin 9 as ledPin.
const int ledPin = 9;
Serial.read() reads a single byte of ASCII value, and therefore you need to declare a int type variable, incomingByte to store the acquired data.
int incomingByte = 0;
Run the serial communication in setup() and set the data rate to 9600.
Serial.begin(9600);
Set ledPin to OUTPUT mode.
pinMode(ledPin,OUTPUT);
The state of serial port monitor is judged in loop(), and the information processing will be carried out only when the data are received.
if (Serial.available() > 0){}
Reads the input value in the serial port monitor and stores it to the variable incomingByte.
incomingByte = Serial.read();
When the character ‘1’ is obtained, the LED is lit; when ’0’ is obtained, the LED turns off.
if(incomingByte=='1'){digitalWrite(ledPin,HIGH);}
else if(incomingByte=='0'){digitalWrite(ledPin,LOW);}
NOTE: Serial.read() takes the ASCII value in single character, which means that when you input ‘1’, the obtained value is not the number ‘1’, but the character ‘1’ whose corresponding ASCII value is 49.
※ ASCII chart
The ASCII (American Standard Code for Information Interchange) encoding dates to the 1960’s. It is the standard way that text is encoded numerically.
Note that the first 32 characters (0-31) are non-printing characters, often called control characters. The more useful characters have been labeled.
ASCII | Character | ASCII | Character | ASCII | Character | ASCII | Character | ASCII | Character | ASCII | Character |
0 | Null | 23 | 46 | . | 69 | E | 92 | / | 115 | s | |
1 | 24 | 47 | / | 70 | F | 93 | ] | 116 | t | ||
2 | 25 | 48 | 0 | 71 | G | 94 | ^ | 117 | u | ||
3 | 26 | 49 | 1 | 72 | H | 95 | _ | 118 | v | ||
4 | 27 | 50 | 2 | 73 | I | 96 | 、 | 119 | w | ||
5 | 28 | 51 | 3 | 74 | J | 97 | a | 120 | x | ||
6 | 29 | 52 | 4 | 75 | K | 98 | b | 121 | y | ||
7 | 30 | 53 | 5 | 76 | L | 99 | c | 122 | z | ||
8 | 31 | 54 | 6 | 77 | M | 100 | d | 123 | { | ||
9 | tab | 32 | space | 55 | 7 | 78 | N | 101 | e | 124 | | |
10 | line feed | 33 | ! | 56 | 8 | 79 | O | 102 | f | 125 | } |
11 | 34 | “ | 57 | 9 | 80 | P | 103 | g | 126 | ` | |
12 | 35 | # | 58 | : | 81 | Q | 104 | h | 127 | ||
13 | carriage return | 36 | $ | 59 | ; | 82 | R | 105 | i | ||
14 | 37 | % | 60 | < | 83 | S | 106 | j | |||
15 | 38 | & | 61 | = | 84 | T | 107 | k | |||
16 | 39 | , | 62 | > | 85 | U | 108 | l | |||
17 | 40 | ( | 63 | ? | 86 | V | 109 | m | |||
18 | 41 | ) | 64 | @ | 87 | W | 110 | n | |||
19 | 42 | * | 65 | A | 88 | X | 111 | o | |||
20 | 43 | + | 66 | B | 89 | Y | 112 | p | |||
21 | 44 | , | 67 | C | 90 | Z | 113 | q | |||
22 | 45 | – | 68 | D | 91 | [ | 114 | r |
Phenomenon Picture