2022年2月11日 星期五

debounce +sw

 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;

}

沒有留言:

張貼留言

algorithm

 #include <iostream> #include <string.h> using namespace std; int main(int argc, char** argv)  { for(int j=2;j<=100;j++)//j...