/* Uses a for loop to print numbers in various formats. */ void setup() { Serial.begin(9600); // open the serial port at 9600 bps: }
void loop() { // print labels Serial.print("NO FORMAT"); // prints a label Serial.print("\t"); // prints a tab
Serial.print("DEC"); Serial.print("\t");
Serial.print("HEX"); Serial.print("\t");
Serial.print("OCT"); Serial.print("\t");
Serial.print("BIN"); Serial.println(); // carriage return after the last label
for (int x = 0; x < 64; x++) { // only part of the ASCII chart, change to suit // print it out in many formats: Serial.print(x); // print as an ASCII-encoded decimal - same as "DEC" Serial.print("\t\t"); // prints two tabs to accomodate the label lenght
Serial.print(x, DEC); // print as an ASCII-encoded decimal Serial.print("\t"); // prints a tab
Serial.print(x, HEX); // print as an ASCII-encoded hexadecimal Serial.print("\t"); // prints a tab
Serial.print(x, OCT); // print as an ASCII-encoded octal Serial.print("\t"); // prints a tab
Serial.println(x, BIN); // print as an ASCII-encoded binary // then adds the carriage return with "println" delay(200); // delay 200 milliseconds } Serial.println(); // prints another carriage return }
/* Analog input reads an analog input on analog in 0, prints the value out. created 24 March 2006 by Tom Igoe */
int analogValue = 0; // variable to hold the analog value
void setup() { // open the serial port at 9600 bps: Serial.begin(9600); }
void loop() { // read the analog input on pin 0: analogValue = analogRead(0);
// print it out in many formats: Serial.println(analogValue); // print as an ASCII-encoded decimal Serial.println(analogValue, DEC); // print as an ASCII-encoded decimal Serial.println(analogValue, HEX); // print as an ASCII-encoded hexadecimal Serial.println(analogValue, OCT); // print as an ASCII-encoded octal Serial.println(analogValue, BIN); // print as an ASCII-encoded binary
// delay 10 milliseconds before the next reading: delay(10); }
Serial.println("Hello Computer"); Serial1.println("Hello Serial 1"); Serial2.println("Hello Serial 2"); Serial3.println("Hello Serial 3"); } void loop() {}
void loop() { // read from port 0, send to port 1: if (Serial.available()) { int inByte = Serial.read(); Serial1.print(inByte, DEC); } // read from port 1, send to port 0: if (Serial1.available()) { int inByte = Serial1.read(); Serial.print(inByte, DEC); } }
Serial.read()
描述 读取传入串行数据。 Serial.read()从 Stream 实用程序类继承。
语法 Serial.read();
参数 Serial:串行端口对象。请参阅串行主页上每个主板的可用串行端口列表。
返回值 传入串行数据的第一个字节可用(如果没有数据可用,或 -1)。数据类型: .int
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int incomingByte = 0; // for incoming serial data
void setup() { Serial.begin(9600); // opens serial port, sets data rate to 9600 bps }
void loop() { // send data only when you receive data: if (Serial.available() > 0) { // read the incoming byte: incomingByte = Serial.read();
// say what you got: Serial.print("I received: "); Serial.println(incomingByte, DEC); } }
Serial.peek()
描述 返回传入串行数据的下一个字节(字符),而不将其从内部串行缓冲区中删除。
Serial.peek()从 Stream 实用程序类继承。
语法 Serial.peek();
参数 Serial:串行端口对象。请参阅串行主页上每个主板的可用串行端口列表。
返回值 传入串行数据的第一个字节可用(如果没有数据可用,或 -1)。数据类型:.int
示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
char incomingByte; // for incoming serial data
void setup() { Serial.begin(9600); // opens serial port, sets data rate to 9600 bps }
void loop() { // send data only when you receive data: if (Serial.available() > 0) { // read the incoming byte: incomingByte = Serial.peek();
// say what you got: # Serial.print("I received: "); Serial.printf("%c", incomingByte); } }