/*
MIT License
Copyright (c) 2024 do2xg
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
**The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.**
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <DHT.h>
#include <DS1307RTC.h>
#include <Adafruit_NeoPixel.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo servoN;
Servo servoE;
Servo servoS;
Servo servoW;
#define DHTPIN 6
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
const int photoresistorPin = A0;
int photoresistorValue;
int currentState = 0;
bool lightsOn = false;
int servoNAngleClosed = 0;
int servoNAngleOpen = 180;
int servoEAngleClosed = 0;
int servoEAngleOpen = 180;
int servoSAngleClosed = 0;
int servoSAngleOpen = 180;
int servoWAngleClosed = 0;
int servoWAngleOpen = 180;
float minHumidity = 60.0;
float maxHumidity = 85.0;
#define NEO_PIXEL_PIN_1 12
#define NEO_PIXEL_PIN_2 11
#define NEO_PIXEL_COUNT 16
Adafruit_NeoPixel strip1 = Adafruit_NeoPixel(NEO_PIXEL_COUNT, NEO_PIXEL_PIN_1, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip2 = Adafruit_NeoPixel(NEO_PIXEL_COUNT, NEO_PIXEL_PIN_2, NEO_GRB + NEO_KHZ800);
void setup() {
lcd.begin(16, 2);
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Hello, Arduino!");
servoN.attach(2);
servoE.attach(3);
servoS.attach(4);
servoW.attach(5);
dht.begin();
setSyncProvider(RTC.get);
strip1.begin();
strip2.begin();
strip1.show();
strip2.show();
}
void loop() {
time_t currentTime = now();
// Check if it's between 08:00 and 00:00
if (hour(currentTime) == 8 && !lightsOn) {
// Turn on lights at 08:00
lightsOn = true;
} else if (hour(currentTime) == 0 && lightsOn) {
// Turn off lights at midnight
lightsOn = false;
}
switch (currentState) {
case 0: // Vents status
displayVentsStatus();
break;
case 1: // Temperature in Fahrenheit
displayTemperature();
break;
case 2: // Humidity
displayHumidity();
break;
case 3: // Light
displayLight(currentTime);
break;
case 4: // Time
displayTime(currentTime);
break;
}
currentState = (currentState + 1) % 5;
delay(3000);
}
void displayVentsStatus() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("N: ");
lcd.print(servoN.read() == servoNAngleClosed ? "C" :
(servoN.read() == servoNAngleOpen ? "O" : "%"));
if (servoN.read() != servoNAngleClosed && servoN.read() != servoNAngleOpen) {
lcd.print(servoN.read());
}
lcd.setCursor(6, 0);
lcd.print("E: ");
lcd.print(servoE.read() == servoEAngleClosed ? "C" :
(servoE.read() == servoEAngleOpen ? "O" : "%"));
if (servoE.read() != servoEAngleClosed && servoE.read() != servoEAngleOpen) {
lcd.print(servoE.read());
}
lcd.setCursor(0, 1);
lcd.print("S: ");
lcd.print(servoS.read() == servoSAngleClosed ? "C" :
(servoS.read() == servoSAngleOpen ? "O" : "%"));
if (servoS.read() != servoSAngleClosed && servoS.read() != servoSAngleOpen) {
lcd.print(servoS.read());
}
lcd.setCursor(6, 1);
lcd.print("W: ");
lcd.print(servoW.read() == servoWAngleClosed ? "C" :
(servoW.read() == servoWAngleOpen ? "O" : "%"));
if (servoW.read() != servoWAngleClosed && servoW.read() != servoWAngleOpen) {
lcd.print(servoW.read());
}
}
void displayTemperature() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(dht.readTemperature(true));
lcd.print(" F");
}
void displayHumidity() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Humidity: ");
lcd.print(dht.readHumidity());
lcd.print("%");
}
void displayLight(time_t currentTime) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Light: ");
lcd.print(analogRead(photoresistorPin));
// Control NeoPixel rings based on light levels
int lightLevel = analogRead(photoresistorPin);
updateNeoPixelRings(lightLevel);
// Display the state of the lights
lcd.setCursor(0, 1);
lcd.print("Lights: ");
lcd.print(lightsOn ? "On" : "Off");
}
void displayTime(time_t currentTime) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time: ");
printDigits(hour(currentTime));
lcd.print(":");
printDigits(minute(currentTime));
lcd.print(":");
printDigits(second(currentTime));
}
void printDigits(int digits) {
// Helper function to print leading zeros
if (digits < 10) {
lcd.print("0");
}
lcd.print(digits);
}
void updateNeoPixelRings(int lightLevel) {
// Update NeoPixel rings based on light levels
if (lightsOn) {
colorWipe(strip1.Color(0, 0, 255), 50);
colorWipe(strip2.Color(0, 0, 255), 50);
} else {
colorWipe(strip1.Color(0, 0, 0), 50);
colorWipe(strip2.Color(0, 0, 0), 50);
}
}
void colorWipe(uint32_t color, int wait) {
// Helper function for NeoPixel color wipe
for(int i = 0; i < strip1.numPixels(); i++) {
strip1.setPixelColor(i, color);
strip2.setPixelColor(i, color);
strip1.show();
strip2.show();
delay(wait);
}
}