【问题标题】:going up the number without touching any button不按任何按钮就增加号码
【发布时间】:2021-08-21 08:25:11
【问题描述】:

我正在开发一个计算器,就像在计算器中按多少按钮一样工作。例如,我按了四次按钮,现在计算器中的数字是四。但问题是,当 Arduino 开启时,数字会在没有触摸按钮的情况下上升。我能做些什么?我正在使用数字显示器、Arduino UNO 板和四个按钮,这是我的代码:

    #include <TM1637.h>


// this is a calculater with four buttons on pins 2 3 4 5 and a number display and the driver is TM1637 CLK on pin 13 and DIO on pin 12. after making sure you connected
// all the modules you can program these codes on your Arduino UNO. LET'S JUTS MAKE IT!

const int btn_one = 2;
const int btn_two = 3;
const int btn_three = 4;
const int btn_four = 5;
/*
int b1s = LOW;
int b2s = LOW;
int b3s = LOW;
int b4s = LOW;
*/

int num1 = 0;
int num2 = 0;
int mathop = 1;


int CLK = 13;
int DIO = 12;

TM1637 tm(CLK,DIO);

void setup(){
  tm.init();
  tm.set(7);
  pinMode(btn_one, INPUT);
  pinMode(btn_two, INPUT);
  pinMode(btn_three, INPUT);
  pinMode(btn_four, INPUT);
  
}

void loop(){
  int b1s = digitalRead(btn_one);
  int b2s = digitalRead(btn_two);
  int b3s = digitalRead(btn_three);
  int b4s = digitalRead(btn_four);

  if(b1s == true){
    num1 = num1 + 1;
    displayNumber(num1);
    //digitalWrite(btn_one, LOW);
    delay(1000);
  }
  
}

void displayNumber(int num){   
    tm.display(3, num % 10);   
    tm.display(2, num / 10 % 10);   
    tm.display(1, num / 100 % 10);   
    tm.display(0, num / 1000 % 10);
}

这些是我的联系: Connections 1 Connections 2

如果你能帮助我,请给我一个对我很有帮助的答案,非常感谢。

【问题讨论】:

  • 你的输入引脚是浮动的
  • 我能做什么?我是 arduino 的新手
  • 跟随一个基本的按钮教程。

标签: arduino calculator


【解决方案1】:

我看到了你的按钮连接电路。主要问题在于电路本身。您没有在按钮和数字输入引脚上添加电阻器,因此,digitalRead 读取的按钮状态是介于 0 和 1 之间的浮点值(假值)。按照这里给出的电路: https://www.arduino.cc/en/Tutorial/BuiltInExamples/Button 每个按钮最好使用 10k 欧姆的电阻。

其次,你为什么使用延迟 1秒?

【讨论】:

  • 请不要回答离题的问题。 Stack Overlow 是关于编码的。如果您想回答 Arduino 问题,我们有 Arduino Stack Exchange
猜你喜欢
  • 1970-01-01
  • 2020-09-10
  • 2023-03-05
  • 2021-12-27
  • 2016-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多