commit dc36f1906c15d40bed28d8302da7b0cb8a5713d4 Author: zlaxy Date: Sat Apr 20 23:44:37 2019 +0300 init diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..3e6713a --- /dev/null +++ b/LICENSE @@ -0,0 +1,27 @@ + This source code is released under the DWTW license. + + This program is free software; you can redistribute it and/or modify it under the terms of the Do What Thou Wilt License. + + Boundless Public License + DO WHAT THOU WILT + TO PUBLIC LICENSE + + Version 2.55 + + 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. + + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. Do what thou wilt shall be the whole of the Law. + + DWTWL – a license with a single requirement: DO WHAT THOU WILT + + The license provides more freedom than any other one (such as GPL or BSD) and does not require saving the license text on copying. + + DWTWL – an accomplished and eligible license for free text, code and any other symbols (including the software, documentation and artwork). + + 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. + + Summary: + + 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. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..04f7b76 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# display sensor button + +![displaysensorbutton](https://dev.ussr.win/microcontrollers/displaysensorbutton/raw/branch/master/displaysensorbutton_bb.png) \ No newline at end of file diff --git a/displaysensorbutton.fzz b/displaysensorbutton.fzz new file mode 100644 index 0000000..7a01f6c Binary files /dev/null and b/displaysensorbutton.fzz differ diff --git a/displaysensorbutton.ino b/displaysensorbutton.ino new file mode 100644 index 0000000..0422821 --- /dev/null +++ b/displaysensorbutton.ino @@ -0,0 +1,124 @@ +#include +#include +#include // DisplayLib // Extended TM1637 library https://github.com/bremme/arduino-tm1637 +#include "DHT.h" // DHTlib +#define DHTPIN 5 // DHTsensorSetting // DHT pin +#define DHTTYPE DHT22 // DHTsensorSetting // DHT11 / DHT21 / DHT22 +DHT dht(DHTPIN, DHTTYPE); // DHTsensorSetting +const byte PIN_CLK = 2; // DisplaySetting // CLK pin +const byte PIN_DIO = 3; // DisplaySetting // DIO pin +bool displayOn = false; // DisplaySetting // Boolian for store display power status +SevenSegmentExtended display(PIN_CLK, PIN_DIO); // DisplaySetting +const byte buttonPin = 4; // ButtonSetting // Button pin (a 10K resistor is necessary) +long buttonPressed = 0; // ButtonSetting // Button ms pressed counter +long previousButtonMillis = 0; // ButtonSetting // Button previous press counter +byte buttonView = 0; // ButtonSetting // Button viewing state +int buttonInterval = 10000; // ButtonSetting // Interval for highlighting 4-Digit Display after pressing button +int buttonLongPress = 2500; // ButtonSetting // Interval for long press button action +int buttonCheck = 200; // ButtonSetting // Interval for checking button state + +// Threads: +Thread pressButtonThread = Thread(); // Create thread for button state checking + +void setup() { + Serial.begin(9600); // Initializes the Serial connection @ 9600 baud for debug + dht.begin(); // DHTsensorSetting // Initializes the sensor + display.begin(); // DisplaySetting // Initializes the display + displayOn = true; // Display On + display.setBacklight(100); // Display On - set the brightness to 100 % + display.print("INIT-TINI"); // Display initialization + pinMode(buttonPin, INPUT); // ButtonSetting + // Button state cheking thread: + pressButtonThread.onRun(pressButton); + pressButtonThread.setInterval(buttonCheck); + display.off(); // Display Off + displayOn = false; // Display Off +} + +void loop() { + // Threads init: + if (pressButtonThread.shouldRun()) + pressButtonThread.run(); + // Serial interaction: + if (Serial.available() > 0) { + int incomingByte = Serial.read() - 48; + if (incomingByte == 5) { + float humFloat; + readDHThum(&humFloat); + Serial.println(humFloat); + } + if (incomingByte == 6) { + float tempFloat; + readDHTtemp(&tempFloat); + Serial.println(tempFloat); + } + } +} + +// Check button pressing thread +void pressButton() { + // Display off after buttonInterval ms slack + unsigned long currentMillis = millis(); + if (currentMillis - previousButtonMillis > buttonInterval) { + previousButtonMillis = currentMillis; + if (displayOn == true) { + display.off(); + displayOn = false; + } + } + + if (digitalRead(buttonPin) == HIGH) { + buttonPressed = buttonPressed + 200; + if (buttonPressed > buttonLongPress) { + buttonPressed = 0; + longPress(); + } + previousButtonMillis = currentMillis; + display.on(); + displayOn = true; + if (buttonView == 0) { + float tempFloat; + readDHTtemp(&tempFloat); + int tempInt = (int)tempFloat; + if (tempInt < 0) + tempInt = tempInt * (-1); + byte rawData[4]; + rawData[0] = display.encode(tempInt / 10); + rawData[1] = display.encode(tempInt % 10); + rawData[2] = B01100011; // degree + rawData[3] = display.encode('c'); + display.printRaw(rawData); + } else if (buttonView == 1) { + float humFloat; + readDHThum(&humFloat); + int humInt = (int)humFloat; + byte rawData[4]; + rawData[0] = display.encode('h'); + rawData[1] = display.encode('u'); + rawData[2] = display.encode(humInt / 10); + rawData[3] = display.encode(humInt % 10); + display.printRaw(rawData); + } + if (buttonView < 1) buttonView += 1; + else buttonView = 0; + } else { + buttonPressed = 0; + } +} + + +// DHT Temperature +void readDHTtemp(float *a) { + float t = dht.readTemperature(); + *a = t; +} + +// DHT Humidity +void readDHThum(float *a) { + float h = dht.readHumidity(); + *a = h; +} + +void longPress() { + display.print("-doctor-equivalent-me-"); +} diff --git a/displaysensorbutton_bb.jpg b/displaysensorbutton_bb.jpg new file mode 100644 index 0000000..41d1256 Binary files /dev/null and b/displaysensorbutton_bb.jpg differ diff --git a/gethum.sh b/gethum.sh new file mode 100755 index 0000000..97ff62e --- /dev/null +++ b/gethum.sh @@ -0,0 +1,4 @@ +#!/bin/sh +echo "5" > /dev/ttyUSB0 +HUM=`dd if=/dev/ttyUSB0 count=1` +echo $HUM \ No newline at end of file diff --git a/gettemp.sh b/gettemp.sh new file mode 100755 index 0000000..6be5d97 --- /dev/null +++ b/gettemp.sh @@ -0,0 +1,4 @@ +#!/bin/sh +echo "6" > /dev/ttyUSB0 +TEMP=`dd if=/dev/ttyUSB0 count=1` +echo $TEMP \ No newline at end of file