/*
你可能会用到的函数:
millis()
digitalRead();
附:
arduino 语法手册:http://arduino.cc/en/Reference/HomePage
*/
#define PIN_key 4 //按键输入引脚
#define Key_Pressed 0
unsigned long time; //可供调用的变量
boolean paper_sta = 0; //可供调用的变量
int paper_num = 0; //格数,最后需要打印出的变量
boolean key, key_cache; //可供调用的变量
boolean getKeyStatus()
{
return digitalRead(PIN_key);
}
void setup()
{
Serial.begin(9600);
pinMode(PIN_key, INPUT_PULLUP);
}
void loop()
{
//按键检测计数
if (Key_Pressed == getKeyStatus())
{
delay(20);
if (Key_Pressed == getKeyStatus())
{
key = 1;
if (0 == time)
{
time = millis();
paper_sta = 1;
}
}
}
else
{
if (paper_sta && (1 == key))
{
paper_num++;
}
key = 0;
}
//定时输出
if ((millis()>=(time + 1000)) && (1 == paper_sta))
{
time = 0;
paper_sta = 0;
Serial.print("1S内按键的次数为: ");
Serial.print(paper_num);
paper_num = 0;
}
}