PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

simple_datalogger.ino 3.5KB

3 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**********************************************************
  2. Simple Data Logger Example using Deep Sleep mode.
  3. Hold the button pin(3) to log data.
  4. Supported Micros: T-LC/3.x/4.0
  5. **********************************************************/
  6. #include <Snooze.h>
  7. #include <Bounce.h>
  8. // Load drivers
  9. // Always use SnoozeUSBSerial with T4.0.
  10. // SnoozeUSBSerial driver does what Serial does like "print",
  11. // "println", "prinf", etc and also handles the effects of using
  12. // the Arduino Serial monitor and sleeping. Use it when you
  13. // want to print to serial monitor for sleeping applications.
  14. SnoozeUSBSerial usb;
  15. // button wakeup driver
  16. SnoozeDigital digital;
  17. #if defined(__MKL26Z64__)
  18. // configures the lc's 5v data buffer (OUTPUT, LOW) for low power
  19. Snoozelc5vBuffer lc5vBuffer;
  20. #endif
  21. const uint8_t BUTTON = 7;
  22. // use bounce for pin 4, debounce of 5ms
  23. Bounce button = Bounce(BUTTON, 5);
  24. // buffer to hold data for printing, this is because the -
  25. // Serial Monitor takes a few seconds to come back online -
  26. // after waking up from sleep mode
  27. static char print_buffer[2000] = {0};
  28. // install driver into SnoozeBlock
  29. #if defined(__IMXRT1062__) | defined(__MK66FX1M0__) || defined(__MK64FX512__) || defined(__MK20DX256__)
  30. SnoozeBlock config_teensy(usb, digital);
  31. #elif defined(__MKL26Z64__)
  32. SnoozeBlock config_teensy(usb, digital, lc5vBuffer);
  33. #endif
  34. // ------------------------------------------------------------------
  35. void setup() {
  36. // Configure pin 3 for bounce library
  37. pinMode(BUTTON, INPUT_PULLUP);
  38. // Configure Snooze pinMode(pin, mode, irq type)
  39. digital.pinMode(BUTTON, INPUT_PULLUP, FALLING);
  40. // debug led
  41. pinMode(LED_BUILTIN, OUTPUT);
  42. digitalWrite(LED_BUILTIN, HIGH);
  43. while (!usb);
  44. delay(100);
  45. digitalWrite(LED_BUILTIN, LOW);
  46. usb.println("Starting Data Logger /w Deep Sleeping Example...");
  47. usb.println("-------------------------------------------");
  48. delay(20);
  49. }
  50. // ------------------------------------------------------------------
  51. void loop() {
  52. SLEEP:
  53. digitalWrite(LED_BUILTIN, LOW);
  54. // you need to update before sleeping.
  55. button.update();
  56. usb.println("Going to deepSleep now.\n");
  57. delay(5);
  58. Snooze.deepSleep( config_teensy );
  59. digitalWrite(LED_BUILTIN, HIGH);
  60. // check for 300ms that the button is held
  61. bool hold = threeHundredMilliSecondHoldCheck();
  62. usb.println("Data Logging.");
  63. // if not held for 300ms go back to sleep
  64. if (hold == false) goto SLEEP;
  65. // Data logging routine
  66. datalog();
  67. }
  68. // ------------------------------------------------------------------
  69. // Make sure the button is help low for aleast 300ms before data logging.
  70. bool threeHundredMilliSecondHoldCheck() {
  71. // Pin is configured as PULLUP so 0 means the button has been
  72. // pushed.
  73. button.update();
  74. while (button.read() == 0) {
  75. // Monitor the pin hold duration.
  76. if (button.duration() > 300) return true;
  77. // Update bounce library.
  78. button.update();
  79. }
  80. return false;
  81. }
  82. // ------------------------------------------------------------------
  83. void datalog() {
  84. elapsedMillis samplerate = 0;
  85. unsigned int idx = 0;
  86. button.update();
  87. while (button.read() == 0) {
  88. if (samplerate >= 20) {
  89. unsigned int x = analogRead(A0);
  90. unsigned int y = analogRead(A1);
  91. unsigned int z = analogRead(A2);
  92. // print data to serial monitor
  93. usb.printf("index: %3u | x: %3u | y: %3u | z: %3u\n", idx++, x, y, z);
  94. digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  95. samplerate = 0;
  96. }
  97. // Update bounce library.
  98. button.update();
  99. }
  100. digitalWrite(LED_BUILTIN, LOW);
  101. }