Wierd数学与Arduino Uno

发布于 2025-01-24 04:22:12 字数 1039 浏览 2 评论 0原文

我正在尝试用我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

燕归巢 2025-01-31 04:22:12

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, use long. Its range is from -2,147,483,648 to 2,147,483,647.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文