AnalogreadSerial.ino 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. Read an analog sensor attached on the grove connector of the M5 Stick
  3. uses the following library make sure to add it to your libraries before compiling
  4. Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
  5. Copyright (c) 2016, olikraus@gmail.com
  6. All rights reserved.
  7. Redistribution and use in source and binary forms, with or without modification,
  8. are permitted provided that the following conditions are met:
  9. * Redistributions of source code must retain the above copyright notice, this list
  10. of conditions and the following disclaimer.
  11. * Redistributions in binary form must reproduce the above copyright notice, this
  12. list of conditions and the following disclaimer in the documentation and/or other
  13. materials provided with the distribution.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  15. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  16. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  17. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  24. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  26. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include <Arduino.h>
  29. #include <U8x8lib.h>
  30. #define SensorPin 13
  31. // U8x8 constructor for your display
  32. U8X8_SH1107_64X128_4W_HW_SPI u8x8(14, /* dc=*/ 27, /* reset=*/ 33);
  33. // Create a U8x8log object
  34. U8X8LOG u8x8log;
  35. // Define the dimension of the U8x8log window
  36. #define U8LOG_WIDTH 16
  37. #define U8LOG_HEIGHT 8
  38. // Allocate static memory for the U8x8log window
  39. uint8_t u8log_buffer[U8LOG_WIDTH*U8LOG_HEIGHT];
  40. void setup() {
  41. // Startup U8x8
  42. u8x8.begin();
  43. // Set a suitable font. This font will be used for U8x8log
  44. u8x8.setFont(u8x8_font_chroma48medium8_r);
  45. // Start U8x8log, connect to U8x8, set the dimension and assign the static memory
  46. u8x8log.begin(u8x8, U8LOG_WIDTH, U8LOG_HEIGHT, u8log_buffer);
  47. // Set the U8x8log redraw mode
  48. u8x8log.setRedrawMode(1); // 0: Update screen with newline, 1: Update screen for every char
  49. Serial.begin(115200);
  50. //Connect the pin set it to output
  51. pinMode(SensorPin, OUTPUT);
  52. ledcSetup(1, 38000, 10);
  53. ledcAttachPin(SensorPin, 1);
  54. }
  55. void loop()
  56. {
  57. analogReadResolution(10);
  58. Serial.println(int(analogRead(SensorPin)));
  59. u8x8log.print(int(analogRead(SensorPin)));
  60. u8x8log.print("\n");
  61. ledcWrite(1, ledcRead(1) ? 0 : 512);
  62. delay(100);
  63. }