Button-Triggered Distance Measurement Solution
使用按键触发感应,轻松精准测量物体距离
你将学到和构建的内容
简介
Button-Triggered Distance Measurement Solution 集成了 Button Play Click 和 LightRanger 3 Click,实现按键触发的距离测量与交互式视觉反馈。该解决方案适用于高精度传感、交互式测量系统和智能界面,可在按下按钮时捕获多次距离读数,计算平均值以提高测量精度,并通过 UART 记录结果。背光 LED 随按钮操作平滑变换,为距离感应应用提供直观且易于使用的用户体验。
mikroBUS 1
Button PLAY Click
Button PLAY Click 是一款非常有趣的交互式 Click board™ 组件,它采用集成的电容式触摸传感器显示屏,呈现按钮形式。利用先进的电容式触摸感应技术,CTHS15CIC05ARROW 传感器可以成功替代传统机械按钮,使用户界面设计更加简洁且可靠。除了触摸检测功能外,该传感器还带有一个带背光的绿色箭头图标,使其成为构建各种时尚且视觉吸引力强的界面的理想选择。
mikroBUS 2
LightRanger 3 Click
LightRanger 3 Click 是一款高精度测距 Click 板™,基于 ToF(飞行时间)测量原理。它搭载了 RF Digital 公司的 Simblee™ 兼容 RFD77402 测距模块,该模块集成完整的测量系统,使用非常简单,仅通过 I2C 接口和 INT 引脚与主控 MCU 进行通信。其核心组件包括 VCSEL(垂直腔面发射激光器),用于发射 850nm 波长的窄带红外调制光束,以及接收传感器,用于检测反射光。该模块可测量距离最长可达 2000mm,测量精度可达 ±10%。此外,该设备符合 Class 1 激光产品标准,在不可见红外光谱范围内运行,确保安全性。
功能概述
开发板
Clicker 4 for STM32F4 是一款紧凑型开发板,专为快速构建自定义设备而设计。它配备 STM32F407VGT6 MCU、四个 mikroBUS™ 插座(用于 Click board™ 连接)、电源管理等功能,使其成为快速应用开发的理想选择。核心采用 STM32F407VGT6 MCU,该芯片基于 Arm® Cortex®-M4 32 位处理器,运行频率高达 168 MHz,提供充足的计算能力以满足高负载任务需求。除了两个 1x20 排针接口外,四个 mikroBUS™ 插座可支持庞大且不断增长的 Click boards™ 生态系统。开发板上清晰标记的功能区域提供直观、易用的界面,加快开发进程。Clicker 4 不仅能加速原型设计,还可直接集成到项目中,无需额外的硬件修改。四个 4.2mm(0.165”)的安装孔位于角落,便于使用螺丝固定,实现简便安装。
微控制器概述
MCU卡片 / MCU

建筑
ARM Cortex-M4
MCU 内存 (KB)
10
硅供应商
STMicroelectronics
引脚数
100
RAM (字节)
100
一步一步来
项目组装
实时跟踪您的结果
应用程序输出
1. 应用程序输出 - 在调试模式下,“应用程序输出”窗口支持实时数据监控,直接提供执行结果的可视化。请按照提供的教程正确配置环境,以确保数据正确显示。

2. UART 终端 - 使用UART Terminal通过USB to UART converter监视数据传输,实现Click board™与开发系统之间的直接通信。请根据项目需求配置波特率和其他串行设置,以确保正常运行。有关分步设置说明,请参考提供的教程。

3. Plot 输出 - Plot功能提供了一种强大的方式来可视化实时传感器数据,使趋势分析、调试和多个数据点的对比变得更加直观。要正确设置,请按照提供的教程,其中包含使用Plot功能显示Click board™读数的分步示例。在代码中使用Plot功能时,请使用以下函数:plot(insert_graph_name, variable_name);。这是一个通用格式,用户需要将“insert_graph_name”替换为实际图表名称,并将“variable_name”替换为要显示的参数。

