Display, sensor, button - arduino nano with serial interaction.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

displaysensorbutton.ino 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include <Arduino.h>
  2. #include <Thread.h>
  3. #include <SevenSegmentExtended.h> // DisplayLib // Extended TM1637 library https://github.com/bremme/arduino-tm1637
  4. #include "DHT.h" // DHTlib
  5. #define DHTPIN 5 // DHTsensorSetting // DHT pin
  6. #define DHTTYPE DHT22 // DHTsensorSetting // DHT11 / DHT21 / DHT22
  7. DHT dht(DHTPIN, DHTTYPE); // DHTsensorSetting
  8. const byte PIN_CLK = 2; // DisplaySetting // CLK pin
  9. const byte PIN_DIO = 3; // DisplaySetting // DIO pin
  10. bool displayOn = false; // DisplaySetting // Boolian for store display power status
  11. SevenSegmentExtended display(PIN_CLK, PIN_DIO); // DisplaySetting
  12. const byte buttonPin = 4; // ButtonSetting // Button pin (a 10K resistor is necessary)
  13. long buttonPressed = 0; // ButtonSetting // Button ms pressed counter
  14. long previousButtonMillis = 0; // ButtonSetting // Button previous press counter
  15. byte buttonView = 0; // ButtonSetting // Button viewing state
  16. int buttonInterval = 10000; // ButtonSetting // Interval for highlighting 4-Digit Display after pressing button
  17. int buttonLongPress = 2500; // ButtonSetting // Interval for long press button action
  18. int buttonCheck = 200; // ButtonSetting // Interval for checking button state
  19. // Threads:
  20. Thread pressButtonThread = Thread(); // Create thread for button state checking
  21. void setup() {
  22. Serial.begin(9600); // Initializes the Serial connection @ 9600 baud for debug
  23. dht.begin(); // DHTsensorSetting // Initializes the sensor
  24. display.begin(); // DisplaySetting // Initializes the display
  25. displayOn = true; // Display On
  26. display.setBacklight(100); // Display On - set the brightness to 100 %
  27. display.print("INIT-TINI"); // Display initialization
  28. pinMode(buttonPin, INPUT); // ButtonSetting
  29. // Button state cheking thread:
  30. pressButtonThread.onRun(pressButton);
  31. pressButtonThread.setInterval(buttonCheck);
  32. display.off(); // Display Off
  33. displayOn = false; // Display Off
  34. }
  35. void loop() {
  36. // Threads init:
  37. if (pressButtonThread.shouldRun())
  38. pressButtonThread.run();
  39. // Serial interaction:
  40. if (Serial.available() > 0) {
  41. int incomingByte = Serial.read() - 48;
  42. if (incomingByte == 5) {
  43. float humFloat;
  44. readDHThum(&humFloat);
  45. Serial.println(humFloat);
  46. }
  47. if (incomingByte == 6) {
  48. float tempFloat;
  49. readDHTtemp(&tempFloat);
  50. Serial.println(tempFloat);
  51. }
  52. }
  53. }
  54. // Check button pressing thread
  55. void pressButton() {
  56. // Display off after buttonInterval ms slack
  57. unsigned long currentMillis = millis();
  58. if (currentMillis - previousButtonMillis > buttonInterval) {
  59. previousButtonMillis = currentMillis;
  60. if (displayOn == true) {
  61. display.off();
  62. displayOn = false;
  63. }
  64. }
  65. if (digitalRead(buttonPin) == HIGH) {
  66. buttonPressed = buttonPressed + 200;
  67. if (buttonPressed > buttonLongPress) {
  68. buttonPressed = 0;
  69. longPress();
  70. }
  71. previousButtonMillis = currentMillis;
  72. display.on();
  73. displayOn = true;
  74. if (buttonView == 0) {
  75. float tempFloat;
  76. readDHTtemp(&tempFloat);
  77. int tempInt = (int)tempFloat;
  78. if (tempInt < 0)
  79. tempInt = tempInt * (-1);
  80. byte rawData[4];
  81. rawData[0] = display.encode(tempInt / 10);
  82. rawData[1] = display.encode(tempInt % 10);
  83. rawData[2] = B01100011; // degree
  84. rawData[3] = display.encode('c');
  85. display.printRaw(rawData);
  86. } else if (buttonView == 1) {
  87. float humFloat;
  88. readDHThum(&humFloat);
  89. int humInt = (int)humFloat;
  90. byte rawData[4];
  91. rawData[0] = display.encode('h');
  92. rawData[1] = display.encode('u');
  93. rawData[2] = display.encode(humInt / 10);
  94. rawData[3] = display.encode(humInt % 10);
  95. display.printRaw(rawData);
  96. }
  97. if (buttonView < 1) buttonView += 1;
  98. else buttonView = 0;
  99. } else {
  100. buttonPressed = 0;
  101. }
  102. }
  103. // DHT Temperature
  104. void readDHTtemp(float *a) {
  105. float t = dht.readTemperature();
  106. *a = t;
  107. }
  108. // DHT Humidity
  109. void readDHThum(float *a) {
  110. float h = dht.readHumidity();
  111. *a = h;
  112. }
  113. void longPress() {
  114. display.print("-doctor-equivalent-me-");
  115. }