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;
}
}
沒有留言:
張貼留言