arduino的舵机代码,备案记录
2023-05-20 21:55:47 来源:哔哩哔哩

#include <Servo.h>


(资料图)

Servo servo1;

Servo servo2;

const int motorPin1 = 3; // IN1 on the L298

const int motorPin2 = 4; // IN2 on the L298

const int enablePin = 5; // ENA on the L298

void setup() {

servo1.attach(9);

servo2.attach(10);

Serial.begin(9600);

pinMode(motorPin1, OUTPUT);

pinMode(motorPin2, OUTPUT);

pinMode(enablePin, OUTPUT);

servo1.write(90);

servo2.write(90);

}

void loop() {

if (Serial.available()) {

char ch = Serial.read();

if (ch == 'Q') {

servo2.write(135);

delay(500);

servo1.write(135);

delay(900);

servo1.write(90);

delay(500);

servo2.write(90);

delay(500);

}

if (ch == 'K') {

servo2.write(55);

delay(500);

servo1.write(60);

delay(900);

servo1.write(90);

delay(500);

servo2.write(90);

delay(500);

digitalWrite(motorPin1, HIGH);

digitalWrite(motorPin2, LOW);

analogWrite(enablePin, 100); // slower speed

delay(2000); // rotate for 1 second

digitalWrite(motorPin1, LOW);

digitalWrite(motorPin2, HIGH);

analogWrite(enablePin, 100); // slower speed

delay(2000); // rotate for 1 second

analogWrite(enablePin, 0);

}

if (ch == 'C') {

servo2.write(120);

delay(500);

servo1.write(45);

delay(900);

servo1.write(90);

delay(500);

servo2.write(90);

delay(500);

}

if (ch == 'Y') {

servo2.write(60);

delay(500);

servo1.write(135);

delay(900);

servo1.write(90);

delay(500);

servo2.write(90);

delay(500);

}

}

}

标签:

下一篇: 陕西煤业简介
上一篇: 张颂文代言背后:77.75亿元库存压力下,海澜之家只想走得稳?|全球速看

相关新闻

保险时讯