博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
电机+1舵机+红外控制+灯+LCD
阅读量:5101 次
发布时间:2019-06-13

本文共 4668 字,大约阅读时间需要 15 分钟。

 

#include 
#include
#include
#include
// IRremote库声明 #include
//ProtoThreads必须包含的头文件#include
#include
#include
#include
#define PT_USE_TIMER#define PT_USE_SEMServo myservo;LiquidCrystal_I2C lcd(0x20,2,1,0,4,5,6,7);int input1 = 9; // 定义uno的pin 5 向 input1 输出int input2 = 10; // 定义uno的pin 6 向 input2 输出int input3 = 5; // 定义uno的pin 9 向 input3 输出int input4 = 6; // 定义uno的pin 10 向 input4 输出int input7 = 7; // 定义uno的pin 7 向 input7 输出int pos = 20;int LED = 4;int RECV_PIN = 3;IRrecv irrecv(RECV_PIN);decode_results results;static struct pt thread1;//thread onelong control[7][3] = { //遥控器矫正数字 { 16580863, 16613503, 16597183}, { 16589023, 16621663, 16605343}, { 16584943, 16617583, 16601263}, { 16593103, 16625743, 16609423}, { 16582903, 16615543, 16599223}, { 16591063, 16623703, 16607383}, { 16586983, 16619623, 16603303}};void initRun() { // init pinMode(input1, OUTPUT); pinMode(input2, OUTPUT); pinMode(input3, OUTPUT); pinMode(input4, OUTPUT); initLeftAndRight();}void initLight() { //init light pinMode(LED, OUTPUT);}void getLeftAndRight_left() { for (pos = 30; pos < 90; pos += 1) { myservo.write(pos); delay(15); }}void getLeftAndRight_center() { Serial.println(110); Serial.println(pos); if (pos == 90) { for (pos = 90; pos > 30; pos -= 1) { myservo.write(pos); delay(15); } }else if(pos == -30){ for (pos = -30; pos < 30; pos += 1) { myservo.write(pos); delay(15); } }}void getLeftAndRight_right() { for (pos = 30; pos > -30; pos -= 1) { myservo.write(pos); delay(15); }}void initLeftAndRightPoint() { myservo.write(pos);}void initLeftAndRight() { myservo.attach(input7); initLeftAndRightPoint();}void wjf_initLcd(){ lcd.begin (16,2); // for 16 x 2 LCD module lcd.setBacklightPin(3,POSITIVE); lcd.setBacklight(HIGH);}void wjf_setLcd(char *str){ lcd.home (); // set cursor to 0,0 lcd.print(str); lcd.setCursor (0,1); // go to start of 2nd line //lcd.print(millis()); //delay(1000); lcd.setBacklight(LOW); // Backlight off delay(250); lcd.setBacklight(HIGH); // Backlight on delay(1000); }void setup(){ Serial.begin(9600); irrecv.enableIRIn(); // 启动接收器 initRun(); initLight(); wjf_initLcd(); wjf_setLcd("welcome human");}void left() { digitalWrite(input1, HIGH); //给高电平 digitalWrite(input2, LOW); //给低电平 //delay(1000); //延时1秒}void right() { digitalWrite(input1, LOW); digitalWrite(input2, HIGH); //delay(1000);}void goRun() { digitalWrite(input3, HIGH); //给高电平 digitalWrite(input4, LOW); //给低电平 //delay(1000); //延时1秒}void goBack() { digitalWrite(input3, LOW); digitalWrite(input4, HIGH); //delay(1000);}void goStop() { //stop 停止 digitalWrite(input1, LOW); digitalWrite(input2, LOW); digitalWrite(input3, LOW); digitalWrite(input4, LOW); //delay(1000); //延时0.5秒}void openLight() { digitalWrite(LED, HIGH);}static int goThread1(struct pt *pt) //线程{ //PT_BEGIN(pt); //线程开始 while (1) //每个线程都不会死 { digitalWrite(LED, HIGH); //delay(1000); digitalWrite(LED, LOW); //delay(1000); } //PT_END(pt); //线程结束}void closeLight() { digitalWrite(LED, LOW);}void loop() { if (irrecv.decode(&results)) { Serial.println(results.value, HEX);//以16进制换行输出接收代码 if (results.value == 4294967295) { //long click } else { if (results.value == control[0][0]) { } else if (results.value == control[0][1]) { //上 //openLight(); //goRun(); openLight(); left(); wjf_setLcd("go forward"); } else if (results.value == control[0][2]) { } else if (results.value == control[1][0]) { //左 openLight(); getLeftAndRight_left(); wjf_setLcd("go left"); } else if (results.value == control[1][1]) { //中 initLeftAndRightPoint(); closeLight(); goStop(); // openLight(); getLeftAndRight_center(); wjf_setLcd("stoping..."); } else if (results.value == control[1][2]) { //右 openLight(); getLeftAndRight_right(); wjf_setLcd("go right"); } else if (results.value == control[2][0]) { } else if (results.value == control[2][1]) { //下 //openLight(); //goBack(); openLight(); right(); wjf_setLcd("go back"); } else if (results.value == control[2][2]) { } else if (results.value == control[3][0]) { //0 } else if (results.value == control[3][1]) { } else if (results.value == control[3][2]) { //st } else if (results.value == control[4][0]) { } else if (results.value == control[4][1]) { } else if (results.value == control[4][2]) { } else if (results.value == control[5][0]) { } else if (results.value == control[5][1]) { } else if (results.value == control[5][2]) { } else if (results.value == control[6][0]) { } else if (results.value == control[6][1]) { } else if (results.value == control[6][2]) { } } irrecv.resume(); // 接收下一个值 } delay(100);}

 

转载于:https://www.cnblogs.com/SATinnovation/p/8047620.html

你可能感兴趣的文章
Xcode CoreData 存储报错问题。
查看>>
学习Jammendo代码的心路历程(二)ViewFlipper数据的填充
查看>>
Quality in the Test Automation Review Process and Design Review Template
查看>>
高亮显示搜索的关键词(二)
查看>>
2. 知识图谱-命名实体识别(NER)详解
查看>>
344. Reverse String
查看>>
JAVA-初步认识-第四章-其他流程控制语句
查看>>
shell脚本中执行python脚本并接收其返回值的例子
查看>>
一次代码重构记录
查看>>
Windows10下安装MySQL8.0
查看>>
html/css静态网页制作
查看>>
内联元素与内联块状元素
查看>>
centos下yum安装wget失败
查看>>
点分治
查看>>
判断游戏对象是否在摄像机视口的一个方法
查看>>
[POJ1830] 开关问题
查看>>
linux下find查找命令用法
查看>>
《Head First设计模式》
查看>>
CentOS 配置网络yum源
查看>>
UVALive 7274 Canvas Painting (优先队列)
查看>>