驱动代码备份

备份,方便修改复制

12

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
// 控制 7 个电机的继电器引脚(高电平触发)
const int motorPins[7] = {1,2,3,4,5,6,7};

void setup() {
// 初始化每个继电器引脚为输出
for (int i = 0; i < 7; i++) {
pinMode(motorPins[i], OUTPUT);
digitalWrite(motorPins[i], LOW); // 初始状态全关闭(低电平)
}

// 启动所有电机
for (int i = 0; i < 7; i++) {
digitalWrite(motorPins[i], HIGH); // 高电平触发,继电器闭合
}

delay(12500); // 所有电机运行 12.5 秒

// 关闭所有电机
for (int i = 0; i < 7; i++) {
digitalWrite(motorPins[i], LOW); // 断开继电器
}
}

void loop() {
// 不再重复执行
}

伸缩模式:

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
const int motorPins[5] = {12456};
void setup() {
// 初始化每个继电器引脚为输出
for (int i = 0; i < 5; i++) {
pinMode(motorPins[i], OUTPUT);
digitalWrite(motorPins[i], LOW); // 初始状态全关闭(低电平)
}

// 启动所有电机
for (int i = 0; i < 5; i++) {
digitalWrite(motorPins[i], HIGH); // 高电平触发,继电器闭合
}

delay(12500); // 所有电机运行 12.5 秒 100/4=25s

// 关闭所有电机
for (int i = 0; i < 5; i++) {
digitalWrite(motorPins[i], LOW); // 断开继电器
}
}

void loop() {
// 不再重复执行
}

弯曲模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const int motorPins[3] = {246};
void setup() {
// 初始化每个继电器引脚为输出
for (int i = 0; i < 3; i++) {
pinMode(motorPins[i], OUTPUT);
digitalWrite(motorPins[i], LOW); // 初始状态全关闭(低电平)
}

// 启动所有电机
for (int i = 0; i < 3; i++) {
digitalWrite(motorPins[i], HIGH); // 高电平触发,继电器闭合
}

delay(12500); // 所有电机运行 12.5 秒 100/4=25s

// 关闭所有电机
for (int i = 0; i < 3; i++) {
digitalWrite(motorPins[i], LOW); // 断开继电器
}
}

void loop() {
// 不再重复执行
}

多电机控制1,2,3,4,5,6,7

伸缩模式:25000,25000,0,25000,25000,25000,0

上弯模式:0,12500,0,50,0,12500,0 //注意调换电机 2电机反向 4反向

下弯模式:0,12500,0,50,0,12500,0

后掠模式:0,7500,0,12250,0,7500,0 // 正向 正向

前掠模式:0,12500,0,10250,0,12500,0 // 反向 反向

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
37
38
const int motorPins[7] = {1234567}; // 继电器引脚
const unsigned long motorDurations[7] = {
12000, 25000, 8000, 18000, 15000, 3000, 10000 // 每个电机的运行时间(毫秒)
};

bool motorStopped[7] = {false, false, false, false, false, false, false};
unsigned long startTime;

void setup() {
for (int i = 0; i < 7; i++) {
pinMode(motorPins[i], OUTPUT);
digitalWrite(motorPins[i], HIGH); // 同时开启所有继电器(高电平触发)
}
startTime = millis();
}

void loop() {
unsigned long currentTime = millis();
unsigned long elapsed = currentTime - startTime;

bool allStopped = true;

for (int i = 0; i < 7; i++) {
if (!motorStopped[i]) {
allStopped = false;
if (elapsed >= motorDurations[i]) {
digitalWrite(motorPins[i], LOW); // 关闭继电器
motorStopped[i] = true;
}
}
}

if (allStopped) {
// 所有电机都停了,永远停止 loop
while (true);
}
}