arduino-sketch.ino 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include <Adafruit_SSD1306.h>
  2. #include "version.h"
  3. const uint8_t DISPLAY_WIDTH = 128;
  4. const uint8_t DISPLAY_HEIGHT = 64;
  5. // https://startingelectronics.org/tutorials/arduino/modules/OLED-128x64-I2C-display/
  6. const uint8_t DISPLAY_I2C_ADDRESS = 0x3C;
  7. const double known_resistor_ohm = 10e3;
  8. Adafruit_SSD1306 display(DISPLAY_WIDTH, DISPLAY_HEIGHT);
  9. void setup() {
  10. // $ stty -F /dev/ttyUSB0 | grep speed
  11. Serial.begin(115200);
  12. Serial.println(PROJECT_REPO_URL);
  13. Serial.println(PROJECT_VERSION);
  14. if (!display.begin(SSD1306_SWITCHCAPVCC, DISPLAY_I2C_ADDRESS)) {
  15. Serial.println("failed to init display");
  16. while (true)
  17. ;
  18. }
  19. display.clearDisplay();
  20. display.setTextColor(SSD1306_WHITE);
  21. display.println(PROJECT_REPO_URL);
  22. display.println(PROJECT_VERSION);
  23. display.display();
  24. delay(2000);
  25. display.setTextSize(3);
  26. }
  27. void loop() {
  28. display.clearDisplay();
  29. display.setCursor(0, 0);
  30. // https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/
  31. int voltage = analogRead(A0);
  32. if (voltage == 0) {
  33. display.println("inf");
  34. } else {
  35. double unknown_resistor_ohm =
  36. known_resistor_ohm * ((double)1024 / voltage - 1);
  37. display.println(unknown_resistor_ohm / 1000);
  38. }
  39. display.print("kOhm");
  40. display.display();
  41. delay(100); // ms
  42. }