当前位置:网站首页>Key detection and sinusoidal signal output developed by Arduino
Key detection and sinusoidal signal output developed by Arduino
2022-07-03 00:47:00 【Childe_ plume】
Prepare the material :
Arduino Uno * 1
Key *2
resistance (860Ω)*2
The steering gear *1
Circuit connection :
Be careful : The button should be connected with pull-up resistance . Otherwise, the level will change randomly , Cause detection error .
Experimental code :
double pi = 3.14159265354;
double t = 0.0; // Company :s
double dt = 0.0; // Company :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(); // Company : Microsecond
dt = (nNowTime - nLastTime)/1e6;
t = t + dt + 6.6e-5;
nLastTime = micros(); // Company : Microsecond
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;
}Key points of the experiment :
1. The key count is not only to determine whether to press , Also determine whether to lift , Otherwise, the counter will accumulate madly .
2. Specific frequency sinusoidal signal output ,Ts stay Te after , The closer we get Te The better ,dt The smaller the error . Code in Figure dt The error of is .”Ts = micros()“ Record the timer value at the current time , Until the next cycle “Te = micro()” The execution time of all statements between is counted . So the error is two “dt = double((Te - Ts)/1e6);t = t + dt;” Statement execution time , add 6.6e-5 Just compensate for the error of these two sentences .
experimental result :
The output data is stored in matlab Draw it up . give the result as follows :

The picture above shows 1Hz Sinusoidal signal output of , It can be seen that the time difference between the two peaks is 1s about . The second peak is not sharp enough, which is caused by quantization error .
边栏推荐
- [shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)
- Lex & yacc & bison & flex configuration problems
- 【AutoSAR 三 RTE概述】
- helm 基础学习
- Kubernetes simple introduction to writing YML
- leetcode-224:基本计算器
- 1.11 - bus
- Centos7 one click compilation to build MySQL script
- 【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(中)-- 搜索建议
- Shell 实现文件基本操作(sed-编辑、awk-匹配)
猜你喜欢
![[MCU project training] eight way answering machine](/img/a3/6a50619cd16269bf485a4a273677aa.jpg)
[MCU project training] eight way answering machine
![[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)

Rust字符串切片、结构体和枚举类

AEM: Nanlin fan Ben et al. - plant rhizosphere growth promoting bacteria control soybean blight

Linux软件:如何安装Redis服务

百度智能云牵头打造智能云综合标准化平台

百数不断创新,打造自由的低代码办公工具

How SQLSEVER removes data with duplicate IDS

ftrace工具的介绍及使用

奥斯陆大学:Li Meng | 基于Swin-Transformer的深度强化学习
随机推荐
How SQLSEVER removes data with duplicate IDS
mm中的GAN模型架构
About the practice topic of screen related to unity screen, unity moves around a certain point inside
Tensorflow 2.x(keras)源码详解之第十五章:迁移学习与微调
机器学习:numpy版本线性回归预测波士顿房价
Illustrated network: what is virtual router redundancy protocol VRRP?
【JetCache】JetCache的配置说明和注解属性说明
Some introduction and precautions about XML
[IELTS reading] Wang Xiwei reading P1 (reading judgment question)
微信小程序获取某个元素的信息(高、宽等),并将px转换为rpx。
Extension of flutter
[jetcache] jetcache configuration description and annotation attribute description
【AutoSAR 三 RTE概述】
Shell implements basic file operations (SED edit, awk match)
JSON conversion tool class
kubernetes编写yml简单入门
[golang syntax] map common errors golang panic: assignment to entry in nil map
2022中国3D视觉企业(引导定位、分拣场景)厂商名单
leetcode-2280:表示一个折线图的最少线段数
图解网络:什么是虚拟路由器冗余协议 VRRP?