如何制作我的“闹钟”?工作? [英] How can I make my "alarm clock" work?

查看:103
本文介绍了如何制作我的“闹钟”?工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,大家好,



我是Arduino编程环境的新手,我想我想创建一个闹钟来工作一点点咬按钮。 闹钟与Piezospeaker和两个按钮配合使用,仅此而已。这背后的想法是分别按下两个按钮,当按下两个按钮时(和)它们停止我的闹钟。现在的问题是我的扬声器不仅仅是(HIGH)它有节奏:



 digitalWrite(扬声器,HIGH) ; 
延迟( 500 );
digitalWrite(speaker,LOW);
延迟( 500 );





但我发现了那个延迟我有一些麻烦。它随时都不起作用。没有延迟,它可以正常工作,但随着延迟越高,它往往不起作用。



我的代码:



  int  speaker =  2 ; 
int taster_1 = 7 ;
int taster_2 = 8 ;
int taster_status_1 = 0 ;
int taster_status_2 = 0 ;
bool taster1_off = 0 ;
bool taster2_off = 0 ;

bool speaker_status = true ;

void setup(){
pinMode(speaker,OUTPUT);
pinMode(taster_1,INPUT);
pinMode(taster_2,INPUT);
digitalWrite(speaker,HIGH);
}

void loop(){

taster_status_1 = digitalRead(taster_1);
taster_status_2 = digitalRead(taster_2);

if (taster_status_1 == HIGH){
taster1_off = 1 ;
} 其他 如果(taster_status_2 == HIGH){
taster2_off = 1 ;
}

if (taster1_off == 1 taster2_off == 1 ){ // 当按下两个按钮时,警报应该停止
speaker_status = false ;
}

if (speaker_status == true ){< span class =code-comment> // 闹钟的节奏,只要speaker_status为真,它就会继续
digitalWrite(扬声器,HIGH);
延迟( 500 );
digitalWrite(speaker,LOW);
延迟( 500 );
}

}





我尝试过:



我尝试使用

 attachInterrupt()

和计时器,但我认为它不能按照我想要的方式工作。我真的很期待一个暗示。



我真的很感激有点提示,非常感谢你的期待!

解决方案

Part问题是延迟功能 [ ^ ]停止程序runnign指定的时间 - 500ms或半秒。你快速连续两次调用它,所以它只能每秒快速检查一次按钮。所以你不能只是按下按钮 - 你必须按住它们。



当你这样做时,你的代码没有按照你的想法行事,因为:

 如果(taster_status_1 == HIGH){
taster1_off = 1 ;
} 其他 如果(taster_status_2 == HIGH){
taster2_off = 1 ;
}

表示按下第一个按钮,它从不检查另一个按钮,因此

此测试将始终失败:

  if (taster1_off ==  1   taster2_off ==  1 ){ //  当按下两个按钮时,闹钟应该停止 



取出 else 它应该有效:

 如果(taster_status_1 == HIGH){
taster1_off = 1 ;
}
如果(taster_status_2 == HIGH){
taster2_off = 1 ;
}


您的压电蜂鸣器是所谓的外部驱动器类型。这意味着它必须用交替信号驱动。蜂鸣器频率取决于类型(典型值在1到9 kHz范围内)。虽然它适用于某些频率,但它在指定频率下具有最大强度。所以你应该查看你的蜂鸣器的频率。



如果频率是例如4.8 kHz,则周期时间是0.21毫秒(1/4800)和开/关时间必须是该时间的一半(0.105 ms)。这远远小于500毫秒的延迟。如果你使用2 ms的延迟,信号将是250 Hz,这应该是可以接受的。



更好的解决方案是使用定时器或PWM输出(I不知道Arduino,但似乎有一个PWM输出: Arduino - PWM [ ^ ])。



最后一些警告:



切勿长时间对压电蜂鸣器施加直流电压。因此,请确保在终止程序时始终清除输出。



压电蜂鸣器在发生热和机械冲击时会产生高电压并产生反向电压(特别是在使用矩形驱动时)信号)可能会破坏Arduino端口。为避免这种情况,请使用两个串联的齐纳二极管(阴极到阴极)并联蜂鸣器或用一个晶体管驱动蜂鸣器(也可以将一个1千欧的电阻并联到蜂鸣器,以保护晶体管免受反向电压的影响)。

