#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include "RTClib.h"
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo myservo;
RTC_DS1307 rtc;
int rPin = 13;
int gPin = 12;
int bPin = 11;
int buttonPin = 7;
int buttonLedPin = 2;
int fadePin = 9;
int servoPin = 6;
int trig = 4;
int echo = 3;
unsigned long curTime = 0;
unsigned long rPre = 0;
unsigned long gPre = 0;
unsigned long bPre = 0;
unsigned long buttonPre = 0;
unsigned long fPre = 0;
unsigned long sPre = 0;
unsigned long lcdPre = 0;
int rTime = 200;
int gTime = 500;
int bTime = 400;
int buttonTime = 50;
int fTime = 10;
int sTime = 15;
int lcdTime = 1000;
int rState = LOW;
int gState = LOW;
int bState = LOW;
int buttonledState = LOW;
int buttonState;
int bright = 0;
int pos = 0;
int posIncre = 1 ;
int cen;
char daysOfTheWeek[7][12] = {"Sun", "Mon", "Tue",
"Wed", "Thu", "Fri", "Sat"
};
void setup() {
Serial.begin(115200);
rtc.begin();
lcd.init();
lcd.backlight();
// Serial.begin(115200);
pinMode(rPin, OUTPUT);
pinMode(gPin, OUTPUT);
pinMode(bPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buttonLedPin, OUTPUT);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
myservo.attach(servoPin);
pinMode(bPin, OUTPUT);
}
void loop() {
curTime = millis();
button();
// servo();
}// end loop
void blink() {
///////////////////////////////////// red
if (curTime - rPre >= rTime) {
rPre = curTime;
if (rState == LOW) {
rState = HIGH;
rTime = 2000;
} else {
rState = LOW;
rTime = 500;
}
digitalWrite(rPin, rState);
}
////////////////////////////// end red
///////////////////////////////////// green
if (curTime - gPre >= gTime) {
gPre = curTime;
if (gState == LOW) {
gState = HIGH;
gTime = 200;
} else {
gState = LOW;
gTime = 1000;
}
digitalWrite(gPin, gState);
}
////////////////////////////// end green
///////////////////////////////////// blue
if (curTime - bPre >= bTime) {
bPre = curTime;
if (bState == LOW) {
bState = HIGH;
} else {
bState = LOW;
}
digitalWrite(bPin, bState);
}
////////////////////////////// end blue
}
void button() {
if (curTime - buttonPre >= buttonTime) {
buttonPre = curTime;
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
buttonledState = !buttonledState;
// } else {
// buttonledState = LOW;
}
digitalWrite(buttonLedPin, buttonledState);
if (buttonledState == HIGH) {
time();
} else {
bin();
}
}
}
void fade() {
if (curTime - fPre >= fTime) {
fPre = curTime;
bright++;
analogWrite(fadePin, bright);
if (bright >= 255) {
bright = 0;
}
}
}
void servo() {
if (curTime - sPre >= sTime) {
sPre = curTime;
pos += posIncre;
myservo.write(pos);
if (pos >= 180 || pos <= 0) {
posIncre = -posIncre;
}
}
}
void bin() {
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
// Read the result:
int duration = pulseIn(echo, HIGH);
cen = duration / 58;
lcd.setCursor(0, 0);
lcd.print("Dist in CM: ");
lcd.print(cen);
lcd.print(" ");
if (cen >= 100) {
myservo.write(90);
}
if (cen <= 100) {
myservo.write(0);
}
lcd.setCursor(0, 1);
lcd.print("Dist inch: ");
lcd.print(duration / 148);
lcd.print(" ");
}
void time() {
DateTime now = rtc.now();
lcd.setCursor(0, 0);
lcd.print("Date:");
lcd.print(now.day());
lcd.print('/');
lcd.print(now.month());
lcd.print('/');
lcd.print(now.year());
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
lcd.setCursor(0, 1);
lcd.print("Time : ");
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute());
lcd.print(':');
lcd.print(now.second(), DEC);
lcd.println("");
}