MCU/CODE/Arduino_MOTOR_0417/Arduino_MOTOR_0417.ino

51 lines
1.6 KiB
Arduino
Raw Normal View History

2024-04-19 11:11:09 +08:00
bool LEDState = HIGH;
void setup() {
// initialize digital pin LED_BUILTIN as an output.
Serial.begin(115200);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
digitalWrite(12, HIGH);
}
void motor_run(){
digitalWrite(12, LOW); // 方向
for (int i = 0; i < 200; i++) {
digitalWrite(13, HIGH); // 将引脚设置为高电平
delay(10); // 等待10毫秒
digitalWrite(13, LOW); // 将引脚设置为低电平
delay(10); // 等待10毫秒
}
}
void motor_36_run(){
digitalWrite(12, LOW); // 方向
for (int i = 0; i < 20; i++) {
digitalWrite(13, HIGH); // 将引脚设置为高电平
delay(10); // 等待10毫秒
digitalWrite(13, 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);
}