#include <Arduino.h>
// #include <Wire.h>
#include <Servo.h>
Servo servo1;
#include <Adafruit_Sensor.h>
#include <DHT.h>
#define DHTPIN 7
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
const int redLEDpin = 13;
const int blueLEDpin = 12;
const int yellowLEDpin = 11;
const int PB1pin = 9;
const int PB2pin = 8;
const int encoderCLK = 2;
const int encoderDT = 3;
const int analogSignal = A3;
volatile bool toggleState = false;
volatile bool interruptTriggered = false;
void blinkRed();
void blinkBlue();
void blinkYellow();
float readTHsens();
void INT0_ISR() {
// buttonPressed = true;
int encoderVal = digitalRead(encoderDT);
if (encoderVal == LOW) {
Serial.println("Counter Clockwise");
digitalWrite(redLEDpin, HIGH);
}
if (encoderVal == HIGH) {
Serial.println("Clockwise");
digitalWrite(redLEDpin, LOW);
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(redLEDpin, OUTPUT);
pinMode(blueLEDpin, OUTPUT);
pinMode(yellowLEDpin, OUTPUT);
pinMode(encoderCLK, INPUT);
pinMode(encoderDT, INPUT);
pinMode(PB1pin, INPUT_PULLUP);
pinMode(PB2pin, INPUT_PULLUP);
pinMode(analogSignal, INPUT);
attachInterrupt(digitalPinToInterrupt(encoderCLK), INT0_ISR, FALLING);
servo1.attach(5);
dht.begin();
// Init
lcd.init();
lcd.backlight();
lcd.setCursor(3, 0);
lcd.print("Testing Zone");
lcd.setCursor(3, 1);
lcd.print("Temp:");
lcd.setCursor(3, 2);
lcd.print("Humi:");
}
void loop() {
// static int count = 0;
// if (count < 5) {
// servo1.write(0);
// delay(1500);
// servo1.write(180);
// delay(1500);
// int sensorValue = analogRead(analogSignal);
// Serial.println("Analog Sig: " + String(sensorValue));
// count++;
// }
// if (digitalRead(encoderCLK) == LOW) {
// toggleState = !toggleState;
// Serial.print("CLK: " + String(digitalRead(encoderCLK)));
// Serial.print("DT: " + String(digitalRead(encoderDT)));
// digitalWrite(redLEDpin, toggleState);
// delay(250);
// }
// if (digitalRead(encoderCLK) == LOW) {
// Serial.println("GUMANA LOW");
// }
// if (digitalRead(encoderCLK) == HIGH) {
// Serial.println("HIGH GUMANA");
// }
blinkBlue();
readTHsens();
float temp = readTHsens();
// Serial.println(temp);
if (temp < 32) {
digitalWrite(yellowLEDpin, LOW);
}
else if (temp >= 32 && temp <= 40) {
digitalWrite(yellowLEDpin, HIGH);
}
else if (temp > 40){
blinkYellow();
}
}
float readTHsens() {
static unsigned long THsensmillis = millis();
if (millis() - THsensmillis > 2000){
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
// Serial.println(F("Failed to read from DHT sensor!"));
lcd.setCursor(9, 1);
lcd.print("Failed to read");
lcd.setCursor(9, 2);
lcd.print("error");
return -1.0;
}
// Compute heat index in Fahrenheit (the default)
// float hif = dht.computeHeatIndex(f, h);
// // Compute heat index in Celsius (isFahreheit = false)
// float hic = dht.computeHeatIndex(t, h, false);
lcd.setCursor(9, 1);
lcd.print(String(t));
lcd.setCursor(9, 2);
lcd.print(String(h));
return t;
THsensmillis = millis();
}
}
void blinkRed(){
static unsigned long redLEDmillis = millis();
if (millis() - redLEDmillis > 1500) {
digitalWrite(redLEDpin, !digitalRead(redLEDpin));
redLEDmillis = millis();
}
}
void blinkBlue(){
static unsigned long blueLEDmillis = millis();
if (millis() - blueLEDmillis > 500) {
digitalWrite(blueLEDpin, !digitalRead(blueLEDpin));
blueLEDmillis = millis();
}
}
void blinkYellow(){
static unsigned long yellowLEDmillis = millis();
if (millis() - yellowLEDmillis > 1000) {
digitalWrite(yellowLEDpin, !digitalRead(yellowLEDpin));
yellowLEDmillis = millis();
}
}Rotary
Encoder