52 lines
1.7 KiB
Arduino
52 lines
1.7 KiB
Arduino
|
int out_1_pin = 26;
|
||
|
int out_2_pin = 27;
|
||
|
|
||
|
bool LEDState = HIGH;
|
||
|
void setup() {
|
||
|
// initialize digital pin LED_BUILTIN as an output.
|
||
|
Serial.begin(115200);
|
||
|
pinMode(out_1_pin, OUTPUT);
|
||
|
pinMode(out_2_pin, OUTPUT);
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
void motor_run(){
|
||
|
digitalWrite(out_1_pin, LOW); // 方向
|
||
|
for (int i = 0; i < 200; i++) {
|
||
|
digitalWrite(out_2_pin, HIGH); // 将引脚设置为高电平
|
||
|
delay(10); // 等待10毫秒
|
||
|
digitalWrite(out_2_pin, LOW); // 将引脚设置为低电平
|
||
|
delay(10); // 等待10毫秒
|
||
|
}
|
||
|
}
|
||
|
void motor_36_run(){
|
||
|
digitalWrite(out_1_pin, LOW); // 方向
|
||
|
for (int i = 0; i < 20; i++) {
|
||
|
digitalWrite(out_2_pin, HIGH); // 将引脚设置为高电平
|
||
|
delay(10); // 等待10毫秒
|
||
|
digitalWrite(out_2_pin, LOW); // 将引脚设置为低电平
|
||
|
delay(10); // 等待10毫秒
|
||
|
}
|
||
|
|
||
|
}
|
||
|
// the loop function runs over and over again forever
|
||
|
void loop() {
|
||
|
if (Serial.available() > 0) { // 检查是否有可用的串行数据
|
||
|
char receivedData[64]; // 用于存储接收到的数据
|
||
|
int dataSize = Serial.readBytesUntil('\n', receivedData, sizeof(receivedData)); // 读取串行数据直到遇到换行符为止
|
||
|
receivedData[dataSize] = '\0'; // 添加字符串结束符
|
||
|
Serial.print("Received: "); // 打印接收到的消息
|
||
|
Serial.println(receivedData); // 打印接收到的数据
|
||
|
if(strcmp(receivedData, "move_all") == 0) {
|
||
|
motor_run();
|
||
|
Serial.println("run_all"); // 打印接收到的数据
|
||
|
}
|
||
|
if(strcmp(receivedData, "move_36") == 0) {
|
||
|
motor_36_run();
|
||
|
Serial.println("run_36"); // 打印接收到的数据
|
||
|
}
|
||
|
}
|
||
|
delay(10);
|
||
|
}
|