当前位置:网站首页>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左右。第二个峰不够尖是量化误差导致。
边栏推荐
- node_ Modules cannot be deleted
- 【雅思阅读】王希伟阅读P2(阅读填空)
- 如何系统学习机器学习
- 图解网络:什么是虚拟路由器冗余协议 VRRP?
- 利亚德:Micro LED 产品消费端首先针对 100 英寸以上电视,现阶段进入更小尺寸还有难度
- Detailed explanation of pod life cycle
- Introduction of UART, RS232, RS485, I2C and SPI
- University of Toronto: Anthony coach | the conditions of deep reinforcement learning can induce dynamic risk measurement
- 多进程编程(四):共享内存
- NC24840 [USACO 2009 Mar S]Look Up
猜你喜欢
Multiprocess programming (I): basic concepts
文件操作IO-Part2
Unity learns from spaceshooter to record the difference between fixedupdate and update in unity for the second time
Linux软件:如何安装Redis服务
[IELTS reading] Wang Xiwei reading P1 (reading judgment question)
Rust所有权(非常重要)
【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(中)-- 搜索建议
[MCU project training] eight way answering machine
Win10 多种方式解决无法安装.Net3.5的问题
Introduction and use of ftrace tool
随机推荐
Some introduction and precautions about XML
NC24325 [USACO 2012 Mar S]Flowerpot
Wechat applet obtains the information of an element (height, width, etc.) and converts PX to rpx.
Nc17059 queue Q
字符设备注册常用的两种方法和步骤
【单片机项目实训】八路抢答器
Why is the website slow to open?
【AutoSAR 十二 模式管理】
node_ Modules cannot be deleted
【雅思阅读】王希伟阅读P2(阅读填空)
多进程编程(四):共享内存
Automated defect analysis in electron microscopic images-论文阅读笔记
node_modules删不掉
AttributeError: ‘tuple‘ object has no attribute ‘layer‘问题解决
[MCU project training] eight way answering machine
详解用OpenCV的轮廓检测函数findContours()得到的轮廓拓扑结构(hiararchy)矩阵的意义、以及怎样用轮廓拓扑结构矩阵绘制轮廓拓扑结构图
腾讯云免费SSL证书扩展文件含义
百数不断创新,打造自由的低代码办公工具
JSON转换工具类
Multiprocess programming (I): basic concepts