arduino print Hello world

Hello world experiment

Purpose

Use arduino's serial port to write "Hello world", and then use arduino to send to PC

Schematic diagram

Insert picture description here

Code writing

void setup() {
    
    
  // put your setup code here, to run once:
  Serial.begin(9600);//Serial.begin(参数)参数为设置串口通讯的波特率,这里设置为9600
}

void loop() {
    
    
  // put your main code here, to run repeatedly:
  Serial.print("Hello world");//打印Hello world
  /*
  Serial.print(参数1,参数2);
  向串口输出的函数
  参数1:打印的值,可以是任意的数据类型
  参数2:输出的数据的格式,包括整型数据和浮点型数据的小数点位数
  如果也要进行换行操作可以Serial.print("Hello world\n");加上\n
  */
  /*
  Serial.println(参数1,参数2);
  向串口输出的函数,与Serial.print(参数1,参数2);不同的是输出的数据带有回车符,可以自动换行
  参数1:打印的值,可以是任意的数据类型
  参数2:输出的数据的格式,包括整型数据和浮点型数据的小数点位数
  */
  delay(1000);//delay(参数);毫秒延时函数延时1000毫秒即延时1秒
}

Usage of Serial.print() function

void setup() {
    
    
  // put your setup code here, to run once:
  Serial.begin(9600);//Serial.begin(参数)参数为设置串口通讯的波特率,这里设置为9600
}

void loop() {
    
    
  // put your main code here, to run repeatedly:
  Serial.print(78,BIN);
  delay(1000);//delay(参数);毫秒延时函数延时1000毫秒即延时1秒
  Serial.print(78,OCT);
  delay(1000);//delay(参数);毫秒延时函数延时1000毫秒即延时1秒
  Serial.print(78,DEC);
  delay(1000);//delay(参数);毫秒延时函数延时1000毫秒即延时1秒
  Serial.print('N');
  delay(1000);//delay(参数);毫秒延时函数延时1000毫秒即延时1秒
  Serial.print(1.23457,2);//小数点后两位
  delay(1000);//delay(参数);毫秒延时函数延时1000毫秒即延时1秒
  Serial.print(1.23457,4);//小数点后四位
  delay(1000);//delay(参数);毫秒延时函数延时1000毫秒即延时1秒
}

There is a function to output data to the serial port, there will be a function to output data to the serial port

void setup() {
    
    
  // put your setup code here, to run once:
  Serial.begin(9600);//Serial.begin(参数)参数为设置串口通讯的波特率,这里设置为9600
}

void loop() {
    
    
  /*
  Serial.wirte(参数1,参数2,参数3,参数4);写二进制数据到串口
  参数1:一个字节
  参数2:一串字节
  参数3:字节数组
  参数4:字节数组的长度
  */
  Serial.println("Hello world");
  Serial.write(45);//输出的不是45,45代表ASCII码的值,输出的是45对应的ASCII码
  Serial.print("\n");
  delay(1000);
}

Function explanation

Void setup() is used to write the initialization content of arduino, and put the communication baud rate here. For the communication setting, you only need to set it once
. The parameter Serial.begin (parameter) is to set the baud rate of serial communication. Here is set to 9600
void loop() is the main part of the arduino program, here a string is sent to the computer via the serial port
Serial.printIn (parameter); used to fill in the content of the string to be sent
delay (parameter); millisecond delay Function delay of 1000 milliseconds is 1 second

Guess you like

Origin blog.csdn.net/qq_45671732/article/details/108893102