搜索代码

Lesson 4/31: What are Binary, Decimal, Hexadecimal and Octal Number, Arduino Serial Monitor

Lesson 4/31: What are Binary, Decimal, Hexadecimal and Octal Number, Arduino Serial Monitor

In this lesson, we will explore the fundamental concepts of number systems, including binary, decimal, hexadecimal, and octal representations. Understanding these systems is crucial for working with Arduino, especially when it comes to data representation and manipulation. By the end of this tutorial, you will be able to differentiate between these number systems and utilize them effectively in your projects.

We will also introduce the Arduino Serial Monitor, a powerful tool that allows you to communicate with your Arduino board through your computer. This functionality is essential for debugging and monitoring data exchanges. To see practical examples and a thorough explanation, make sure to check the video at (in video at 12:30).

Code Examples & Walkthrough

In the following code snippets, we will explore how to use the Serial Monitor effectively. The first example initializes serial communication and prints a welcome message.

void setup() {
 Serial.begin(9600);
 Serial.println("Robojax");
 Serial.println("Welcome");
 Serial.print("To Arduino Class\n");
}

Here, the setup() function is called once at the start of the program. It initializes the serial communication at a baud rate of 9600 and prints a welcome message to the Serial Monitor.

Next, we will look at how to read user input from the Serial Monitor.

void loop() {
  if(Serial.available() > 0) {
    Serial.println(Serial.read());
  }
}

This code checks if there is any data available in the serial buffer. If there is, it reads the input and prints it back to the Serial Monitor. This is a fundamental way to interact with user input.

Lastly, let's see how to print characters based on user input.

void loop() {
  if(Serial.available() > 0) {
    Serial.print(Serial.read());
    Serial.print("\t");
    Serial.println((char) Serial.read());
  }
}

This example demonstrates how to read a character from the Serial Monitor and print it along with its corresponding ASCII value. The (char) cast is used to convert the numerical value to its character representation.

For the full code and more examples, please refer to the complete code that loads below the article.

Demonstration / What to Expect

Once you upload the code to your Arduino, you should see the welcome messages displayed in the Serial Monitor. You can then enter characters, and the Arduino will respond with the corresponding ASCII values. Be cautious of the baud rate; if it does not match the one set in the code, the output may appear garbled (in video at 18:45).

Video Timestamps

  • 00:00 Introduction
  • 0:55 Base Numbers
  • 03:13 Decimal numbers
  • 4:59 Binary numbers
  • 3:61 Octal numbers
  • 8:00 Hexadecimal numbers
  • 11:34 Serial Monitor
  • 22:54 Reading user keyboard inputs
  • 25:56 Printing actual numbers On Serial Monitor
  • 26:42 Serial monitor example codes
857-Lesson 4/30: SunFounder's 3-in-1 Smart Car Arduino kit code Serial monitor-1
语言: C++
/*
 * 课时 04 - 串口监视器和数据类型  
 * SunFounder 的 3 合 1 智能车 Arduino 套件  
 * 📚⬇️ 下载和资源页面 https://robojax.com/RJT587  
 * 由 Ahmad Shamshiri 撰写  
 * 观看完整视频说明 https://youtu.be/rJiSo6sr1hA  
 * www.robojax.com
 */
void setup() {
 Serial.begin(9600);
 Serial.println("Robojax");
 Serial.println("Welcome");
 Serial.print("To Arduino Class\n");
 Serail.print("Name: \nTelephone: \nAddress:");
 Serail.print("Last Name: \tTele\\Phone: \tAddress:");

}

void loop() {
 // 将主要代码放在这里,以便重复运行:

}

858-Lesson 4/30: SunFounder's 3-in-1 Smart Car Arduino kit code Serial monitor-2
语言: C++
/*
 * 第04课-串行监视器读取用户输入  
 * SunFounder的3合1智能车Arduino套件  
 * 📚⬇️ 下载和资源页面 https://robojax.com/RJT587  
 * 作者:Ahmad Shamshiri  
 * 观看完整视频讲解 https://youtu.be/rJiSo6sr1hA  
 * www.robojax.com
 */
void setup() {
 Serial.begin(9600);

}

void loop() {
  if(Serial.available() >0 ){
    Serial.println(Serial.read());
  }

}

859-Lesson 4/30: SunFounder's 3-in-1 Smart Car Arduino kit code Serial monitor-3
语言: C++
/*
 * 第04课-串行监视器读取用户输入并打印字符  
 * SunFounder的3合1智能车Arduino套件  
 * 📚⬇️ 下载和资源页面 https://robojax.com/RJT587  
 * 撰写者:Ahmad Shamshiri  
 * 观看完整视频说明 https://youtu.be/rJiSo6sr1hA  
 * www.robojax.com
 */
void setup() {
 Serial.begin(9600);

}

void loop() {
  if(Serial.available() >0 ){
    Serial.print(Serial.read());
    Serial.print("\t");
    Serial.println((char) Serial.read());
  }

}

资源与参考

尚无可用资源。

文件📁

没有可用的文件。