软件支持
库描述
Button-Triggered Distance Measurement Solution 使用 NECTO Studio 开发,确保兼容 mikroSDK 的开源库和工具。该解决方案采用即插即用的设计,支持快速实施和测试,并与所有配备 mikroBUS™ 插座的开发板、入门套件和 mikromedia 板完全兼容。
示例描述
Button-Triggered Distance Measurement Solution 集成了 Button Play Click 和 LightRanger 3 Click,实现按键触发的物体距离测量,并通过平滑渐变的背光灯提供视觉反馈。距离测量通过多次读取并计算平均值以提高精度,结果通过 UART 输出日志。
关键功能:
initialize_button_play
- 初始化 Button Play Click,配置 PWM 用于控制 LED 背光,并启动 PWM 信号。如果初始化失败,则记录错误并停止程序运行。initialize_light_ranger
- 初始化 LightRanger 3 Click 并执行传感器设置。如果传感器初始化失败,则记录错误并停止程序运行。update_button_backlight
- 根据按钮状态平滑调整背光 LED 的亮度。按下时亮起,松开时渐暗。measure_and_log_distance
- 使用 LightRanger 3 Click 进行单次距离测量,并通过 UART 记录测量结果。
应用初始化
初始化序列设置 UART 日志记录,配置两个 Click 板并验证硬件是否准备就绪。如果初始化过程中出现错误,系统会记录错误信息并停止运行。
应用任务
主应用循环持续执行以下任务:
1. 检测按钮按下事件并触发距离测量。
2. 进行多次距离测量,计算平均值,并通过 UART 输出日志。
3. 在按下按钮时渐亮背光,松开时渐暗。
4. 记录按钮按下/释放事件及距离测量结果。
开源
代码示例
完整的应用程序代码和一个现成的项目可以通过NECTO Studio包管理器直接安装到NECTO Studio。 应用程序代码也可以在MIKROE的GitHub账户中找到。
/*
* Solution Name: Button-Triggered Distance Measurement Solution
*
* Description:
* This embedded application uses Button Play Click and LightRanger 3
* Click to measure the distance to an object when a button is pressed.
* The button's backlight gradually fades in on press and fades out on release,
* providing visual feedback. Distance measurements are averaged over multiple
* readings to mitigate sensor noise and improve accuracy.
*
* The system utilizes the following Click boards:
* - Button Play Click: Captures button press/release events and controls
* LED backlight.
* - LightRanger 3 Click: Measures the distance to a nearby object using
* a laser sensor.
*
* The `application_init` function initializes the logger and both Click boards,
* ensuring proper startup conditions. Any initialization failure is logged,
* and the system halts.
*
* The `application_task` function monitors the button state and triggers
* distance measurements when the button is pressed. It takes multiple readings,
* calculates the average distance, and logs the result via UART.
*
* Hardware Setup:
* - MIKROBUS_1: Button Play Click
* - MIKROBUS_2: LightRanger 3 Click
*
* Key Features:
* - Button-controlled distance measurement with backlight feedback.
* - Multi-sample averaging to reduce sensor reading noise.
* - Smooth LED fade-in/out on button press/release.
* - UART logging for measurement data and system events.
*
* Development Environment:
* - [NECTO Studio](https://www.mikroe.com/necto)
* - [mikroSDK v2.0](https://www.mikroe.com/mikrosdk) framework
* - MIKROE [Click boards](https://www.mikroe.com/click-boards) Add-ons
*
* Author: Branko Jaksic
* Date: 2025
*/
// ------------------------------------------------------------------- INCLUDES
#include "log.h"
#include "board.h"
#include "buttonplay.h"
#include "lightranger3.h"
// ------------------------------------------------------------------ VARIABLES
static buttonplay_t buttonplay;
static lightranger3_t lightranger3;
static log_t logger;
// ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
/**
* @brief Initializes the Button Play Click board.
*
* Configures the Button Play Click board, sets up the PWM for LED
* backlight control, and starts the PWM signal. If initialization fails,
* the function logs an error and halts execution.
*
* @note The Button Play Click is connected to MIKROBUS_1.
*/
void initialize_button_play( void );
/**
* @brief Initializes the LightRanger 3 Click board.
*
* Configures the LightRanger 3 Click board and performs device initialization.
* If the sensor fails to initialize, the function logs an error and halts
* execution.
*
* @note The LightRanger 3 Click is connected to MIKROBUS_2.
*/
void initialize_light_ranger( void );
/**
* @brief Updates the button backlight brightness.
*
* Smoothly fades the button's backlight LED in or out, depending on the
* button state. When the button is pressed, the backlight gradually brightens.
* When released, the backlight gradually dims.
*
* @param[in] is_pressed Boolean indicating the button state:
* - `true`: Fade in the backlight.
* - `false`: Fade out the backlight.
*/
void update_button_backlight( bool is_pressed );
/**
* @brief Measures and logs the distance using the LightRanger 3 Click.
*
* Takes a single distance measurement, retrieves the distance value,
* and logs it via UART. This function is useful for quick,
* real-time distance readings.
*/
void measure_and_log_distance( void );
// ------------------------------------------------------ APPLICATION FUNCTIONS
void application_init( void )
{
log_cfg_t log_cfg;
// Logger initialization
LOG_MAP_USB_UART( log_cfg );
log_init( &logger, &log_cfg );
log_info( &logger, "Application Init" );
initialize_button_play();
initialize_light_ranger();
log_info( &logger, "Initialization Complete, Starting Application Task" );
}
void initialize_button_play( void )
{
buttonplay_cfg_t buttonplay_cfg;
buttonplay_cfg_setup( &buttonplay_cfg );
BUTTONPLAY_MAP_MIKROBUS( buttonplay_cfg, MIKROBUS_1 );
if ( buttonplay_init( &buttonplay, &buttonplay_cfg ) == PWM_ERROR )
{
log_error( &logger, "Button Play Click initialization failed!" );
for ( ; ; ); // Halt execution
}
buttonplay_set_duty_cycle( &buttonplay, 0.0 );
buttonplay_pwm_start( &buttonplay );
log_info( &logger, "Button Play Click initialized successfully." );
}
void initialize_light_ranger( void )
{
lightranger3_cfg_t lightranger3_cfg;
lightranger3_cfg_setup( &lightranger3_cfg );
LIGHTRANGER3_MAP_MIKROBUS( lightranger3_cfg, MIKROBUS_2 );
lightranger3_init( &lightranger3, &lightranger3_cfg );
if ( lightranger3_device_init( &lightranger3 ) != 0 )
{
log_error( &logger, "LightRanger Click initialization failed!" );
for ( ; ; ); // Halt execution
}
log_info( &logger, "LightRanger Click initialized successfully." );
}
void application_task( void )
{
static uint8_t button_state_old = 0;
uint8_t button_state = buttonplay_get_button_state( &buttonplay );
if ( button_state != button_state_old )
{
if ( button_state )
{
log_printf( &logger, " <-- Button Pressed --> \r\n" );
update_button_backlight( true );
// Measure distance multiple times and calculate the mean value
uint32_t distance_sum = 0;
uint8_t num_measurements = 5;
for ( uint8_t i = 0; i < num_measurements; i++ )
{
lightranger3_take_single_measurement( &lightranger3 );
distance_sum += lightranger3_get_distance( &lightranger3 );
Delay_ms( 100 ); // Small delay between measurements
}
uint16_t average_distance = ( uint16_t )( distance_sum / num_measurements );
log_printf( &logger, "Average Distance: %u mm\r\n", average_distance );
}
else
{
log_printf( &logger, " <-- Button Released --> \r\n" );
update_button_backlight( false );
}
button_state_old = button_state;
}
}
void update_button_backlight( bool is_pressed )
{
float duty_cycle;
if ( is_pressed )
{
for ( uint8_t n = 1; n <= 100; n++ )
{
buttonplay_set_duty_cycle( &buttonplay, (float)n / 100 );
Delay_ms( 10 );
}
}
else
{
for ( uint8_t n = 100; n > 0; n-- )
{
buttonplay_set_duty_cycle( &buttonplay, (float)n / 100 );
Delay_ms( 10 );
}
}
}
void measure_and_log_distance( void )
{
lightranger3_take_single_measurement( &lightranger3 );
uint16_t distance = lightranger3_get_distance( &lightranger3 );
log_printf( &logger, "Distance: %u mm\r\n", distance );
}
int main( void )
{
#ifdef PREINIT_SUPPORTED
preinit();
#endif
application_init();
while ( 1 )
{
application_task();
}
return 0;
}
额外支持
资源
类别:Human-Machine Interface (HMI)