引脚号问题

1即代表D1,D几就是程序中的端口号几

Arduino驱动舵机

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <Servo.h>

Servo myservo; // 定义Servo对象来控制
int pos = 0; // 角度存储变量

void setup() {
myservo.attach(9); // 控制线连接数字9 D9

Serial.begin(9600);
}

void loop() {
for (pos = 0; pos <= 180; pos ++) { // 0°到180°
// in steps of 1 degree
myservo.write(pos); // 舵机角度写入
Serial.print("Hello world.");
delay(5); // 等待转动到指定角度
}
for (pos = 180; pos >= 0; pos --) { // 从180°到0°
myservo.write(pos); // 舵机角度写入
Serial.print("Hello world2.");
delay(5); // 等待转动到指定角度
}
}

Arduino驱动HC-06蓝牙模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <SoftwareSerial.h>
//使用软件串口,能讲数字口模拟成串口
SoftwareSerial BT(4,5); //新建对象,接收脚4为D4,发送5脚为D5
char val; //存储接收的变量

void setup() {
Serial.begin(9600); //与电脑的串口连接
Serial.println("BT is ready!");
BT.begin(9600); //设置波特率
}

void loop() {
//如果串口接收到数据,就输出到蓝牙串口
if (Serial.available()) {
val = Serial.read();
BT.print(val);
}

//如果接收到蓝牙模块的数据,输出到屏幕
if (BT.available()) {
val = BT.read();
Serial.print(val);
}
}

Arduion驱动HC-SR04超声波模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int trigPin = 2;    //Trig d2
int echoPin = 3; //Echo d3
long duration, cm, inches;

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

cm = (duration/2) / 29.1;
Serial.print(cm);
Serial.print("cm");
Serial.println();

delay(300);
}

Arduino驱动OLED

SCL连接A5

SDA连接A4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
void setup() {
Serial.begin(9600);
delay(500);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

display.setTextSize(1); //设置字体大小
display.setTextColor(WHITE); //设置字体颜色
display.setCursor(40,0); //设置起始光标
display.clearDisplay();
display.println("Hello");
display.display();
delay(2000);
display.clearDisplay();
}
String a="";
void loop() {
if(Serial.available()>0)
{
char val=Serial.read();
a+=val;
if(val=='\n'){
display.clearDisplay();
display.setCursor(0,0); //设置起始光标
display.print(a);
display.display();
Serial.println("got");
a="";
}
}
}