servo_led共用pin
舵机和LED使用了同一个GPIO,但他们确实可以正常使用。
原因是引脚只是给出一定脉宽的信号,而舵机和LED分别对这个信号做出响应。
20ms的脉冲周期,各自脉宽的表现
-
LED在20ms时熄灭,随着脉宽减小,越来越亮。
-
舵机在0.5ms至2.5ms之间工作,其余部分都不工作
因此只要设置LED亮度时避开舵机工作区间即可。
用digitalWrite
时LED只能被设置为最亮或熄灭,避开了舵机工作区间。
若使用analogWrite
控制LED亮度,同理只要避开舵机区间即可。
代码见下:(110行有测试)
copy
- 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
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
//WiFi config
const char* ssid = "Tenda_D7BEA8";
const char* pwd = "********";
//mqtt config
const char* mqtt_broker = "192.168.0.103";
const int mqtt_port = 1883;
const char* topic = "/lock";
WiFiClient espclient;
PubSubClient client(espclient); //必须传一个client作为参数
//servo config
Servo servo;
//LED config
void setup() {
analogWriteFreq(50);
analogWriteRange(200); //0.1ms
Serial.begin(115200);
Serial.println();
//LED
analogWrite(LED_BUILTIN, led_ON); //注意LED_BUILTIN也是GPIO2
//servo
servo.attach(servo_pin, 500, 2500);
servo.write(servo_OFF);
delay(100);
//wifi
connect_wifi();
//mqtt
client.setServer(mqtt_broker, mqtt_port);
client.setCallback(callback);
connect_mqtt();
client.subscribe(topic);
}
void loop() {
client.loop();
}
void connect_wifi() {
analogWrite(LED_BUILTIN, led_ON);
Serial.printf("connecting to %s......", ssid);
WiFi.begin(ssid, pwd);
while (!WiFi.isConnected()) {
delay(500);
}
Serial.println("connected");
analogWrite(LED_BUILTIN, led_OFF);
}
void reconnect_wifi() {
Serial.printf("reconnecting to %s......", ssid);
analogWrite(LED_BUILTIN, led_ON);
WiFi.disconnect(); //清楚连接信息,防止路由器重启后信道变化
WiFi.begin(ssid, pwd);
while (!WiFi.isConnected()) {
delay(500);
}
Serial.println("connected");
analogWrite(LED_BUILTIN, led_OFF);
delay(20);
}
void connect_mqtt() {
analogWrite(LED_BUILTIN, led_ON);
while (!client.connected()) {
String client_id = "esp8266-client-" + String(WiFi.macAddress());
Serial.printf("client %s is connecting to broker %s ... ", client_id.c_str(), mqtt_broker);
if (client.connect(client_id.c_str())) {
Serial.println("connected");
delay(20);
} else {
Serial.printf("failed with state %d\n", client.state());
delay(2000);
}
}
analogWrite(LED_BUILTIN, led_OFF);
delay(20);
}
void callback(char *topic, uint8_t *payload, unsigned int length) {
analogWrite(LED_BUILTIN, led_ON);
String msg = "";
for (unsigned int i = 0; i < length; i++) {
msg += (char)payload[i];
}
Serial.printf("receive message from topic %s: %s\n", topic, msg.c_str());
act_on_msg(msg);
Serial.println("--------------------------");
// delay(1000);
// analogWrite(LED_BUILTIN, 5); //这里本只想控制灯但是这个脉宽在舵机控制范围内,所以它被驱动
analogWrite(LED_BUILTIN, led_OFF);
}
void act_on_msg(String msg) {
if (msg.equals("open_door")) {
servo.write(servo_ON);
delay(2000);
}
servo.write(servo_OFF);
delay(200);
}
本文作者:faf4r
本文链接:https://www.cnblogs.com/faf4r/p/18107194
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步