int reading;
boolean led_state ;//你宣告int led_state也可以pass
int btn_state;
int last_btn_state = LOW;
long last_debounce_time = 0;
void setup()
{
Serial.begin(9600);
pinMode(10, OUTPUT);
pinMode(2, INPUT);
}
void loop()
{
reading = digitalRead(2);
if (reading != last_btn_state)
{
last_debounce_time = millis();
}
if ((millis() - last_debounce_time) > 50)
{
if (reading != btn_state)
{
btn_state = reading;//先交插state reading swap
if (btn_state == LOW)
{
led_state=!led_state;//按下去再變化
Serial.println(led_state);
digitalWrite(10, led_state);
}
}
}
last_btn_state = reading;
}
沒有留言:
張貼留言