1

是的,标题没有意义,我的问题也没有意义。

(在 Win 10 上使用 Arduino Uno、Eclipse 作为 IDE 和 Sloeber 插件)

我只是想读取一个简单的按钮按下并使用软件去抖动来清理它的信号。我在这里按照教程:https ://www.arduino.cc/en/Tutorial/Debounce

我最终将拥有三个按钮来读取和去抖动,但我只是想让“保存”按钮立即工作。一旦我让它实际打印“保存”,我应该计划添加代码以写入 EEPROM。不过那是后来的。

我的程序进入:

//if the save reading is different to the save state
if (SaveReading!=SaveState){
  Serial.println("savereading != savestate");
  SaveState=SaveReading;
  if (SaveState==HIGH){
    Serial.println("Save");
  }
}

仅当我在上一节中将一定数量的字符打印到串行监视器时才声明:

//if the save button is in a different state to the last saved state:
if (SaveReading!= lastSaveState){
  //reset debounce timer
  lastDebounceTime=millis();

  Serial.println("change detected");
  Serial.println("Save reading= ");
  Serial.println("1");

  Serial.println("last 123=");
  Serial.println("2");

}

最初打印的所有数字都是文字,但为了隔离 WTF 正在进行,我删除了文本,直到它坏掉。我认为这取决于字符数,因为我已经合并了几个 Serial.println() 行并保持字符数相同并且它仍然有效。

这是否存在根本性的问题,使其依赖于打印到串行监视器的内容?自从我弄乱 Arduino 以来已经很久了,所以我可能会遗漏一些简单的东西。

我确实尝试过使用 delay() 而不是 serial.println() ,但这似乎不起作用,而且我真的不想要实际的延迟。

我确实看到了这个问题:Arduino Serial.println 奇怪的错误

但是我检查了一下,我认为我没有遇到同样的问题,尽管症状看起来确实是一样的。

谁能看到我错过了什么?我确实考虑过放弃它并使用 Bounce2 库,但我想如果我自己能做到,我实际上会知道发生了什么。

完整代码如下:

// constants won't change. They're used here to set pin numbers:
const int hallPin = 12;     // the number of the hall effect sensor pin
const int SaveButtonPin=11; //GREEN
const int ResetButtonPin=10;//YELLOW
const int UnitButtonPin=9;  //BLACK

// variables will change:
int hallState = 0;          // variable for reading the hall sensor status
int oldhallState=0;         //to store previous hall-state reading.
int counter=0;

//save button
int SaveState=LOW;
int lastSaveState=LOW;

//reset button
int ResetState;
int lastResetState=LOW;

//unit button
int UnitState;
int lastUnitState=LOW;

//instantaneous readings
int SaveReading;  //temporary variable for checking debounce status.
int ResetReading;
int UnitReading;

unsigned long lastDebounceTime=0;
unsigned long debounceDelay=50;

void setup() {
  Serial.begin(9600);//initialize the Baud-rate for the Serial Monitor.

  //set input pins as inputs
  pinMode(SaveButtonPin, INPUT);
  pinMode(ResetButtonPin,INPUT);
  pinMode(UnitButtonPin,INPUT);
  pinMode(hallPin, INPUT);

}

void loop(){
  //check the save button.
  SaveReading=digitalRead(SaveButtonPin);

  //if the save button is in a different state to the last saved state:
  if (SaveReading!= lastSaveState){
    //reset debounce timer
    lastDebounceTime=millis();

    Serial.println("change detected");
    Serial.println("Save reading= ");
    Serial.println("1");

    Serial.println("last 123=");
    Serial.println("2");

  }

  //if (time since program started)-(time at which the save state changed) is greater than the
  //debounce delay, then:
  if((millis()-lastDebounceTime)>debounceDelay){

    //if the save reading is different to the save state
    if (SaveReading!=SaveState){
      Serial.println("savereading != savestate");
      SaveState=SaveReading;
      if (SaveState==HIGH){
        Serial.println("Save");
      }
    }
  }


  ResetReading=digitalRead(ResetButtonPin);
  UnitReading=digitalRead(UnitButtonPin);

  // read the state of the hall effect sensor:
  hallState = digitalRead(hallPin);

  if (hallState == LOW) {
    if(hallState!=oldhallState){
      counter++;
      Serial.println(counter);
    }
    oldhallState=hallState; //set hall state of (t-1)
  }
  else {
    oldhallState=hallState; //set hall state of (t-1)
  }

  lastSaveState=SaveState;

}
4

0 回答 0