Arduino编程之Serial.println()和Serial.print()

Arduino编程之Serial.println()和Serial.print()

Arduino 的输出基本就用两个函数 print 和 println,区别在于后者比前者多了回车换行

Serial.println(data)
从串行端口输出数据,跟随一个回车(ASCII 13, 或 'r')和一个换行符(ASCII 10, 或 'n')。这个函数所取得的值与 Serial.print()一样。
Serial.println(b) 以十进制形式输出b的ASCII编码值,并同时跟随一个回车和换行符。
Serial.println(b, DEC) 以十进制形式输出b的ASCII编码值,并同时跟随一个回车和换行符。
Serial.println(b, HEX) 以十六进数据形式输出b的ASCII编码值,并同时跟随一个回车和换行符。
Serial.println(b, OCT)以八进数据形式输出b的ASCII编码值,并同时跟随一个回车和换行符。
Serial.println(b, BIN)以二进数据形式输出b的ASCII编码值,并同时跟随一个回车和换行符。
Serial.println(b, BYTE)以单个字节输出b,并同时跟随一个回车和换行符。
Serial.println(str)如果 str是一个字符串或数组,输出整个 str的 ASCII编码字符串。
Serial.println()仅输出一个回车和换行符。
参数
b:需要输出的字节。
str:需要输出的字符串。
Returns
None

 
/*
模拟输入
在0号模拟插口读取一个模拟值,并输出。
created 24 March 2006 by Tom Igoe
*/
int analogValue = 0;    // 需要保留的模拟值
 
 
void setup() {
//在9600 bps打开串行端口:
Serial.begin(9600);
}
void loop() {
// 在0号模拟输入插口读取值
analogValue = analogRead(0);
// 以多种格式输出
Serial.println(analogValue);
// 以ASCII编码十进制浮点值输出
Serial.print(analogValue, DEC);
// 以ASCII编码十进制浮点值输出
Serial.println(analogValue, HEX);
// 以ASCII编码十六进制输出
Serial.println(analogValue, OCT);
// 以ASCII编码八进制输出
Serial.println(analogValue, BIN);
// 以ASCII编码二进制输出
// 在下次读取之前延迟10毫秒
delay(10);
}

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
主程序: #include "gyro.h" #include "pid.h" #include "motor.h" #define dt 10 // in ms /* Object definitions and settings */ Gyro myGyro(dt); PIDControl pid(1.0,0,0.005,-255,255,dt); OmniMotorDriver omd; /* Deadzone definition. With a rotation of less than this value, * the robot will stand still */ double deadzone = 1.5; /* Buffers for timing, gyro data and PID outputs */ long lastMillis; double xAngle, yAngle; double xOut, yOut; /* Main setup call * Initializes Serial, the IMU and prepares the motor driver, * calbriates the IMU and enables the PID controller */ void setup() { pinMode(13, OUTPUT); Serial.begin(115200); if(!myGyro.init()) { while(1) { digitalWrite(13, HIGH); delay(1000); digitalWrite(13, LOW); delay(2000); } } myGyro.calibrate(); omd.init(); pid.enable(); } /* Main loop * The main loop connects all the libraries and classes together, * in that it describes the order of function calls that is necessary * for the function of the robot */ void loop() { /* Save the current point in time in lastMillis */ lastMillis = millis(); /* Get Data from Gyro */ myGyro.update(&xAngle, &yAngle); /* Compute PID Output with the data */ pid.compute(xAngle, yAngle, &xOut, &yOut); /* Plot Angle on Serial Plotter Serial.print(xAngle); Serial.print(" "); Serial.print(yAngle); Serial.println(" "); */ /* Plot Angle on Serial Plotter Serial.print(xOut); Serial.print(" "); Serial.println(yOut); */ /* If any calculated angle is larger than the deadzone */ if(abs(xAngle) > deadzone || abs(yAngle) > deadzone) { /* Actually drive the motors */ omd.drive(xOut, yOut); /* Otherwise, stop them */ } else omd.stop(); /* Wait until the next dt cycle * if processing took less than dt, wait for the remaining time, * otherwise wait for the next full cycle to start * This is needed for gyro and PID accuracy */ int pTime = millis() - lastMillis; int resttime = (dt > pTime
Arduino对智能机器⼈进⾏简单编程 对智能机器⼈进⾏简单编程 Arduino对智能机器⼈进⾏简单编程 对智能机器⼈进⾏简单编程 #include "ESP8266.h" #define TRIG_PIN 5 #define ECHO_PIN A5 #define BEEP_PIN 12 #define BEEP_ON LOW #define BEEP_OFF HIGH #define CH_PD 8 #define OK_BEEP_TIME 200 #define ERROR_BEEP_TIME 1000 ESP8266 wifi = ESP8266(Serial);//串⼝ void check_esp8266_cmd(){ char buff[10]; if(wifi.available()>0&&wifi.read(buff,10)>0){ switch(buff[0]){ case 'w' : Serial.print("$DGT:2-7,0!"); break; case 's' : Serial.print("$DGT:8-13,0!"); break; case 'a': Serial.print("$DGT:14-17,0!"); break; case 'd' : Serial.print("$DGT:17-20,0!"); break; } } }//wifi控制 void chk_status() { int x,y; x=analogRead(A7); y=analogRead(A6); Serial.print("x="); Serial.print(x); Serial.print(",y="); Serial.println(y); if(x>310&&x<320&&y>320&&y<332) { Serial.print("$DGT:33-35,0!"); }//前倒爬起 if(x>330&&x<342&&y>320&&y<335) { Serial.print("$DGT:36-38,0!"); }//后倒爬起 if(x>325&&x<335&&y>315&&y<320) { Serial.print("$DGT:45-50,0!"); }//左侧倒爬起 if(x>325&&x<335&&y>340&&y<350) { Serial.print("$DGT:51-55,0!"); }//右侧倒爬起 delay(2000); }//姿态判断 void chk_distance() { unsigned long d=0; int i; for(i=0;i<=5;i++) { digitalWrite(TRIG_PIN,LOW); digitalWrite(TRIG_PIN,HIGH); delayMicroseconds(12); digitalWrite(TRIG_PIN,LOW);//脉冲信号 d+=pulseIn(ECHO_PIN,HIGH)/58; delay(80); } d/=5; Serial.print("d="); Serial.println(d); if(d<50){ beep(100); Serial.println("$DGS:2!"); } } void beep(int t){ digitalWrite(BEEP_PIN,BEEP_ON); delay(t); digitalWrite(BEEP_PIN,BEEP_OFF); } //蜂鸣器 void chk_result(int s) { if(s==ESP8266_COMMAND_OK) { beep(OK_BEEP_TIME); } else { beep(ERROR_BEEP_TIME); } } void init_esp8266() { pinMode(CH_PD,OUTPUT); digitalWrite(CH_PD,HIGH); delay(500); auto s = wifi.test(); chk_result(s); s = wifi.setMode(ESP8266_WIFI_ACCESSPOINT); s = wifi.setAPConfiguration("esp8266", "12345678", 10,ESP8266_ENCRYPTION_WPA_WPA2_PSK);//wifi 名称 密码 s = wifi.setMultipleConnections(true); s = wifi.createServer(5000); } void setup() { // put your setup code here, to run once: pinMode(BEEP_PIN,OUT

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值