Arduino开发之Analog Rotation Sensor

本文详细介绍如何使用Arduino Uno R3开发板与Arduino IDE 1.8.5,结合DFR0058 Analog Rotation Sensor和DFR0021-R,实现旋转传感器读取值并控制LED亮灭。通过具体实例,讲解了设备连接方式、代码编写、编译上传及运行效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境搭建:

1. Arduino UNO R3开发板,

2. Arduino IDE。

我这里使用的是1.8.5。可以在https://www.arduino.cc/en/Main/Software下载并安装。

安装好之后,桌面会有如下图标。

示例开发:

1.连接设备。

本例中我们以DFR0058 Analog Rotation Sensor结合DFR0021-R为例,基于Arduino Uno R3和Arduino IDE开发。

DFR0021-R的引脚和Arduino Uno开发板的连接方式如下,

DFR0021-R

DFR0058

Arduino Uno R3

VCC

VCC

3.3V

GND

GND

GND

信号引脚

信号引脚

DFR0021接数字引脚8,DFR0058接模拟

引脚A0

2. 编码。

连接好之后,用数据线连接Arduino开发板和电脑。同时打开Arduino IDE。输入下述代码。

const int AnalogRotationPin = A0; // Analog Rotation Sensor Pin

const int LedPin = 8; // Digital LED Pin

const int ROTATION_RANGE = 50; // Rotation Sensor Range Value

 

int sensorValue = 0;

int outputValue = 0;

 

void setup() {

// initialize serial communications at 9600 bps:

Serial.begin(9600);

pinMode(LedPin, OUTPUT); // declare LED as output

pinMode(AnalogRotationPin, INPUT); // declare Analog Rotation Sensor as input

}

 

void loop() {

// read the analog in value:

Serial.print("Reading Analog Rotation Sensor Value: \n");

sensorValue = analogRead(AnalogRotationPin);

// map it to the range of the analog out:

outputValue = map(sensorValue, 0, 1023, 0, 255);

Serial.print("Analog Rotation Vaule = ");

Serial.print(sensorValue);

Serial.print("\n");

Serial.print("\t Output Value = ");

Serial.println(outputValue);

Serial.print("\n");

 

if (outputValue > ROTATION_RANGE) // check Analog Rotation Sensor

{

digitalWrite(LedPin, HIGH); // turn LED ON

Serial.print("LED is On...\n");

}

else

{

digitalWrite(LedPin, LOW); // turn LED OFF

Serial.print("LED is OFF...\n");

}

Serial.println();

delay (1000);

}

然后保存文件。

选择Arduino Uno开发板。

编译上传到开发板。

3.运行。

选择COM口信息,

然后选择端口监视工具,查看程序运行信息。

串口监视信息,

备注:上面的数据就是当前Analog Rotation Sensor的值和Red LED 的当前状态(ON表示开,OFF表示关)。此例中,当Analog Rotation Sensor的值大于50时,就打开LED灯光;反之就关闭LED灯。

 

 

整体运行界面:

实际效果图:

 

### Mixly 中巡线小车避障功能的实现 在 Mixly 图形化编程环境中,可以通过组合多个传感器和执行器来完成巡线小车的避障功能。以下是基于提供的引用内容以及专业知识所整理的内容。 #### 1. **硬件配置** 为了实现巡线与避障功能,通常需要以下硬件组件: - 数字灰度传感器:用于检测黑白线条。 - 超声波传感器:用于测量前方障碍物的距离。 - 小车底盘及其驱动电机:负责移动和转向操作。 这些设备可以直接连接至 Arduino 控制板上,并通过 Mixly 编写相应的逻辑程序[^1]。 #### 2. **核心逻辑设计** ##### (1)循迹部分 利用数字灰度传感器读取地面颜色的变化值。当传感器检测到白色区域时,说明偏离了黑线轨迹;此时应调整方向继续寻找黑线位置。具体方法如下: ```python if (gray_sensor_value == WHITE): turn_left() or turn_right() else: go_straight() ``` 上述伪代码表示如果当前检测的颜色为白,则让车辆适当偏移一定角度再前进直至重新回到黑色路径之上。 ##### (2)避障部分 借助 HC-SR04 这类超声波测距模块获取实时距离数据。一旦发现目标物体接近设定阈值(如小于20cm),立即停止运动并改变航向规避风险。 ```python distance = read_ultrasonic_distance() if distance < SAFE_DISTANCE: stop_moving() rotate_away_from_obstacle() else: continue_following_line() ``` 这里 `SAFE_DISTANCE` 定义了一个安全界限,在此范围之外允许正常运行而不会触发紧急反应机制[^3]。 #### 3. **综合控制策略优化建议** 虽然简单的来回修正能够基本满足沿直线行走的需求,但会造成效率低下且动作不稳定的现象。因此推荐引入更先进的动态窗口法(DWA)作为改进措施之一,该技术属于 ROS 导航堆栈的一部分,能够在复杂场景下提供平滑流畅的速度调节方案[^2]。不过需要注意的是DWA算法相对较为复杂可能不适合初学者直接应用于基础项目当中。 另外还可以尝试PID控制器来精细化处理误差补偿过程从而减少震荡现象的发生几率提高整体性能表现水平。 --- ### 示例代码片段 下面给出一段完整的Mixly图形化脚本转换后的Arduino C++源码供参考学习之用: ```cpp const int trigPin = 9; // Ultrasonic trigger pin const int echoPin = 10; // Ultrasonic echo receive pin const int sensorPin = A0; // Gray scale sensor input analog port void setup(){ Serial.begin(9600); } void loop(){ float dist = getDistance(); int grayVal = analogRead(sensorPin); if(dist<20){ brake(); // Stop immediately upon detecting obstacles within safe limit. delay(500); // Pause briefly before attempting to maneuver around it. spinLeftOrRight(); // Choose either left/right rotation depending on specific design requirements. } else{ followLine(grayVal); // Proceed normally following the black line otherwise. } } float getDistance(){...} // Function definition omitted here due brevity reasons. void brake(){...} void spinLeftOrRight(){...} void followLine(int val){...} ``` > 注明:以上仅为示意性质简化版框架结构,请依据实际需求补充完善各子函数内部细节内容。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值