// 按钮开关: GND ---按钮--- Pin 1
///
int bounce = 88; // bounce time for 抖动判断; you can change it
int btn = 1; // you can use pin 1, pin 0
int state = 0; // 0, 1 两个状态
///
int bt00 = 0; // pin0 if you hava a button on it
int state00 = 0; // for Button on pin0 if have
void setup( ) {
pinMode (btn, INPUT_PULLUP); // pull High
// ...
//...
} // setup(
void loop( ) {
//...
if( click( ) ) { // 按一下 Button on Pin 1
state = 1 - state; // 切换状态
}
// check state to do some thing
// if(state == 1) ...;
// else ...;
//...
} // loop(
//
int highLow = HIGH; //没按下是 HIGH (因INPUT_PULLUP)
int highLow00 = HIGH; // for example a Button on pin0
unsigned long lastHit01 = 0;
unsigned long lastHit00 = 0; // for Button on pin 0
bool clickCheck(int b, int& status, unsigned long& lastTime); // declare
bool click( ) {
return clickCheck(btn, highLow, lastHit01);
}
bool click00( ) {
return clickCheck(bt00, highLow00, lastHit00);
}
//
bool clickCheck(int bt, int& xxStat, unsigned long& lastHit) {
int a = digitalRead(bt);
if(a == xxStat) return false; // no change
unsigned long m = millis( );
if(a == LOW) { // 压下 !
if(m - lastHit < bounce) return false; // ignore
lastHit = m; // record the time
xxStat = a; // LOW
return true; // Yes, you hit it
}
// released button ( Low --> HIGH)
xxStat = a; // HIGH
return false;
}
/// End of the Program
// by tsaiwn@ydu.edu.tw 育达科大 多媒体与游戏系; 苗栗; 台湾
//==========================================================