/** * author wjf * date 2017/12/30 14:23 * 每个长注释中的代码,都可以单独的模块运用 */ ////////////////引入头文件////////////////////////////////////////////////// //通用字符串库 #include <stdio.h> #include <string.h> #include <stdlib.h> //LCD #include <LiquidCrystal_I2C.h> //LCD #include <Wire.h> #include <LCD.h> // 红外 #include <IRremote.h> //////////////////定义变量//////////////////////////////////////////////////// /** * 定义LCD对象, * 第一个参数0x20需要先扫描I2C地址扫描代码地址: * http://www.cnblogs.com/SATinnovation/p/8047240.html * 后面的暂时不知道什么意思,可以这么直接用 */ LiquidCrystal_I2C lcd(0x20,2,1,0,4,5,6,7); ///////////////定义电机驱动变量//////////////////////////////////////////////////////// //电机针脚PIN const int IN1=5; const int IN2=4; const int ENA=6; const int IN3=8; const int IN4=7; const int ENB=9; //电机速度 int speed = 100; //////////////定义红外接收变量////////////////////////////// int RECV_PIN = 3;//红外使用针脚 IRrecv irrecv(RECV_PIN);//定义接收对象 decode_results results;//暂存结果的变量 long 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 initMotor(){ pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(ENA, OUTPUT); pinMode(IN4, OUTPUT); pinMode(IN3, OUTPUT); pinMode(ENB, OUTPUT); } //////////////////定义红外接收方法///////////////////////////////////// void initIR(){ irrecv.enableIRIn(); } //////////////////////定义LCD方法//////////////////////////////////////////////////////////// void initLCD(){ lcd.begin (16,2); // for 16 x 2 LCD module lcd.setBacklightPin(3,POSITIVE); lcd.setBacklight(HIGH); } void wjf_setLcd(char *str){//设置LCD显示的字符串 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() { //初始电机 initMotor(); //初始化红外接收 initIR(); //初始化LCD initLCD(); wjf_setLcd("Hello world"); } void loop() { if (irrecv.decode(&results)) //如果接收到有红外发射器发送过来的数据 { Serial.println(results.value, HEX);//以16进制换行输出接收代码 if (results.value == 4294967295) {//红外长按得时候获取固定值 } else { if (results.value == control[0][0]) { Motor1_Brake();//停止电机1 Motor2_Brake();//停止电机2 Motor1_Backward(speed);//电机反转,PWM调速 } else if (results.value == control[0][1]) {//上 Motor1_Backward(speed);//电机反转,PWM调速 Motor2_Backward(speed);//电机反转,PWM调速 } else if (results.value == control[0][2]) { Motor1_Brake();//停止电机1 Motor2_Brake();//停止电机2 Motor2_Backward(speed);//电机反转,PWM调速 } else if (results.value == control[1][0]) {//左 } else if (results.value == control[1][1]) {//中 Motor1_Brake();//停止电机1 Motor2_Brake();//停止电机2 } else if (results.value == control[1][2]) {//右 } else if (results.value == control[2][0]) { Motor1_Brake();//停止电机1 Motor2_Brake();//停止电机2 Motor1_Forward(speed);//电机正转,PWM调速 } else if (results.value == control[2][1]) {//下 Motor1_Forward(speed);//电机正转,PWM调速 Motor2_Forward(speed);//电机正转,PWM调速 } else if (results.value == control[2][2]) { Motor1_Brake();//停止电机1 Motor2_Brake();//停止电机2 Motor2_Forward(speed);//电机正转,PWM调速 } 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]) {//1 speed = 100; } else if (results.value == control[4][1]) {//2 speed = 200; } else if (results.value == control[4][2]) {//3 speed = 300; } else if (results.value == control[5][0]) {//4 speed = 400; } else if (results.value == control[5][1]) {//5 speed = 500; } else if (results.value == control[5][2]) {//6 speed = 600; } else if (results.value == control[6][0]) {//7 speed = 700; } else if (results.value == control[6][1]) {//8 speed = 800; } else if (results.value == control[6][2]) {//9 speed = 900; } } irrecv.resume(); // 接收下一个值 } delay(100); } void Motor1_Forward(int Speed) //电机1正转,Speed值越大,电机转动越快 { digitalWrite(IN1,HIGH); digitalWrite(IN2,LOW); analogWrite(ENA,Speed); } void Motor1_Backward(int Speed) //电机1反转,Speed值越大,电机转动越快 { digitalWrite(IN1,LOW); digitalWrite(IN2,HIGH); analogWrite(ENA,Speed); } void Motor1_Brake() //电机1刹车 { digitalWrite(IN1,LOW); digitalWrite(IN2,LOW); } void Motor2_Forward(int Speed) //电机2正转,Speed值越大,电机转动越快 { digitalWrite(IN3,HIGH); digitalWrite(IN4,LOW); analogWrite(ENB,Speed); } void Motor2_Backward(int Speed) //电机2反转,Speed值越大,电机转动越快 { digitalWrite(IN3,LOW); digitalWrite(IN4,HIGH); analogWrite(ENB,Speed); } void Motor2_Brake() { digitalWrite(IN3,LOW); digitalWrite(IN4,LOW); }
需要库请发送邮件到邮箱feige_hunter@163.com