|
(原創(chuàng)) 不用delay, 不阻塞運行!
**************************************
int b2 = 0; //button state, default HIGH
bool b2on = false;
int wait = 0; //counter when key is hold
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
}
void loop() {
// read the state of the pushbutton value:
b2 = digitalRead(buttonPin1);
if( b2 == LOW ) // pressed, from HIGH->LOW
{
wait++; // wait for release
}
if( b2 && wait > 39) // button return back to HIGH, and LOW for 39 times
{
b2on = !b2on; // one CLICK finished!
wait = 0; // reset
}
if (b2on) {
// turn LED on:
digitalWrite(ledPin1, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin1, LOW);
}
**************************************
|
評分
-
查看全部評分
|