Wierd数学与Arduino Uno
我正在尝试用我的arduino发出警报,该曲线总是在0:00激活,并在23:59停用,因此变量重置且更容易制作。现在,我想做的,蜂鸣器在1分钟后开始。但是数学实际上并没有工作,或者我缺少一些东西。
代码:
/* Tutorial:
* ---------
* -Set the custom Variables
* -Plug arduino at exact 0:00
*
*
*/
//Imports
//Custom Variables
int alarmMin = 1;
//Other Variables
boolean active = false;
int alarmMilli = alarmMin * 60000;
//Pins
int pBuzzer = 2;
//Start
void setup() {
Serial.begin(9600);
Serial.println("-------------------------------");
Serial.println(alarmMin);
Serial.println(alarmMilli);
}
//Loop
void loop() {
Serial.println(active);
Serial.println(millis()/1000);
Serial.println(alarmMilli/1000);
Serial.println("-----");
//If alarm-time started
if(millis()/1000 == alarmMilli/1000){
active = true;
}
//Buzzer
if(active){
digitalWrite(2, HIGH);
}
}
输出:
-------------------------------
1
-5536
0
0
-5
-----
0
0
-5
-----
0
0
-5
-----
0
0
-5
-----
...
为什么1*6000 = -5536
I'm trying to make an alarm with my arduino, which always activates at 0:00 and deactivates at 23:59 so the variables reset and its easier to make. Right now I want to make, that the buzzer starts after 1 minute. But the maths don't really work or I'm missing something.
Code:
/* Tutorial:
* ---------
* -Set the custom Variables
* -Plug arduino at exact 0:00
*
*
*/
//Imports
//Custom Variables
int alarmMin = 1;
//Other Variables
boolean active = false;
int alarmMilli = alarmMin * 60000;
//Pins
int pBuzzer = 2;
//Start
void setup() {
Serial.begin(9600);
Serial.println("-------------------------------");
Serial.println(alarmMin);
Serial.println(alarmMilli);
}
//Loop
void loop() {
Serial.println(active);
Serial.println(millis()/1000);
Serial.println(alarmMilli/1000);
Serial.println("-----");
//If alarm-time started
if(millis()/1000 == alarmMilli/1000){
active = true;
}
//Buzzer
if(active){
digitalWrite(2, HIGH);
}
}
Output:
-------------------------------
1
-5536
0
0
-5
-----
0
0
-5
-----
0
0
-5
-----
0
0
-5
-----
...
Why is 1*6000 = -5536
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
int
在Arduino Uno上键入16位整数,即它的范围从-32,768到327,67。因此,已有60,000个已经超出了范围,导致看似奇怪的数学。而不是
int
,使用long
。它的范围从-2,147,483,648到2,147,483,647。The
int
type on an Arduino Uno is a 16-bit integer, i.e. it's range its range is from -32,768 to 327,67. Thus, 60,000 is already outside the range resulting in seemingly strange math.Instead of
int
, uselong
. Its range is from -2,147,483,648 to 2,147,483,647.