Arduino智能小车--仅仅是随便一搞
在某宝宝买的智能小车,挺精致的,开心的连接上打印机的线,结果port都没有反应,
查了一下发现是少了驱动,博主用的mac os10.12.3
安装好之后我们再启动arduino 就能够看到新的port了,选择他,我们就能够进行上传代码了。
仅仅是对附赠源代码进行了一下整理。
。。
这个用到了红外遥控,须要一个IRremote库。打包资源在csdn下载里边自行下载,内附说明。感谢智宇科技--链接 感谢智宇科技--链接 感谢智宇科技--链接 感谢智宇科技--链接
链接: https://pan.baidu.com/s/1dFwzx53 password: 2aju
#include <IRremote.h>//包括红外库 关键点 int RECV_PIN = A4;//port声明 IRrecv irrecv(RECV_PIN); decode_results results;//结构声明 int on = 0;//标志位 unsigned long last = millis(); long run_car = 0x00FF629D;//按键CH long back_car = 0x00FFA857;//按键+ long left_car = 0x00FF22DD;//按键<< long right_car = 0x00FFC23D;//按键>|| long stop_car = 0x00FF02FD;//按键>>| long left_turn = 0x00ffE01F;//按键- long right_turn = 0x00FF906F;//按键EQ //避障开关 long avoidopen = 0x00FF42BD;//按键7 long trackingopen = 0x00FF52AD;//按键9 //============================== int Left_motor_go=8; //左电机前进(IN1) int Left_motor_back=9; //左电机后退(IN2) int Right_motor_go=10; // 右电机前进(IN3) int Right_motor_back=11; // 右电机后退(IN4) //避障 int key=A2;//定义按键 数字A2 接口 int beep=A3;//定义蜂鸣器 数字A3 接口 int LED=7;//定义LED 数字7 接口 const int SensorRight_2 = 5; //中间红外避障传感器(P3.4 OUT3) int SR_2; //右红外传感器状态 //循迹 const int SensorRight = 3; //右循迹红外传感器(P3.2 OUT1) const int SensorLeft = 4; //左循迹红外传感器(P3.3 OUT2) int SL; //左循迹红外传感器状态 int SR; //右循迹红外传感器状态 void setup() { //初始化电机驱动IO为输出方式 pinMode(Left_motor_go,OUTPUT); // PIN 8 (PWM) pinMode(Left_motor_back,OUTPUT); // PIN 9 (PWM) pinMode(Right_motor_go,OUTPUT);// PIN 10 (PWM) pinMode(Right_motor_back,OUTPUT);// PIN 11 (PWM) pinMode(13, OUTPUT);////port模式,输出 Serial.begin(9600); //波特率9600 irrecv.enableIRIn(); // Start the receiver //避障 pinMode(key,INPUT);//定义按键接口为输入接口 pinMode(beep,OUTPUT);//定义蜂鸣器为输出接口 pinMode(LED,OUTPUT);//定义LED为输出接口 pinMode(SensorRight, INPUT); //定义右循迹红外传感器为输入 pinMode(SensorLeft, INPUT); //定义左循迹红外传感器为输入 } void run() // 前进 { digitalWrite(Right_motor_go,HIGH); // 右电机前进 digitalWrite(Right_motor_back,LOW); //analogWrite(Right_motor_go,200);//PWM比例0~255调速,左右轮差异略增减 //analogWrite(Right_motor_back,0); digitalWrite(Left_motor_go,LOW); // 左电机前进 digitalWrite(Left_motor_back,HIGH); //analogWrite(Left_motor_go,0);//PWM比例0~255调速。左右轮差异略增减 //analogWrite(Left_motor_back,200); //delay(time * 100); //运行时间,能够调整 } void brake(int time) //刹车,停车 { digitalWrite(Right_motor_go,LOW); digitalWrite(Right_motor_back,LOW); digitalWrite(Left_motor_go,LOW); digitalWrite(Left_motor_back,LOW); delay(time * 100);//运行时间,能够调整 } void left(int time) //左转(左轮不动,右轮前进) { digitalWrite(Right_motor_go,HIGH); // 右电机前进 digitalWrite(Right_motor_back,LOW); analogWrite(Right_motor_go,200); analogWrite(Right_motor_back,0);//PWM比例0~255调速 digitalWrite(Left_motor_go,LOW); //左轮后退 digitalWrite(Left_motor_back,LOW); analogWrite(Left_motor_go,0); analogWrite(Left_motor_back,0);//PWM比例0~255调速 delay(time * 100); //运行时间。能够调整 } void spin_left(int time) //左转(左轮后退,右轮前进) { digitalWrite(Right_motor_go,HIGH); // 右电机前进 digitalWrite(Right_motor_back,LOW); analogWrite(Right_motor_go,200); analogWrite(Right_motor_back,0);//PWM比例0~255调速 digitalWrite(Left_motor_go,HIGH); //左轮后退 digitalWrite(Left_motor_back,LOW); analogWrite(Left_motor_go,200); analogWrite(Left_motor_back,0);//PWM比例0~255调速 delay(time * 100); //运行时间,能够调整 } void spin_right(int time) //右转(右轮后退。左轮前进) { digitalWrite(Right_motor_go,LOW); //右电机后退 digitalWrite(Right_motor_back,HIGH); analogWrite(Right_motor_go,0); analogWrite(Right_motor_back,200);//PWM比例0~255调速 digitalWrite(Left_motor_go,LOW);//左电机前进 digitalWrite(Left_motor_back,HIGH); analogWrite(Left_motor_go,0); analogWrite(Left_motor_back,200);//PWM比例0~255调速 delay(time * 100); //运行时间,能够调整 } void back(int time) //后退 { digitalWrite(Right_motor_go,LOW); //右轮后退 digitalWrite(Right_motor_back,HIGH); analogWrite(Right_motor_go,0); analogWrite(Right_motor_back,150);//PWM比例0~255调速 digitalWrite(Left_motor_go,HIGH); //左轮后退 digitalWrite(Left_motor_back,LOW); analogWrite(Left_motor_go,150); analogWrite(Left_motor_back,0);//PWM比例0~255调速 delay(time * 100); //运行时间,能够调整 } void brake() //刹车,停车 { digitalWrite(Right_motor_go,LOW); digitalWrite(Right_motor_back,LOW); digitalWrite(Left_motor_go,LOW); digitalWrite(Left_motor_back,LOW); //delay(time * 100);//运行时间,能够调整 } void left() //左转(左轮不动,右轮前进) { digitalWrite(Right_motor_go,HIGH); // 右电机前进 digitalWrite(Right_motor_back,LOW); //analogWrite(Right_motor_go,200); //analogWrite(Right_motor_back,0);//PWM比例0~255调速 digitalWrite(Left_motor_go,LOW); //左轮后退 digitalWrite(Left_motor_back,LOW); //analogWrite(Left_motor_go,0); //analogWrite(Left_motor_back,0);//PWM比例0~255调速 //delay(time * 100); //运行时间,能够调整 } void spin_left() //左转(左轮后退,右轮前进) { digitalWrite(Right_motor_go,HIGH); // 右电机前进 digitalWrite(Right_motor_back,LOW); //analogWrite(Right_motor_go,200); //analogWrite(Right_motor_back,0);//PWM比例0~255调速 digitalWrite(Left_motor_go,HIGH); //左轮后退 digitalWrite(Left_motor_back,LOW); //analogWrite(Left_motor_go,200); //analogWrite(Left_motor_back,0);//PWM比例0~255调速 //delay(time * 100); //运行时间,能够调整 } void right() //右转(右轮不动,左轮前进) { digitalWrite(Right_motor_go,LOW); //右电机后退 digitalWrite(Right_motor_back,LOW); //analogWrite(Right_motor_go,0); //analogWrite(Right_motor_back,0);//PWM比例0~255调速 digitalWrite(Left_motor_go,LOW);//左电机前进 digitalWrite(Left_motor_back,HIGH); //analogWrite(Left_motor_go,0); // analogWrite(Left_motor_back,200);//PWM比例0~255调速 // delay(time * 100); //运行时间,能够调整 } void spin_right() //右转(右轮后退,左轮前进) { digitalWrite(Right_motor_go,LOW); //右电机后退 digitalWrite(Right_motor_back,HIGH); //analogWrite(Right_motor_go,0); //analogWrite(Right_motor_back,200);//PWM比例0~255调速 digitalWrite(Left_motor_go,LOW);//左电机前进 digitalWrite(Left_motor_back,HIGH); //analogWrite(Left_motor_go,0); //analogWrite(Left_motor_back,200);//PWM比例0~255调速 //delay(time * 100); //运行时间,能够调整 } void back() //后退 { digitalWrite(Right_motor_go,LOW); //右轮后退 digitalWrite(Right_motor_back,HIGH); //analogWrite(Right_motor_go,0); //analogWrite(Right_motor_back,150);//PWM比例0~255调速 digitalWrite(Left_motor_go,HIGH); //左轮后退 digitalWrite(Left_motor_back,LOW); //analogWrite(Left_motor_go,150); //analogWrite(Left_motor_back,0);//PWM比例0~255调速 //delay(time * 100); //运行时间。能够调整 } void dump(decode_results *results) { int count = results->rawlen; if (results->decode_type == UNKNOWN) { //Serial.println("Could not decode message"); brake(); } //串口打印,调试时能够打开,实际运行中会影响反应速度,建议屏蔽 /* else { if (results->decode_type == NEC) { Serial.print("Decoded NEC: "); } else if (results->decode_type == SONY) { Serial.print("Decoded SONY: "); } else if (results->decode_type == RC5) { Serial.print("Decoded RC5: "); } else if (results->decode_type == RC6) { Serial.print("Decoded RC6: "); } Serial.print(results->value, HEX); Serial.print(" ("); Serial.print(results->bits, DEC); Serial.println(" bits)"); } Serial.print("Raw ("); Serial.print(count, DEC); Serial.print("): "); for (int i = 0; i < count; i++) { if ((i % 2) == 1) { Serial.print(results->rawbuf[i]*USECPERTICK, DEC); } else { Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC); } Serial.print(" "); } Serial.println(""); */ } //========================================================== //避障改为遥控器,此处能够忽略掉 void keysacn()//按键扫描 { int val; val=digitalRead(key);//读取数字7 口电平值赋给val while(!digitalRead(key))//当按键没被按下时。一直循环 { val=digitalRead(key);//此句可省略,可让循环跑空 } while(digitalRead(key))//当按键被按下时 { delay(10); //延时10ms val=digitalRead(key);//读取数字7 口电平值赋给val if(val==HIGH) //第二次推断按键是否被按下 { digitalWrite(beep,HIGH); //蜂鸣器响 while(!digitalRead(key)) //推断按键是否被松开 digitalWrite(beep,LOW); //蜂鸣器停止 } else digitalWrite(beep,LOW);//蜂鸣器停止 } } //避障程序 void avoid() { //改动为遥控器控制 //keysacn(); //调用按键扫描函数 while(1) { //有信号为LOW 没有信号为HIGH 有障碍物输出0 没有障碍物输出1 SR_2 = digitalRead(SensorRight_2); if (SR_2==HIGH)//前面没有障碍物 { run(); //调用前进函数 digitalWrite(beep,LOW); //蜂鸣器不响 digitalWrite(LED,LOW); //LED不亮 } else if ( SR_2 == LOW)// 前面探測到有障碍物,有信号返回 { digitalWrite(beep,HIGH); //蜂鸣器响 digitalWrite(LED,HIGH); //LED亮 brake(3);//停止300MS back(4);//后退400MS left(5);//调用左转函数 延时500ms } } } void tracking(){ while(1) { //有信号为LOW 没有信号为HIGH 检測到黑线 输出高 检測到白色区域输出低 SR = digitalRead(SensorRight);//有信号表明在白色区域。车子底板上L1亮;没信号表明压在黑线上。车子底板上L1灭 SL = digitalRead(SensorLeft);//有信号表明在白色区域。车子底板上L2亮;没信号表明压在黑线上,车子底板上L2灭 if (SL == LOW&&SR==LOW) run(); //调用前进函数 else if (SL == HIGH & SR == LOW)// 左循迹红外传感器,检測到信号,车子向右偏离轨道,向左转 left(); else if (SR == HIGH & SL == LOW) // 右循迹红外传感器,检測到信号。车子向左偏离轨道,向右转 right(); else // 都是黑色, 停止 brake(); } } void loop() { if (irrecv.decode(&results)) //调用库函数:解码 { // If it's been at least 1/4 second since the last // IR received, toggle the relay if (millis() - last > 250) //确定接收到信号 { on = !on;//标志位置反 digitalWrite(13, on ? HIGH : LOW);//板子上接收到信号闪烁一下led dump(&results);//解码红外信号 } if (results.value == run_car )//按键CH run();//前进 if (results.value == back_car )//按键+ back();//后退 if (results.value == left_car )//按键<< left();//左转 if (results.value == right_car )//按键>|| right();//右转 if (results.value == stop_car )//按键>>| brake();//停车 if (results.value == left_turn )//按键- spin_left();//左旋转 if (results.value == right_turn )//按键EQ spin_right();//右旋转 if (results.value == avoidopen)//按键7 avoid();// 开启智能避障 if (results.value == trackingopen)//按键9 tracking(); last = millis(); irrecv.resume(); // Receive the next value } }