Browse Source

init

master
ivan 4 months ago
commit
dc36f1906c
7 changed files with 162 additions and 0 deletions
  1. 27
    0
      LICENSE
  2. 3
    0
      README.md
  3. BIN
      displaysensorbutton.fzz
  4. 124
    0
      displaysensorbutton.ino
  5. BIN
      displaysensorbutton_bb.jpg
  6. 4
    0
      gethum.sh
  7. 4
    0
      gettemp.sh

+ 27
- 0
LICENSE View File

@@ -0,0 +1,27 @@
1
+    This source code is released under the DWTW license.
2
+
3
+    This program is free software; you can redistribute it and/or modify it under the terms of the Do What Thou Wilt License.
4
+
5
+    Boundless Public License
6
+    DO WHAT THOU WILT
7
+    TO PUBLIC LICENSE
8
+
9
+    Version 2.55
10
+
11
+    Everyone is permitted to copy and distribute verbatim or modified copies of this license document, and changing it in full or in part is allowed without any restrictions.
12
+
13
+    TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
14
+
15
+    0. Do what thou wilt shall be the whole of the Law.
16
+
17
+    DWTWL – a license with a single requirement: DO WHAT THOU WILT
18
+
19
+    The license provides more freedom than any other one (such as GPL or BSD) and does not require saving the license text on copying.
20
+
21
+    DWTWL – an accomplished and eligible license for free text, code and any other symbols (including the software, documentation and artwork).
22
+
23
+    The license does not contain a "no warranty" clause. DWTWL can be used in countries that do not legally acknowledge the transition to public domain.
24
+
25
+    Summary:
26
+
27
+    An author-creator gives their source code to the world for free, without becoming distracted by worldly thinking regarding how and why the others will use it.

+ 3
- 0
README.md View File

@@ -0,0 +1,3 @@
1
+# display sensor button
2
+
3
+![displaysensorbutton](https://dev.ussr.win/microcontrollers/displaysensorbutton/raw/branch/master/displaysensorbutton_bb.png)

BIN
displaysensorbutton.fzz View File


+ 124
- 0
displaysensorbutton.ino View File

@@ -0,0 +1,124 @@
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
+
20
+// Threads:
21
+Thread pressButtonThread = Thread();   // Create thread for button state checking
22
+
23
+void setup() {
24
+  Serial.begin(9600);                  // Initializes the Serial connection @ 9600 baud for debug
25
+  dht.begin();                         // DHTsensorSetting // Initializes the sensor
26
+  display.begin();                     // DisplaySetting // Initializes the display
27
+  displayOn = true;                    // Display On
28
+  display.setBacklight(100);           // Display On - set the brightness to 100 %
29
+  display.print("INIT-TINI");          // Display initialization
30
+  pinMode(buttonPin, INPUT);           // ButtonSetting
31
+  // Button state cheking thread:
32
+  pressButtonThread.onRun(pressButton);
33
+  pressButtonThread.setInterval(buttonCheck);
34
+  display.off();                       // Display Off
35
+  displayOn = false;                   // Display Off
36
+}
37
+
38
+void loop() {
39
+  // Threads init:
40
+  if (pressButtonThread.shouldRun())
41
+    pressButtonThread.run();
42
+  // Serial interaction:
43
+  if (Serial.available() > 0) {
44
+    int incomingByte = Serial.read() - 48;
45
+    if (incomingByte == 5) {
46
+      float humFloat;
47
+      readDHThum(&humFloat);
48
+      Serial.println(humFloat);
49
+    }
50
+    if (incomingByte == 6) {
51
+      float tempFloat;
52
+      readDHTtemp(&tempFloat);
53
+      Serial.println(tempFloat);
54
+    }
55
+  }
56
+}
57
+
58
+// Check button pressing thread
59
+void pressButton() {
60
+  // Display off after buttonInterval ms slack
61
+  unsigned long currentMillis = millis();
62
+  if (currentMillis - previousButtonMillis > buttonInterval) {
63
+    previousButtonMillis = currentMillis;
64
+    if (displayOn == true) {
65
+      display.off();
66
+      displayOn = false;
67
+    }
68
+  }
69
+
70
+  if (digitalRead(buttonPin) == HIGH) {
71
+    buttonPressed = buttonPressed + 200;
72
+    if (buttonPressed > buttonLongPress) {
73
+      buttonPressed = 0;
74
+      longPress();
75
+    }
76
+    previousButtonMillis = currentMillis;
77
+    display.on();
78
+    displayOn = true;
79
+    if (buttonView == 0) {
80
+      float tempFloat;
81
+      readDHTtemp(&tempFloat);
82
+      int tempInt = (int)tempFloat;
83
+      if (tempInt < 0)
84
+        tempInt = tempInt * (-1);
85
+      byte rawData[4];
86
+      rawData[0] = display.encode(tempInt / 10);
87
+      rawData[1] = display.encode(tempInt % 10);
88
+      rawData[2] = B01100011;          // degree
89
+      rawData[3] = display.encode('c');
90
+      display.printRaw(rawData);
91
+    } else if (buttonView == 1) {
92
+      float humFloat;
93
+      readDHThum(&humFloat);
94
+      int humInt = (int)humFloat;
95
+      byte  rawData[4];
96
+      rawData[0] = display.encode('h');
97
+      rawData[1] = display.encode('u');
98
+      rawData[2] = display.encode(humInt / 10);
99
+      rawData[3] = display.encode(humInt % 10);
100
+      display.printRaw(rawData);
101
+    }
102
+    if (buttonView < 1) buttonView += 1;
103
+    else buttonView = 0;
104
+  } else {
105
+    buttonPressed = 0;
106
+  }
107
+}
108
+
109
+
110
+// DHT Temperature
111
+void readDHTtemp(float *a) {
112
+  float t = dht.readTemperature();
113
+  *a = t;
114
+}
115
+
116
+// DHT Humidity
117
+void readDHThum(float *a) {
118
+  float h = dht.readHumidity();
119
+  *a = h;
120
+}
121
+
122
+void longPress() {
123
+  display.print("-doctor-equivalent-me-");
124
+}

BIN
displaysensorbutton_bb.jpg View File


+ 4
- 0
gethum.sh View File

@@ -0,0 +1,4 @@
1
+#!/bin/sh
2
+echo "5" > /dev/ttyUSB0
3
+HUM=`dd if=/dev/ttyUSB0 count=1`
4
+echo $HUM

+ 4
- 0
gettemp.sh View File

@@ -0,0 +1,4 @@
1
+#!/bin/sh
2
+echo "6" > /dev/ttyUSB0
3
+TEMP=`dd if=/dev/ttyUSB0 count=1`
4
+echo $TEMP

Loading…
Cancel
Save