当前位置: 首页 > news >正文

MATLAB+Arduino利用板上的按键控制板上Led灯

几年不使用,之前的知识都忘掉了。需要逐步捡起来。

1 熟悉按键的使用

2熟悉灯的控制

1 电路

我们将通过 MATLAB 的 Arduino 支持包与 Arduino 板通信,读取按键状态并控制 LED 灯的亮灭。

  1. 按键:连接到 Arduino 的数字引脚(例如 D2),另一端接地。(arduino UNO板子上自带了两个按键,通过杜邦线将KEY-L连接到D2

  2. LED:连接到 Arduino 的数字引脚(例如 D13,Arduino Uno 自带 LED),另一端通过限流电阻接地。(uno板自带

2 功能

使用 MATLAB 和 Arduino 实现按键控制 LED 灯的程序。

3 代码

app中查找arduino资源管理器

查看com口,方便编码用(如果是matlab2022版本及以上,且只有一个arduino板挂在电脑上,可省略com口,matlab可以自己查到)

编码: 按键为低电平。

arduinoObj = arduino("COM5", "Uno")
%Configure specified Arduino pins.
configurePin(arduinoObj, "D13", "DigitalOutput");
configurePin(arduinoObj, "D2", "Pullup"); %必须上拉,否则按键无法正确识别


% main code
disp('按下按键控制 LED 灯(按 Ctrl+C 退出)...');
while true
    % 读取按键状态
    buttonState = readDigitalPin(arduinoObj, 'D2');

    % 控制 LED 灯
    if buttonState == 0 % 按键按下(假设按键按下为低电平)
        writeDigitalPin(arduinoObj, 'D13', 1); % 点亮 LED
        disp('LED 灯亮');
    else
        writeDigitalPin(arduinoObj, 'D13', 0); % 熄灭 LED
        disp('LED 灯灭');
    end

    pause(0.5); % 稍微延迟,避免过于频繁读取
end

% 清理
% clear a;
disp('程序结束');
%Clean Up
%Clear variables that are not needed.
clear arduinoObj

4 结果

按键和LED关联功能实现。按键一定要用上拉功能。

5 通过按键判断是否停止程序

手动结束while true 会让arduino产生的通信信息无法清除,为此把arduino板自带的KEY_R按键使用,用它来控制程序启停

KEY_R键必须按下,然后再按KEY——L键 ,程序才能一致执行;否则就会直接退出while循环。

Create an Arduino Connection
Create an Arduino connection using the specified device and connection parameters.
arduinoObj = arduino("COM5", "Uno")

Configure Arduino Pins
Configure specified Arduino pins.
configurePin(arduinoObj, "D13", "DigitalOutput");
configurePin(arduinoObj, "D2", "Pullup");
configurePin(arduinoObj, "D3", "Pullup");

Read and Write data
Read data from and write data to Arduino pins configured above.

disp('按下按键控制 LED 灯(按 Ctrl+C 退出)...');
stopState = readDigitalPin(arduinoObj, 'D3')% 这是反向的?
buttonState = readDigitalPin(arduinoObj, 'D2')
while stopState~=0 %必须一致按着KEY R,才能才能一致循环
    % 读取按键状态
    buttonState = readDigitalPin(arduinoObj, 'D2');
    stopState = readDigitalPin(arduinoObj, 'D3');
    % 控制 LED 灯
    if buttonState == 0 % 按键按下(假设按键按下为低电平)
        writeDigitalPin(arduinoObj, 'D13', 1); % 点亮 LED
        disp('LED 灯亮');
    else
        writeDigitalPin(arduinoObj, 'D13', 0); % 熄灭 LED
%         disp('LED 灯灭');
    end

    pause(0.5); % 稍微延迟,避免过于频繁读取
end

% 清理
% clear a;
disp('程序结束');

相关文章:

  • Ant Design Vue Select 选择器 全选 功能
  • 深度学习核心算法
  • git 基础操作
  • Python---数据分析(Pandas十:二维数组DataFrame统计计算一)
  • SpringCloud介绍
  • SQL的DCL,DDL,DML和DQL分别是什么
  • 运维--个人打包脚本分享(Linux)
  • canvas数据标注功能简单实现:矩形、圆形
  • 2025年如何避免使用验证码求解器时被IP封禁
  • (每日一道算法题)翻转对
  • 基于 ECharts 实现动态图表渲染支持10万+数据点实时更新方案
  • Flutter使用自签证书打包ipa
  • Excel 小黑第19套
  • 基于springboot的美容院管理系统(044)
  • 深圳问顶安全科技有限公司asktopsec是做什么的?
  • 事件、页面跳转、wxml语法——微信小程序学习笔记
  • 【AI】Orin Nano+ubuntu22.04上移植YoloV11,并使用DeepStream测试成功
  • How to share files with Windows via samba in Linux mint 22
  • MyBatisPlus(SpringBoot版)学习第二讲:基本CRUD
  • 安装PrettyZoo操作指南
  • 美股全线收涨:道指涨逾千点,纳斯达克中国金龙指数涨5.4%
  • 长沙查处疑似非法代孕:有人企图跳窗,有女子被麻醉躺手术台
  • 美英贸易协议|不,这不是一份重大贸易协议
  • “海豚音”依旧互动更多,玛丽亚·凯莉本周来沪开唱
  • 湖北石首市委副书记、市长付勇接受审查调查
  • 富家罹盗与财富迷思:《西游记》与《蜃楼志》中的强盗案