Display, sensor, button - arduino nano with serial interaction.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

124 lines
4.3 KiB

#include <Arduino.h>
#include <Thread.h>
#include <SevenSegmentExtended.h> // 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-");
}