Hey there to all,

I am new in the Arduino programming-environment and I thought I want to create me an "alarm clock" to work a little bit with the buttons. The "alarm clock" works with a Piezospeaker and two buttons, nothing more. The idea behind this is to press both buttons separately and when both buttons are pressed (AND) they stop my alarm clock. Now the problem is that my speaker isn't just on (HIGH) it have a rhythm:

digitalWrite(speaker, HIGH);
delay(500);
digitalWrite(speaker, LOW);
delay(500);



But I discovered that with that delay I have some troubles. It doesn't work anytime. Without a delay it works properly but as higher the delay it more tend to doesn't work.

My code:

int speaker = 2;
int taster_1 = 7;
int taster_2 = 8;
int taster_status_1 = 0;
int taster_status_2 = 0;
bool taster1_off= 0;
bool taster2_off = 0;

bool speaker_status = true;

void setup() {
  pinMode(speaker, OUTPUT);
  pinMode(taster_1, INPUT);
  pinMode(taster_2, INPUT);
  digitalWrite(speaker, HIGH);
}

void loop() {

  taster_status_1 = digitalRead(taster_1);
  taster_status_2 = digitalRead(taster_2); 

  if ( taster_status_1 == HIGH) { 
     taster1_off = 1;
  } else if ( taster_status_2 == HIGH) {
      taster2_off = 1;
  }

  if (taster1_off == 1 and taster2_off == 1){ //when both buttons were pressed the alarm should stop
    speaker_status = false;
  }

  if (speaker_status == true) { // rhythm of the alarm, as long speaker_status is true it goes on and on
    digitalWrite(speaker, HIGH);
    delay(500);
    digitalWrite(speaker, LOW);
    delay(500);
  }

}



What I have tried:

I tried with

attachInterrupt()

and Timers but I think it doesn't work the way I want to. I really looking forward for a hint.

I would really appreciate a little hint and a really huge thank you in anticipation!

解决方案

Part of the problem is that the delay function[^] stops your program runnign for the ammount of time you specify - 500ms or half a second. And you call it twice in quick succession, so it only checks the buttons quickly once a second. So you can't just "push the buttons" - you have to hold them down.

And when you do, you code doesn't do what you think, because:

if ( taster_status_1 == HIGH) { 
     taster1_off = 1;
  } else if ( taster_status_2 == HIGH) {
      taster2_off = 1;
  }

means that is teh first button is pressed, it never checks the other, and so
this test will always fail:

if (taster1_off == 1 and taster2_off == 1){ //when both buttons were pressed the alarm should stop


Take out the else and it should work:

if ( taster_status_1 == HIGH) { 
     taster1_off = 1;
  } 
if ( taster_status_2 == HIGH) {
      taster2_off = 1;
  }


Your piezo buzzer is a so called "external drive type". That means it must be driven with an alternating signal. The buzzer frequency is type depending (typical values are in the range of 1 to 9 kHz). While it will work with some frequencies, it will have maximum intensity at the specified frequency. So you should look up the frequency for your buzzer.

If the frequecy is for example 4.8 kHz, the period time is 0.21 milli seconds (1 / 4800) and the on / off times must be half that time (0.105 ms). This is much less than your delays of 500 ms. If you use a delay of 2 ms, the signal will be 250 Hz which should still be hearable.

A better solution would be using a timer or PWM output (I don't know the Arduino but it seems that there is a PWM output: Arduino - PWM[^]).

Finally some warnings:

Never apply DC voltage to piezo buzzers for long time periods. So ensure that the output is always cleared when terminating your program.

Piezo buzzers can cause high voltages upon thermical and mechanical shocks and induce reverse voltages (especially when driven with rectangular signals) that may destroy the Arduino port. To avoid this use two Zener diodes in series (cathode to cathode) connected in parallel to the buzzer or drive the buzzer with a transistor (than put also a 1 kOhm resistor in parallel to the buzzer to protect the transistor from the reverse voltage).


这篇关于如何制作我的“闹钟”?工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