|
| 1 | +# 模拟输入,串行输出 |
| 2 | +读取模拟输入引脚,映射结果,然后使用该数据来调暗或调亮 LED。 |
| 3 | + |
| 4 | +此示例向您展示如何读取模拟输入引脚,将结果映射到 0 到 255 的范围,使用该结果设置输出引脚的脉宽调制 (PWM) 以调暗或调亮 LED,并将值打印在VS Code的串行监视器或者终端。 |
| 5 | + |
| 6 | +## 硬件要求 |
| 7 | +- Arduino 板卡 |
| 8 | +- 电位器 |
| 9 | +- 红色LED |
| 10 | +- 220欧姆电阻 |
| 11 | + |
| 12 | +## 电路 |
| 13 | +将电位计的一个引脚连接到 5V,中心引脚连接到模拟引脚 0,其余引脚接地。接下来,将 220 欧姆限流电阻连接到数字引脚 9,并串联一个 LED。 LED 的长正引脚(阳极)应连接到电阻器的输出,而较短的负引脚(阴极)应接地。 |
| 14 | + |
| 15 | +### 电路图 |
| 16 | + |
| 17 | + |
| 18 | +## 代码 |
| 19 | +在下面的草图中,声明两个引脚分配(我们的电位器的模拟 0 和 LED 的数字 9)和两个变量(sensor_value 和 output_value)后,创建一个串口连接。 |
| 20 | + |
| 21 | +接下来,在主循环中,sensor_value 被分配来存储从电位计读取的原始模拟值。 Arduino 的 analog_read范围为0到1023,而 set_duty_cycle_percent 范围仅为 0 到 100,因此,在使用电位计调暗 LED 之前,需要将电位计的数据转换为更小的范围。 |
| 22 | + |
| 23 | +转换这个值: |
| 24 | +```rust |
| 25 | +let output_value = (sensor_value * 100/1023) as u8; |
| 26 | +``` |
| 27 | +输出值被指定为等于电位计的缩放值。 map() 接受五个参数:要映射的值、输入数据的低范围和高值以及要重新映射到的数据的低值和高值。在这种情况下,传感器数据从其原始范围 0 到 1023 向下映射到 0 到 255。 |
| 28 | + |
| 29 | +然后,新映射的传感器数据会输出到analogOutPin,随着电位计的转动,LED 会变暗或变亮。最后,原始传感器值和缩放传感器值均以稳定的数据流发送到VS Code串行监视器窗口。 |
| 30 | + |
| 31 | +编译并运行示例 |
| 32 | +```shell |
| 33 | +cargo build |
| 34 | +cargo run |
| 35 | +``` |
| 36 | + |
| 37 | +完整代码如下: |
| 38 | + |
| 39 | +src/main.rs |
| 40 | +```rust |
| 41 | +/*! |
| 42 | + * Analog input, analog output, serial output |
| 43 | + * |
| 44 | + * Reads an analog input pin, maps the result to a range from 0 to 100 and uses |
| 45 | + * the result to set the pulse width modulation (PWM) of an output pin. |
| 46 | + * |
| 47 | + * Also prints the results to the Serial Monitor. |
| 48 | + * |
| 49 | + * We are using an embedded_hal compatible version. |
| 50 | + */ |
| 51 | +#![no_std] |
| 52 | +#![no_main] |
| 53 | + |
| 54 | +use arduino_hal::prelude::_unwrap_infallible_UnwrapInfallible; |
| 55 | +use arduino_hal::simple_pwm::*; |
| 56 | +use embedded_hal::delay::DelayNs; |
| 57 | +use embedded_hal::pwm::SetDutyCycle; |
| 58 | +use panic_halt as _; |
| 59 | + |
| 60 | +#[arduino_hal::entry] |
| 61 | +fn main() -> ! { |
| 62 | + let dp = arduino_hal::Peripherals::take().unwrap(); |
| 63 | + let pins = arduino_hal::pins!(dp); |
| 64 | + let mut serial = arduino_hal::default_serial!(dp, pins, 57600); |
| 65 | + let mut adc = arduino_hal::Adc::new(dp.ADC, Default::default()); |
| 66 | + |
| 67 | + let a0 = pins.a0.into_analog_input(&mut adc); |
| 68 | + let mut pwm_led = pins |
| 69 | + .d9 |
| 70 | + .into_output() |
| 71 | + .into_pwm(&Timer1Pwm::new(dp.TC1, Prescaler::Prescale64)); |
| 72 | + pwm_led.enable(); |
| 73 | + |
| 74 | + let mut delay = arduino_hal::Delay::new(); |
| 75 | + |
| 76 | + loop { |
| 77 | + let sensor_value = a0.analog_read(&mut adc) as u32; |
| 78 | + |
| 79 | + let output_value = (sensor_value * 100 / 1023) as u8; |
| 80 | + |
| 81 | + pwm_led.set_duty_cycle_percent(output_value).unwrap(); |
| 82 | + |
| 83 | + ufmt::uwriteln!( |
| 84 | + &mut serial, |
| 85 | + "sensor: {}, output: {}", |
| 86 | + sensor_value, |
| 87 | + output_value |
| 88 | + ) |
| 89 | + .unwrap_infallible(); |
| 90 | + delay.delay_ms(2); |
| 91 | + } |
| 92 | +} |
| 93 | +``` |
0 commit comments