当前位置:网站首页>Arduino开发之按键检测与正弦信号输出
Arduino开发之按键检测与正弦信号输出
2022-07-02 23:53:00 【公子_羽】
准备材料:
Arduino Uno * 1
按键*2
电阻(860Ω)*2
舵机*1
电路连线:
注意:按键要接上拉电阻。否则电平会随机变化,导致检测错误。
实验代码:
double pi = 3.14159265354;
double t = 0.0; //单位:s
double dt = 0.0; //单位:s
unsigned long nLastTime = 0;
int pulse = 0;
float w = 0.1;
int pwm_channel = 3;
int freq_channel = 7;
int begin_channel = 5;
int adn_pmax = 254;
int adn_pmin = 128;
int delta_adn_pwm = 0;
int adn_pmid = 0;
int pwm_out_min = 180;
int pwm_out_max = 206;
int pwm_out_mid = 193;
float cnt = 1.0;
float last_cnt;
int stage = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(pwm_channel,OUTPUT);
pinMode(freq_channel,INPUT);
pinMode(begin_channel,INPUT);
nLastTime = micros();
}
void loop() {
// put your main code here, to run repeatedly:
int freq_val = digitalRead(freq_channel);
int begin_val = digitalRead(begin_channel);
if(stage == 0)
if(freq_val == LOW){
stage = 1;
}
if(stage == 1)
if(freq_val == HIGH){
cnt++;
stage = 0;
}
if(w<1){
w = (cnt / 10);
}
else if( (w>=1) && (w<10) ){
if(last_cnt != cnt){
w = w + 1.0;
}
}
else{
if(last_cnt != cnt){
w = w + 10.0;
}
}
last_cnt = cnt;
// Serial.print(cnt); Serial.print('\t');
double nNowTime = micros(); //单位:微秒
dt = (nNowTime - nLastTime)/1e6;
t = t + dt + 6.6e-5;
nLastTime = micros(); //单位:微秒
Serial.print(w); Serial.print('\t');
if(begin_val == LOW){
pulse = int(pwm_out_mid + 0.5*(pwm_out_max-pwm_out_min)*sin(2*pi*w*t));
constrant();
}
else{
pulse = pwm_out_mid;
}
analogWrite(pwm_channel,pulse);
Serial.print(pulse); Serial.print('\t');
Serial.print(dt); Serial.print('\t');
Serial.println(t,6);
}
void constrant(void){
if(pulse>=pwm_out_max)
pulse = pwm_out_max;
if(pulse<=pwm_out_min)
pulse = pwm_out_min;
}实验要点:
1.按键计数不仅要判断是否按下,还要判断是否抬起,否则计数器会疯狂累加。
2.特定频率正弦信号输出,Ts在Te之后,越靠近Te越好,dt的误差越小。图中代码dt的误差为。”Ts = micros()“ 记录当前时刻的定时器数值,直到下一个周期“Te = micro()”之间所有语句的执行时间都被计算在内。因此误差为两条“dt = double((Te - Ts)/1e6);t = t + dt;”语句执行的时间,加上6.6e-5正好补偿这两条语句的误差。
实验结果:
通过串口将输出的数据在matlab上画出来。结果如下:

上图为1Hz的正弦信号输出,可以看到两个峰值之间的时间差在1s左右。第二个峰不够尖是量化误差导致。
边栏推荐
- 【单片机项目实训】八路抢答器
- Callback event after the antv X6 node is dragged onto the canvas (stepping on a big hole record)
- Centos7 one click compilation to build MySQL script
- 1.11 - bus
- 在线预览Word文档
- antv x6节点拖拽到画布上后的回调事件(踩大坑记录)
- University of Toronto: Anthony coach | the conditions of deep reinforcement learning can induce dynamic risk measurement
- [jetcache] jetcache configuration description and annotation attribute description
- JSON conversion tool class
- Overlay of shutter (Pop-Up)
猜你喜欢

Markdown使用教程

【AutoSAR 四 BSW概述】

【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(中)-- 搜索建议

Hundreds of continuous innovation to create free low code office tools

Teach you JDBC hand in hand -- structure separation

University of Toronto:Anthony Coache | 深度强化学习的条件可诱导动态风险度量

MySQL 23 classic interview hanging interviewer

Is there a free text to speech tool to help recommend?

Multiprocess programming (I): basic concepts
![[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)](/img/73/19e2e0fc5ea6f05e34584ba40a452d.jpg)
[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)
随机推荐
如何系统学习机器学习
mm中的GAN模型架构
Multiprocess programming (V): semaphores
The most painful programming problem in 2021, adventure of code 2021 Day24
免费自媒体必备工具分享
图解网络:什么是虚拟路由器冗余协议 VRRP?
NC17059 队列Q
About qbytearray storage hexadecimal and hexadecimal conversion
【AutoSAR 八 OS】
UART、RS232、RS485、I2C和SPI的介绍
Win10 多种方式解决无法安装.Net3.5的问题
Helm basic learning
利亚德:Micro LED 产品消费端首先针对 100 英寸以上电视,现阶段进入更小尺寸还有难度
Markdown使用教程
Leetcode 294. Flip game II (game theory)
Linux Software: how to install redis service
【Pulsar文档】概念和架构/Concepts and Architecture
MySQL 23 classic interview hanging interviewer
Sentry developer contribution Guide - configure pycharm
Program analysis and Optimization - 9 appendix XLA buffer assignment