当前位置:网站首页>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左右。第二个峰不够尖是量化误差导致。
边栏推荐
- 【luogu P4320】道路相遇(圆方树)
- Andorid 获取系统标题栏高度
- NC50965 Largest Rectangle in a Histogram
- Hdu3507 (slope DP entry)
- Introduction of UART, RS232, RS485, I2C and SPI
- Preview word documents online
- Kubernetes simple introduction to writing YML
- Sentry developer contribution Guide - configure pycharm
- Markdown使用教程
- Program analysis and Optimization - 9 appendix XLA buffer assignment
猜你喜欢

An excellent orm in dotnet circle -- FreeSQL

How SQLSEVER removes data with duplicate IDS

奥斯陆大学:Li Meng | 基于Swin-Transformer的深度强化学习
![[shutter] image component (load network pictures | load static pictures | load local pictures | path | provider plug-in)](/img/7e/4f9d96edd04e9ffb26434baf34aa43.jpg)
[shutter] image component (load network pictures | load static pictures | load local pictures | path | provider plug-in)
![[MCU project training] eight way answering machine](/img/a3/6a50619cd16269bf485a4a273677aa.jpg)
[MCU project training] eight way answering machine

Hdu3507 (slope DP entry)

Logback configuration file

The "2022 China Digital Office Market Research Report" can be downloaded to explain the 176.8 billion yuan market in detail

Teach you JDBC hand in hand -- structure separation

Unity learns from spaceshooter to record the difference between fixedupdate and update in unity for the second time
随机推荐
【AutoSAR 十 IO架构】
Tensorflow 2. Chapter 15 of X (keras) source code explanation: migration learning and fine tuning
node_modules删不掉
Multiprocess programming (4): shared memory
多进程编程(三):消息队列
An excellent orm in dotnet circle -- FreeSQL
线程的启动与优先级
Multiprocess programming (V): semaphores
Array common operation methods sorting (including ES6) and detailed use
1.12 - Instructions
Rust ownership (very important)
cordova-plugin-device获取设备信息插件导致华为审核不通过
NC24840 [USACO 2009 Mar S]Look Up
About qbytearray storage hexadecimal and hexadecimal conversion
Redis21 classic interview questions, extreme pull interviewer
MySQL 23道经典面试吊打面试官
NC24840 [USACO 2009 Mar S]Look Up
免费自媒体必备工具分享
Attributeerror: 'tuple' object has no attribute 'layer' problem solving
Wechat applet obtains the information of an element (height, width, etc.) and converts PX to rpx.