该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
这是我改完之后的代码。 我想先实现通过L298N对两个马达的控制。
#include
#include
#include
#include
#define LW_USERKEY "嘿嘿嘿"
#define LW_GATEWAY "01"
#define LED_PIN 12//plug led in the d3,gnd->gnd
#define p1 2
#define p2 3
#define p3 4
#define p4 5
LeweiTcpClient *client;
void setup()
{
Serial.begin(9600);
client = new LeweiTcpClient(LW_USERKEY, LW_GATEWAY);
UserFunction uf2 (ledOn, "l1");
client->addUserFunction(uf2);
UserFunction uf3 (ledOff, "l2");
client->addUserFunction(uf3);
client->easySetupMode(true);
pinMode(LED_PIN, 1);
pinMode(p1, 1);
pinMode(p2, 1);
pinMode(p3, 1);
pinMode(p4, 1);
digitalWrite(p1, 0);
digitalWrite(p2, 0);
digitalWrite(p3, 0);
digitalWrite(p4, 0);
warning();
}
void warning() {
for (int i = 1; i <= 3; i++) {
digitalWrite(LED_PIN, 1);
delay(200);
digitalWrite(LED_PIN, 0);
delay(200);
}
}
//=====================motor control=====================
void runA(int mode, int tt) {
switch (mode) {
case 1:
digitalWrite(p1, 1);
digitalWrite(p2, 0);
break;
case 0:
digitalWrite(p1, 0);
digitalWrite(p2, 0);
break;
case -1:
digitalWrite(p1, 0);
digitalWrite(p2, 1);
};
delay(tt);
}
void runB(int mode, int tt) {
switch (mode) {
case 1:
digitalWrite(p4, 1);
digitalWrite(p3, 0);
break;
case 0:
digitalWrite(p4, 0);
digitalWrite(p3, 0);
break;
case -1:
digitalWrite(p4, 0);
digitalWrite(p3, 1);
};
delay(tt);
}
//=====================operate function=====================
void ledOn()
{
client->setRevCtrlMsg("true", "<
warning();
runA(1, 5000);
runA(0, 0);
}
void ledOff()
{
client->setRevCtrlMsg("true", "-->>");
warning();
runB(1, 5000);
runB(0, 0);
}
//=====================main function=====================
void loop()
{
client->keepOnline();
}