PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include <Bounce.h>
  2. /*
  3. This example uses a mercury tilt switch to detect the orientation
  4. of a robot. If you turn the bot over, it falls asleep to conserve
  5. battery life. If you have ever used a tilt switch before,
  6. you know they can be very "noisy" - they jump around a lot!
  7. The bounce library can handle sensing when a noisy switch has
  8. "settled down" with the "duration" method that tells you how long
  9. the pin has been in the current state.
  10. */
  11. const uint8_t ORIENTATION_PIN = 16;
  12. Bounce orientation = Bounce( ORIENTATION_PIN, 50 );
  13. int awake; // Are we awake based on our current orientation?
  14. void setup() {
  15. pinMode( ORIENTATION_PIN, INPUT );
  16. digitalWrite( ORIENTATION_PIN, HIGH ); // Activate Pull-Up
  17. Serial.begin(19200);
  18. Serial.println( "Orientation Test " );
  19. Serial.println();
  20. awake = orientation.read();
  21. Serial.println( awake ? "Awake!" : "Asleep!" );
  22. }
  23. unsigned long lastKnock = 0;
  24. void loop() {
  25. orientation.update();
  26. // has our orientation changed for more the 1/2 a second?
  27. if ( orientation.read() != awake && orientation.duration() > 500 ) {
  28. awake = orientation.read();
  29. if ( awake ) {
  30. Serial.println( "Waking Up!" );
  31. // Do Something Here...
  32. }
  33. else {
  34. Serial.println( "Falling Asleep" );
  35. // Do Something Here...
  36. }
  37. }
  38. }