#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"
LiquidCrystal_I2C lcd(0x27,20,4);
int rPin = 13;
int gPin = 12;
int bPin = 11;
int bottonPin = 7;
int bottonLedPin = 2;
int fadePin = 8;
int servoPin = 6;
////////// distance sensor
int trigPin = 4;
int echoPin = 3;
float cm;
float inches;
int R_ledState = LOW;
int G_ledState = LOW;
int B_ledState = LOW;
int Botton_ledState = LOW;
int Botton_State;
int bright = 0;
int pos = 0;
int posIncr = 1;
unsigned long currentMillis = 0;
unsigned long R_previousMillis = 0;
unsigned long G_previousMillis = 0;
unsigned long B_previousMillis = 0;
unsigned long Botton_previousMillis = 0;
unsigned long fade_previousMillis = 0;
unsigned long servo_previousMillis = 0;
unsigned long sensor_previousMillis = 0;
unsigned long rtc_previousMillis = 0;
int R_interval = 1000;
int G_interval = 500;
int B_interval = 100;
int Botton_interval = 10;
int fade_interval = 100;
int servo_interval = 50; ////////////////servo
int sensor_interval = 2000; /////// sensor
int rtc_interval = 1000;
Servo myservo;
//////////////////////
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup() {
Serial.begin(115200);
pinMode(rPin, OUTPUT);
pinMode(gPin, OUTPUT);
pinMode(bPin, OUTPUT);
pinMode(bottonPin, INPUT_PULLUP);
pinMode(bottonLedPin, OUTPUT);
pinMode(fadePin, OUTPUT);
///////////////// servo
pinMode(servoPin, OUTPUT);
myservo.attach(servoPin);
//////////////// distance sensor
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
///////////////// LCD
lcd.init();
lcd.backlight();
//lcd.print("--> Distance <--");
//delay(1000);
//lcd.clear();
//////////////////////////rtc
rtc.begin();
}
void loop() {
Blink();
BottonPress();
//fade();
//servo();
//lcdsensor();
sensorservo();
//Timer();
Button();
}
void Blink() {
currentMillis = millis();
////////////////
if (currentMillis - R_previousMillis >= R_interval) {
R_previousMillis = currentMillis;
if (R_ledState == LOW) {
R_ledState = HIGH;
R_interval = 2000;
} else {
R_ledState = LOW;
R_interval = 500;
}
digitalWrite(rPin, R_ledState);
}
///////////////////
if (currentMillis - G_previousMillis >= G_interval) {
G_previousMillis = currentMillis;
if (G_ledState == LOW) {
G_ledState = HIGH;
G_interval = 2000;
} else {
G_ledState = LOW;
G_interval = 300;
}
digitalWrite(gPin, G_ledState);
}
//////////////////
if (currentMillis - B_previousMillis >= B_interval) {
B_previousMillis = currentMillis;
if (B_ledState == LOW) {
B_ledState = HIGH;
B_interval = 2000;
} else {
B_ledState = LOW;
B_interval = 100;
}
digitalWrite(bPin, B_ledState);
}
}
void BottonPress(){
currentMillis = millis();
if (currentMillis - Botton_previousMillis >= Botton_interval) {
Botton_previousMillis = currentMillis;
Botton_ledState = digitalRead(bottonPin);
if (Botton_ledState == LOW) {
Botton_ledState = HIGH;
} else {
Botton_ledState = LOW;
}
digitalWrite(bottonLedPin, Botton_ledState);
}
}
void fade(){
currentMillis = millis();
if (currentMillis - fade_previousMillis >= fade_interval) {
fade_previousMillis = currentMillis;
bright++;
analogWrite(fadePin,bright);
if (bright >= 255){
bright = 0;
}
}
}
void servo(){
currentMillis = millis();
if (currentMillis - servo_previousMillis >= servo_interval) {
servo_previousMillis = currentMillis;
pos += posIncr;
myservo.write(pos);
if (pos >=180 || pos <=0){
posIncr =- posIncr;
}
}
}
long readUltrasonicDistance(int trigPin, int echoPin) {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
return pulseIn(echoPin, HIGH);
}
/*
////////////////////////////////
float readDistanceCM() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
int duration = pulseIn(echoPin, HIGH);
return duration * 0.034 / 2;
}
void printsensor(){
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the result:
int duration = pulseIn(echoPin, HIGH);
Serial.print("Distance in CM: ");
Serial.println(duration / 58);
Serial.print("Distance in inches: ");
Serial.println(duration / 148);
delay(1000);
}
////////////////////////////////
*/
void lcdsensor(){
currentMillis = millis();
if (currentMillis - sensor_previousMillis >= sensor_interval) {
sensor_previousMillis = currentMillis;
cm = 0.0344/2 * readUltrasonicDistance(4, 3);
inches = (cm / 2.54);
lcd.setCursor(0,0);
lcd.print("Inches");
lcd.setCursor(4,0);
lcd.setCursor(12,0);
lcd.print("cm");
lcd.setCursor(1,1);
lcd.print(inches, 1);
lcd.setCursor(11,1);
lcd.print(cm, 1);
lcd.setCursor(14,1);
delay(2000);
lcd.clear();
}
}
void sensorservo(){
cm = 0.0344/2 * readUltrasonicDistance(4, 3);
if (cm >= 100){
myservo.write(90);
}
else{
myservo.write(0);
}
}
void Timer(){
currentMillis = millis();
if (currentMillis - rtc_previousMillis >= rtc_interval) {
rtc_previousMillis = currentMillis;
DateTime now = rtc.now();
lcd.setCursor(0,0);
lcd.print("Time: ");
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute(), DEC);
lcd.print(':');
lcd.print(now.second(), DEC);
lcd.print(' ');
lcd.print(' ');
lcd.print(' ');
///////////////
lcd.setCursor(0,1);
lcd.print("Date: ");
lcd.print(now.day(), DEC);
lcd.print('/');
lcd.print(now.month(), DEC);
lcd.print('/');
lcd.print(now.year(), DEC);
lcd.print(' ');
lcd.print(' ');
}
}
void Button(){
currentMillis = millis();
if (currentMillis - Botton_previousMillis >= Botton_interval) {
Botton_previousMillis = currentMillis;
Botton_ledState = digitalRead(bottonPin);
if (Botton_State == LOW) {
Botton_ledState = !Botton_ledState;
}
}
digitalWrite(bottonLedPin, Botton_ledState);
if (Botton_ledState == HIGH) {
Timer();
} else {
lcd.clear();
lcdsensor();
}
}