_02_btnAndLed02.ino 1.4 KB

1234567891011121314151617181920212223242526272829303132333435
  1. /***********************************************************
  2. File name: 02_btnAndLed02.ino
  3. Description: Using interrupt mode, every time you press the
  4. button, LED status is switched(ON->OFF,OFF->ON).
  5. Website: www.adeept.com
  6. E-mail: support@adeept.com
  7. Author: Tom
  8. Date: 2015/05/02
  9. ***********************************************************/
  10. int ledpin=11; //definition digital 11 pins as pin to control the LED
  11. int btnpin=2; //Set the digital 2 to button interface
  12. volatile int state = LOW; //Defined output status LED Interface
  13. void setup()
  14. {
  15. pinMode(ledpin, OUTPUT); //Set digital 11 port mode, the OUTPUT for the output
  16. attachInterrupt(0, stateChange, FALLING); //Monitoring Interrupt 0 (Digital PIN 2) changes in the input pins FALLING
  17. }
  18. void loop()
  19. {
  20. digitalWrite(ledpin, state); //Output control status LED, ON or OFF
  21. }
  22. void stateChange() //Interrupt function
  23. {
  24. if(digitalRead(btnpin)==LOW) //Detection button interface to low
  25. {
  26. delayMicroseconds(10000); //Delay 10ms for the elimination of key leading-edge jitter
  27. if(digitalRead(btnpin)==LOW) //Confirm button is pressed
  28. {
  29. state = !state; //Negate operation, each time you run the program here, state the HGIH becomes LOW, or the state becomes the LOW HGIH.
  30. }
  31. }
  32. }