boolean led_state;
boolean btn_state;
int last_btn_state;
unsigned long last_debounce_time;
void setup()
{
Serial.begin(9600);
pinMode(2, INPUT);
pinMode(13, OUTPUT);
}
void loop()
{
int reading = digitalRead(2);
if (reading != last_btn_state)
{
last_debounce_time = millis();
}
if ((millis() - last_debounce_time) > 50)
{
if (btn_state != reading)
{
btn_state = reading;
Serial.println(btn_state);
if (btn_state == LOW)
{
led_state = !led_state;
digitalWrite(13, led_state);
}
}
}
last_btn_state=reading;
}
沒有留言:
張貼留言