DAY01:Arduino硬件入门02__手把手教你组装一辆小车
5.手把手教你组装一辆小车
文章图片
- 1)焊接电机线
- 如何使用电烙铁视频链接
-
文章图片
- 2)小车底盘安装
- 小车零件
-
文章图片
- 安装万向轮
-
文章图片
- 固定电机(注意螺丝的安装方向!!不然可能别轮子)
-
文章图片
- 在小车正面固定电池,安装车轮
-
文章图片
- 底盘安装结束
-
文章图片
- 3)安装主板
- 准备好arduino板子、透明隔板、电池、螺丝等
-
文章图片
-
- 把arduino板子固定在透明隔板上
-
文章图片
-
- 把板子架高,固定在小车上。
-
文章图片
-
- 准备好arduino板子、透明隔板、电池、螺丝等
- 4)安装小车电机驱动模块
-
文章图片
-
- 5)安装扩展板、连线电机驱动模块、电池盒模块
-
文章图片
-
文章图片
-
- 6)红外避障模块安装与连线
-
文章图片
-
文章图片
-
- 7)红外避障模块程序烧写与调试
-
文章图片
- 避障模块代码如下:
-
//智能小车红外避障实验1(基础避障) //=============================================================== //#include 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; //左边红外避障传感器() const int SensorLeft_2 = 6; //右边红外避障传感器()int SR_2; //右边红外避障传感器状态 int SL_2; //左边红外避障传感器状态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(key,INPUT); //定义按键接口为输入接口 pinMode(beep,OUTPUT); //定义蜂鸣器为输出接口 pinMode(LED,OUTPUT); //定义LED为输出接口pinMode(SensorRight_2, INPUT); //定义中间避障传感器为输入} //=======================智能小车的基本动作========================= //void run(int time)// 前进 void run()// 前进 { digitalWrite(Right_motor_go,HIGH); // 右电机前进 digitalWrite(Right_motor_back,LOW); analogWrite(Right_motor_go,160); //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,160); //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(int time)//左转(左轮不动,右轮前进) 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(int time) 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 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 loop() { keysacn(); //调用按键扫描函数 while(1) { //有信号为LOW没有信号为HIGH SR_2 = digitalRead(SensorRight_2); SL_2 = digitalRead(SensorLeft_2); if (SL_2 == HIGH&&SR_2==HIGH) { run(); //调用前进函数 digitalWrite(beep,LOW); } else if (SL_2 == HIGH & SR_2 == LOW)// 右边探测到有障碍物,有信号返回,向左转 spin_left(); else if (SR_2 == HIGH & SL_2 == LOW) //左边探测到有障碍物,有信号返回,向右转 spin_right(); else // 都是有障碍物, 后退 { digitalWrite(beep,HIGH); //蜂鸣器响 //digitalWrite(LED,HIGH); //LED亮 brake(); //停止200MS delay(300); back(); //后退500MS delay(400); left(); //调用左转函数延时500ms delay(500); }} }
-
- 8)寻迹模块安装与调试
-
文章图片
-
-
文章图片
//智能小车黑线循迹实验 //=============================================================== //#include 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 接口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(key,INPUT); //定义按键接口为输入接口 pinMode(beep,OUTPUT); pinMode(SensorRight, INPUT); //定义右循迹红外传感器为输入 pinMode(SensorLeft, INPUT); //定义左循迹红外传感器为输入 }//=======================智能小车的基本动作========================= //void run(int time)// 前进 void run() { digitalWrite(Right_motor_go,HIGH); // 右电机前进 digitalWrite(Right_motor_back,LOW); analogWrite(Right_motor_go,60); //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,60); //delay(time * 100); //执行时间,可以调整 }//void brake(int time)//刹车,停车 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(int time)//左转(左轮不动,右轮前进) void left() { digitalWrite(Right_motor_go,HIGH); // 右电机前进 digitalWrite(Right_motor_back,LOW); analogWrite(Right_motor_go,60); 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,60); analogWrite(Right_motor_back,0); //PWM比例0~255调速 digitalWrite(Left_motor_go,HIGH); //左轮后退 digitalWrite(Left_motor_back,LOW); analogWrite(Left_motor_go,30); analogWrite(Left_motor_back,0); //PWM比例0~255调速 //delay(time * 100); //执行时间,可以调整 }//void right(int time)//右转(右轮不动,左轮前进) 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,60); //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,30); //PWM比例0~255调速 digitalWrite(Left_motor_go,LOW); //左电机前进 digitalWrite(Left_motor_back,HIGH); analogWrite(Left_motor_go,0); analogWrite(Left_motor_back,60); //PWM比例0~255调速 //delay(time * 100); //执行时间,可以调整 }//void back(int time)//后退 void back(int time) { digitalWrite(Right_motor_go,LOW); //右轮后退 digitalWrite(Right_motor_back,HIGH); analogWrite(Right_motor_go,0); analogWrite(Right_motor_back,60); //PWM比例0~255调速 digitalWrite(Left_motor_go,HIGH); //左轮后退 digitalWrite(Left_motor_back,LOW); analogWrite(Left_motor_go,60); analogWrite(Left_motor_back,0); //PWM比例0~255调速 delay(time * 100); //执行时间,可以调整 } //==========================================================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 loop() { keysacn(); //调用按键扫描函数 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)// 左循迹红外传感器,检测到信号,车子向右偏离轨道,向左转 right(); else if (SR == HIGH & SL == LOW) // 右循迹红外传感器,检测到信号,车子向左偏离轨道,向右转 left() ; else // 都是黑色, 停止 brake(); } }
9)红外遥控
-
文章图片
- 其中,红外遥控程序中,需要增加IRremote库。
- IRremote库下载:百度网盘链接:https://pan.baidu.com/s/120ig2QjajK5gcnMY0M8-wQ 提取码:c6wt
- 下载后将IRremote 复制到Arduino安装目录下面libraries 文件夹下面 ,否则编译会出现C:\Program Files (x86)\Arduino\libraries\RobotIRremote\src\IRremoteTools.cpp:5:16: error: 'TKD2' was not declared in this scope int RECV_PIN = TKD2; // the pin the IR receiver is connected to编译有误。
-
//============================启航科技======================================== //智能小车红外遥控实验 //实验中所用接收红外信号为配送遥控器的信号,也可打印出信号数值,配合其他红外信号控制 //参考4.视频教程\Arduino套件视频\视频教程\例程19-红外遥控.mp4 //本实验不可调节电机速度,调节pwm值会影响红外的信号接收 //============================================================================= #include
//包含红外库关键点 int RECV_PIN = A4; //端口声明 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 //============================== int Left_motor_go=8; //左电机前进(IN1) int Left_motor_back=9; //左电机后退(IN2)int Right_motor_go=10; // 右电机前进(IN3) int Right_motor_back=11; // 右电机后退(IN4)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); 端口模式,输出 Serial.begin(9600); //波特率9600 irrecv.enableIRIn(); // Start the receiver } 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()//刹车,停车 { 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 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 =https://www.it610.com/article/= 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 =https://www.it610.com/article/= stop_car )//按键>>| brake(); //停车 if (results.value =https://www.it610.com/article/= left_turn )//按键- spin_left(); //左旋转 if (results.value == right_turn )//按键EQ spin_right(); //右旋转 last = millis(); irrecv.resume(); // Receive the next value } }
【DAY01:Arduino硬件入门02__手把手教你组装一辆小车】
-
- 作业一(Level 1):将小车组装完成,将课程内容记录在简书、CSDN、知乎中。(内容不限,可以是:电机的功能、红外传感器功能、生活中arduino的应用场景、如何焊接电线、我的收获、开发过程中遇到的问题与解决方案。)
- 作业二(Level 2):学生发挥创意制作一个“会______的xxx”,不限实现方式方法,将过程记录在自己的博客中。(如:会转的风扇、会跑的独轮车)
推荐阅读
- 单片机|Arduino、arm、树莓派、单片机四者有什么不同()
- Android|Android 清单文件的硬件加速android:hardwareAccelerated 慎重
- 杭州视频会议硬件终端和四海云视频会议软件系统解决方案
- Ubuntu查看硬件配置
- 硬件工程|六大原则让你做好电路板
- 修改ARM|修改ARM linux的系统时钟和硬件时钟
- | 电子工程师的出路在哪里(干了两年电子工程师(硬件开发),不知道以后方向在哪里,挺迷茫!)
- 一年多A轮到C轮|一年多A轮到C轮 这家硬件公司凭何如此吸金
- RXTX|Java调用RXTX库与Arduino进行串口通讯需注意的编程问题
- linux|linux hwclock命令参数及用法详解--linux显示/设置硬件时钟命令