Arduino机器人快速上手经验分享(L298N马达驱动,摇杆模块)
Bill of Material:
直流马达(<9V, 2A) x2, 底盘 x1,9V电池 x1, 电池扣 x1, 摇杆模块 x1, L298N 马达驱动模块 x1, Arduino Uno x1, 面包板线。
.演示视频,拼装教程:
Arduino机器人快速上手经验分享
接线图
程式/代码:
/* Demonstrates the use of Joystick control with Arduino UNO and L298N Motor Driver MOdule.Last Edited: Jan.4th.2020 by Mun Kim
*/int in1 = 3; // Direction Control: Motor 1(Left Hand Side)
int in2 = 4; // Direction Control: Motor 1(Left Hand Side)
int enA = 5; // Power Control: Motor 1int in3 = 7; // Direction Control: Motor 2 (Right Hand Side)
int in4 = 8; // Direction Control: Motor 2 (Right Hand Side)
int enB = 9; // Power Control: Motor 2int MotorSpeed1 = 0; // Motor 1 Speed Values - Start at zero
int MotorSpeed2 = 0; // Motor 2 Speed Values - Start at zeroint joyHorz = A0; // Joystick Input: Horizontal(X-axis)
int joyVert = A1; // Joystick Input: Vertical(Y-axis)// Joystick Values - Start at 512 (middle position)
int joyposVert = 512;
int joyposHorz = 512; void setup()
{// Set all the motor control pins to outputspinMode(enA, OUTPUT);pinMode(enB, OUTPUT);pinMode(in1, OUTPUT);pinMode(in2, OUTPUT);pinMode(in3, OUTPUT);pinMode(in4, OUTPUT);// Start with motors disabled and direction forwarddigitalWrite(enA, LOW);digitalWrite(in1, HIGH);digitalWrite(in2, LOW);digitalWrite(enB, LOW);digitalWrite(in3, HIGH);digitalWrite(in4, LOW);
}void loop() {// Read the Joystick X and Y positionsjoyposVert = analogRead(joyVert); joyposHorz = analogRead(joyHorz);// Forward and backward driveif (joyposVert < 460) //Backward{digitalWrite(in1, LOW);digitalWrite(in2, HIGH);digitalWrite(in3, LOW);digitalWrite(in4, HIGH);joyposVert = -joyposVert +460; // As we are going backwards we need to reverse readingsMotorSpeed1 = map(joyposVert, 0, 460, 0, 255);MotorSpeed2 = map(joyposVert, 0, 460, 0, 255);}else if (joyposVert > 564) //Forward{digitalWrite(in1, HIGH);digitalWrite(in2, LOW);digitalWrite(in3, HIGH);digitalWrite(in4, LOW);MotorSpeed1 = map(joyposVert, 564, 1023, 0, 255);MotorSpeed2 = map(joyposVert, 564, 1023, 0, 255);
}else //Stopped{MotorSpeed1 = 0;MotorSpeed2 = 0; }//Left and right steering: The horizontal position weights the motor speedif (joyposHorz < 460) //Leftward{joyposHorz = -joyposHorz + 460; // Again, reverse the readings here.joyposHorz = map(joyposHorz, 0, 460, 0, 255);MotorSpeed1 = MotorSpeed1 - joyposHorz;MotorSpeed2 = MotorSpeed2 + joyposHorz;if (MotorSpeed1 < 0)MotorSpeed1 = 0;if (MotorSpeed2 > 255)MotorSpeed2 = 255;}else if (joyposHorz > 564) //Rightward{joyposHorz = map(joyposHorz, 564, 1023, 0, 255);MotorSpeed1 = MotorSpeed1 + joyposHorz;MotorSpeed2 = MotorSpeed2 - joyposHorz;if (MotorSpeed1 > 255)MotorSpeed1 = 255;if (MotorSpeed2 < 0)MotorSpeed2 = 0; }// Adjust to prevent "buzzing" at very low speedif (MotorSpeed1 < 8)MotorSpeed1 = 0;if (MotorSpeed2 < 8)MotorSpeed2 = 0;// Set the motor speedsanalogWrite(enA, MotorSpeed1);analogWrite(enB, MotorSpeed2);}
Arduino机器人快速上手经验分享(L298N马达驱动,摇杆模块)相关推荐
- 力创eda 画布大小_立创EDA快速上手经验篇.docx
立创EDA快速上手经验篇 立创EDA(海外版本叫easyeda)是近年比较流行的一款在线设计EDA(电子自动化)软件.相比早已经完成抢占市场的老牌工具,立创EDA通过"终身免费,在线方便,开 ...
- 「雕爷学编程」Arduino动手做(38)——joystick双轴摇杆模块
37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的.鉴于本人手头积累了一些传感器和模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里 ...
- PMP快速通过经验分享
看到标题,能点进来看的,我想至少应该知道什么是 PMP,或者是想要报考的.对 PMP 不太清楚的同学,可点击 「PMP价值」查看.我之前是一名程序员,现在其中的一个身份是项目经理.参加了201803期 ...
- APICloud App开发上手经验分享之模块调用
继上次简单分享了使用APICloud开发APP的全流程后,今天来分享一下如何使用模块.APICloud提供了很多方便好用的模块,只要学习自己想要使用的模块的文档,然后调用模块就可以使用了.使用模块可以 ...
- 力创eda 画布大小_立创EDA快速上手经验指南
目录 : ~9 ` u+ Q" {2 ?, o 一:浏览器设置. 3 v8 Y: }, I) f+ Q 二:界面使用 6 }6 I4 V; n& _/ J! E9 A三:常用快速操 ...
- 力创eda 画布大小_立创EDA快速上手经验篇 -
6 所有 7 所有 8 所有 9 所有 10 所有 11 所有 12 所有 13 所有 14 所有 15 所有 16 所有 17 所有 18 所有 19 所有 20 所有 21 所有 22 所有 23 ...
- Arduino SPI快速上手
MOSI:主设备数据输出,从设备数据输入 MISO SCK(Serial Clock):时钟信号,由主设备产生,又名SCLK SS(Slave Select):从设备使能信号,由主设备控制,低电平有效 ...
- 快速上手CH340N电路设计(CH340N USB转串口模块 USB Type-C接口 CH340系列芯片讲解)
一.上模块 二.功能分析 l 芯片:CH340N l 输入接口:USB.TYPE-C l 输出接口:TTL(5V\3.3V\GND\TX\RX) l 指示灯:电源.TX.RX 三.CH340x ...
- 【“探索者”小车】机器人大赛“探索者”全地形小车参赛经验分享
提示:本文内容基于"2022年中国机器人及人工智能大赛--全地形小车设计赛"的参赛经历而作,其他年份赛题和规则可能会有变动,所以该文章只做参考,具体操作得看实际情况. 本文只做参考 ...
最新文章
- CRichEdit小记
- keepalived+nginx安装与配置
- Ubuntu常用安装源
- 作者:​王宁(1979-),男,厦门华厦学院副教授、高级工程师。
- logstash关于date时间处理的几种方式总结
- 「Android」Lenovo K860 root脚本备忘
- mysql public权限_MySQL · 引擎特性 · Binlog encryption 浅析
- 数据结构----二路归并排序
- 定义客户类(Customer): 1,客户类的属性包括:姓名、年龄、电话、金钱数量、账号、密码; 2,方法包括:购买商品、付款、显示自己的信息。 3,创建测试类,在main方法中使用客户类创建两个客户
- matlab中ode45如何设置,如何使用Matlab中的ode45修正赋值错误(ode45函数的第488行)
- JAVA面试题及解答
- 快速上手五大技巧!自媒体新人创作攻略,想要通过拍视频赚钱
- 8、JAVA入门——switch选择结构
- python处理字符串(SQL语句批量新增)
- MYSQL 唯一约束
- Macadam自然语言处理(NLP)工具包(TextClassification, SequenceLabeling, RelationExtraction)
- 分享:ahk官网内容下载
- 如何确定EINT中断唤醒源
- MySQL 创建表时出现 Tablespace for `xxx`.`xxx` exists.
- Linux ❉ top命令详解