5234| 0
|
[求助问答] 请问使用红外遥控,但是在接收到红外信号后程序不运行ze... |
本帖最后由 是我夕立哒 于 2021-5-8 15:57 编辑 使用红外遥控作为开关以及部分功能的遥控,在启动自动跟随后程序无法运行,并且其他动作在通过遥控执行一次后就无法在重复执行,并且自动跟随也无法使用,各位大佬帮忙看看是什么问题则么解决,谢谢! 单独在主程序中去掉函数(距离)就没问题。。 代码 *! * MindPlus * mega2560 * */ #include <DFRobot_IRremote.h> #include <DFRobot_Libraries.h> // 动态变量 String mind_s_HongWai; volatile float mind_n_Men, mind_n_index_L, mind_n_index_R, mind_n_switch; // 函数声明 void DF_JuLi(); void DF_ZuoZhengZhuan(float mind_n_ZuoSu); void DF_KaiMen(); void DF_ZuoFanZhuan(float mind_n_ZuoSu); void DF_GuanMen(); void DF_ZuoTing(); void DF_YouZhengZhuan(float mind_n_YouSu); void DF_YouFanZhuan(float mind_n_YouSu); void DF_YouTing(); // 创建对象 IRremote_Receive remoteReceive_2; DFRobot_Servo360 servo360_22; DFRobot_Servo360 servo360_23; byte comdataL[3] = {}; //接受左边传感器的数据 int mind_n_DistanceL = 6800; //左边小车的距离发送模块的距离 byte comdataR[3] = {}; //接受右边传感器的数据 int mind_n_DistanceR = 6800; //右边小车的距离发送模块的距离 // 主程序开始 void setup() { Serial.begin(9600); Serial1.begin(115200); Serial2.begin(115200); servo360_22.attach(22); servo360_23.attach(23); } void loop() { mind_s_HongWai = (remoteReceive_2.getIrCommand(2)); if ((mind_s_HongWai==String("FFA857"))) { mind_n_switch = 0; } if ((mind_s_HongWai==String("FFE01F"))) { mind_n_switch = 1; } if (((mind_s_HongWai==String("FF30CF")) && (mind_n_switch==1))) { DF_KaiMen(); } if (((mind_s_HongWai==String("FF18E7")) && (mind_n_switch==1))) { DF_GuanMen(); } if (((mind_n_Men==0) && (mind_n_switch==1))) { delay(10); DF_JuLi(); Serial.print(mind_n_DistanceL); Serial.print(" "); Serial.println(mind_n_DistanceR); if (((mind_n_DistanceR + mind_n_DistanceL)>=13400)) { DF_YouTing(); DF_ZuoTing(); } else { if (((abs((mind_n_DistanceL - mind_n_DistanceR)))<=50)) { DF_ZuoZhengZhuan(150); DF_YouZhengZhuan(150); } else { if (((mind_n_DistanceL - mind_n_DistanceR)>0)) { DF_ZuoZhengZhuan(150); DF_YouFanZhuan(150); } else { DF_YouZhengZhuan(150); DF_ZuoFanZhuan(150); } } } } Serial.print("switch:"); Serial.print(mind_n_switch); Serial.print("\"门\""); Serial.println(mind_n_Men); } void DF_JuLi() { mind_n_DistanceL =6800; mind_n_DistanceR =6800; static byte mind_n_index_L = 0, mind_n_index_R = 0; while (Serial1.available()) //如果串口数据可用 { if (mind_n_index_L == 0) { comdataL[mind_n_index_L] = Serial1.read();//把获取传感器的数据存放到数组里面 if (comdataL[mind_n_index_L] == 0xA5) //判断数据的头文件 mind_n_index_L++; } else { comdataL[mind_n_index_L++] = Serial1.read(); } if (mind_n_index_L >= 3) { mind_n_index_L = 0; mind_n_DistanceL = comdataL[1] << 8 | comdataL[2]; //把收到的数据合成为距离值 } } while (Serial2.available()) //如果串口数据可用 { if (mind_n_index_R == 0) { comdataR[mind_n_index_R] = Serial2.read();//把获取传感器的数据存放到数组里面 if (comdataR[mind_n_index_R] == 0xA5) //判断数据的头文件 mind_n_index_R++; } else { comdataR[mind_n_index_R++] = Serial2.read(); } } if (mind_n_index_R >= 3) { mind_n_index_R= 0; mind_n_DistanceR = comdataR[1] << 8 | comdataR[2]; //把收到的数据合成为距离值 } } void DF_ZuoTing() { analogWrite(13, 0); analogWrite(12, 0); } void DF_ZuoZhengZhuan(float mind_n_ZuoSu) { analogWrite(12, mind_n_ZuoSu); analogWrite(13, mind_n_ZuoSu); digitalWrite(50, LOW); digitalWrite(51, HIGH); digitalWrite(52, LOW); digitalWrite(53, HIGH); } void DF_GuanMen() { // 22右 // 23左 delay(10); servo360_22.speed(90); servo360_23.speed(-90); delay(600); servo360_22.speed(0); servo360_23.speed(0); mind_n_Men = 0; } void DF_ZuoFanZhuan(float mind_n_ZuoSu) { analogWrite(12, mind_n_ZuoSu); analogWrite(13, mind_n_ZuoSu); digitalWrite(50, HIGH); digitalWrite(51, LOW); digitalWrite(52, HIGH); digitalWrite(52, LOW); } void DF_KaiMen() { delay(10); servo360_22.speed(-90); servo360_23.speed(90); delay(600); servo360_22.speed(0); servo360_23.speed(0); mind_n_Men = 1; } void DF_YouTing() { analogWrite(10, 0); analogWrite(11, 0); } void DF_YouZhengZhuan(float mind_n_YouSu) { analogWrite(11, mind_n_YouSu); analogWrite(10, mind_n_YouSu); digitalWrite(46, LOW); digitalWrite(47, HIGH); digitalWrite(48, LOW); digitalWrite(49, HIGH); } void DF_YouFanZhuan(float mind_n_YouSu) { analogWrite(11, mind_n_YouSu); analogWrite(10, mind_n_YouSu); digitalWrite(46, HIGH); digitalWrite(47, LOW); digitalWrite(48, HIGH); digitalWrite(49, LOW); } |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed