作者:Greddy | 更新时间:2019-05-20 | 浏览量:961
这是一个自己做的智能车,有循迹避障功能。可以用贝壳互联控制小车前进后退,但是切换到循迹就只能动一下,有啥办法让他一直动吗
String DEVICEID="3935"; // 你的设备编号==
String APIKEY="b11db05cf"; // 设备密码==
//==================================
const int LED2 = 9; // LED正极连接针脚 左轮子IN1
const int LED3 = 10;// LED正极连接针脚 左轮子IN2
const int LED4 = 11;// LED正极连接针脚 右轮子IN3
const int LED5 = 12;// LED正极连接针脚 右轮子IN4
const int LED6 = 13;// LED正极连接针脚 鸣笛
unsigned long lastCheckInTime = 0; //记录上次报到时间
const unsigned long postingInterval = 40000; // 每隔40秒向服务器报到一次
int pxq; //定义变量用于将C字符串变成pxq整数型
String inputString = "";
boolean stringComplete = false;
boolean CONNECT = true;
char* parseJson(char *jsonString);
void setup() {
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);
Serial.begin(115200);
delay(5000);
}
void loop() {
if(millis() - lastCheckInTime > postingInterval || lastCheckInTime==0) {
checkIn();
}
serialEvent();
if (stringComplete) {
inputString.trim();
//Serial.println(inputString);
if(inputString=="CLOSED"){
Serial.println("connect closed!");
CONNECT=false;
}else{
int len = inputString.length()+1;
if(inputString.startsWith("{")&& inputString.endsWith("}")){
char jsonString[len];
inputString.toCharArray(jsonString,len);
aJsonObject *msg = aJson.parse(jsonString);
processMessage(msg);
aJson.deleteItem(msg);
}
}
// clear the string:
inputString = "";
stringComplete = false;
}
}
void checkIn() {
if (!CONNECT) {
Serial.print("+++");
delay(500);
Serial.print("\r\n");
delay(1000);
Serial.print("AT+RST\r\n");
delay(6000);
CONNECT=true;
lastCheckInTime=0;
}
else{
Serial.print("{\"M\":\"checkin\",\"ID\":\"");
Serial.print(DEVICEID);
Serial.print("\",\"K\":\"");
Serial.print(APIKEY);
Serial.print("\"}\r\n");
lastCheckInTime = millis();
}
}
void processMessage(aJsonObject *msg){
aJsonObject* method = aJson.getObjectItem(msg, "M");
aJsonObject* content = aJson.getObjectItem(msg, "C");
aJsonObject* client_id = aJson.getObjectItem(msg, "ID");
//char* st = aJson.print(msg);
if (!method) {
return;
}
//Serial.println(st);
//free(st);
String M=method->valuestring;
String C=content->valuestring;
String F_C_ID=client_id->valuestring;
if(M=="say"){
if(C=="1001"){ //前
digitalWrite(LED2, LOW);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, LOW);
digitalWrite(LED5, HIGH);
sayToClient(F_C_ID,"qian on!");
}
if(C=="2001"){ //后
digitalWrite(LED2, HIGH);
digitalWrite(LED3, LOW);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, LOW);
sayToClient(F_C_ID,"hou on!");
}
if(C=="1002"){ //停止
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
sayToClient(F_C_ID,"ting off!");
}
if(C=="3001"){ //左
digitalWrite(LED2, HIGH);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, HIGH);
sayToClient(F_C_ID,"zuo on!");
}
if(C=="4001"){ //右
digitalWrite(LED2, LOW);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, LOW);
sayToClient(F_C_ID,"you on!");
}
if(C=="5001"){ //鸣笛
digitalWrite(LED6, HIGH);
sayToClient(F_C_ID,"mingdi on!");
}
if(C=="2002"){ //静音
digitalWrite(LED6, LOW);
sayToClient(F_C_ID,"mingdi off!");
}
}
}
void sayToClient(String client_id, String content){
Serial.print("{\"M\":\"say\",\"ID\":\"");
Serial.print(client_id);
Serial.print("\",\"C\":\"");
Serial.print(content);
Serial.print("\"}\r\n");
lastCheckInTime = millis();
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == '\n') {
stringComplete = true;
}
}
}
上面是程序,我想在它等于2003时一直执行循迹程序,但是没法做到,有啥办法吗