2021年7月4日 星期日

DebounceTime monitor 要等一段時間才按就會顯示

 const int buttonPin =2;

int buttonState;

int lastButtonState=LOW;

int lastDebounceTime=0;

long buttonPushCounter=0;

long debounceDelay=50;

void setup() {

  // put your setup code here, to run once:

  pinMode(buttonPin,INPUT);

  Serial.begin(9600);

}


void loop() {

  // put your main code here, to run repeatedly:

  int reading=digitalRead(buttonPin);

  if(reading != lastButtonState)

  {

    lastDebounceTime=millis();

  }

  if((millis()-lastDebounceTime)>debounceDelay)

  {

    if(reading !=buttonState)

    {

      buttonState=reading;

      if(buttonState==HIGH)

      {

        buttonPushCounter++;

        Serial.print("number of button pushes");//沒有ln為不換行

        Serial.println(buttonPushCounter);//ln為換行 var沒雙引號

      }

      else

      {

        Serial.println("off");

      }

    }

  lastButtonState=reading;

}

}

沒有留言:

張貼留言

algorithm

 #include <iostream> #include <string.h> using namespace std; int main(int argc, char** argv)  { for(int j=2;j<=100;j++)//j...