// proj 30 LDR (solar tracker)
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include "RTClib.h"
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo myservo;
Servo myservoV;
RTC_DS1307 rtc;
int rPin = 13; // redPin
int gPin = 12; // greenPin
int bPin = 11; // bluedPin
int buttonPin = 7; // Button switch
int buttonLedPin = 2; // LED
int fadePin = 8; // PWM
int servoVPin = 5;
int servoPin = 6; // Servo
int trig = 4; // UltraSonic
int echo = 3;
int topRPin = A0;
int topLPin = A1;
int botRPin = A2;
int botLPin = A3;
unsigned long curTime = 0;
unsigned long rPre = 0;
unsigned long gPre = 0;
unsigned long bPre = 0; // bluePreviousMillis
unsigned long buttonPre = 0;
unsigned long fPre = 0; // fedePreviousMillis
unsigned long sPre = 0;
unsigned long rtcPre = 0;
int rTime = 1000;
int gTime = 500; // greenTimeinterval
int bTime = 200; // blueTimeinterval
int buttonTime = 50; // buttonTimeinterval
int fTime = 10; // fedeTimeinterval
int sTime = 15;
int rtcTime = 1000;
int rState = LOW; // redState
int gState = LOW; // greenState
int bState = LOW; // bluedState
int buttonledState = LOW; // buttonledState
int buttonState;
int bright = 0;
int pos = 0;
int posIncre = 1;
int posLimitl = 0;
int posLimith = 180;
int posV = 0;
int cen;
int valtopR;
int valtopL;
int valbotR;
int valbotL;
char daysOfTheWeek[7][12] = {"Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat"};
void setup() {
//Serial.begin(115200);
lcd.init();
lcd.backlight();
pinMode(rPin, OUTPUT);
pinMode(gPin, OUTPUT);
pinMode(bPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buttonLedPin, OUTPUT);
pinMode(fadePin, OUTPUT);
myservo.attach(servoPin);
myservoV.attach(servoVPin);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
rtc.begin();
pinMode(topRPin, INPUT);
pinMode(topLPin, INPUT);
myservo.write(90);
myservoV.write(90);
} /////////////// end setup
void loop() {
curTime = millis();
//solartrac();
time();
} // end loop
void blink() {
///////////////////////////////// red LED
if ( curTime - rPre >= rTime) {
rPre = curTime;
if (rState == LOW) {
rState = HIGH; // rTime =2000
rTime = 2000; ///
} else {
rState = LOW; // rTime =500
rTime = 500; ////
}
digitalWrite(rPin, rState);
}
///////////////////////////////// green LED
if ( curTime - gPre >= gTime) {
gPre = curTime;
if (gState == LOW) {
gState = HIGH;
gTime = 100;
} else {
gState = LOW;
gTime = 3000;
}
digitalWrite(gPin, gState);
}
///////////////////////////////// blue LED
if ( curTime - bPre >= bTime) {
bPre = curTime;
if (bState == LOW) {
bState = HIGH;
} else {
bState = LOW;
}
digitalWrite(bPin, bState);
}
}
void button() {
if ( curTime - buttonPre >= buttonTime) {
buttonPre = curTime;
buttonState = digitalRead(buttonPin); // read sw at Pin7
if (buttonState == LOW) {
buttonledState = !buttonledState;
//} else {
// buttonState = LOW;
}
digitalWrite(buttonLedPin, buttonledState); // buttonledState is Hi to LED
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;
if (pos >= 180 || pos <= 0) {
myservo.write(pos);
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 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(" ");
// delay(1000);
//lcd.clear();
}
void time() {
if ( curTime - rtcPre >= rtcTime) {
rtcPre = curTime;
DateTime now = rtc.now();
lcd.setCursor(0, 0);
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
lcd.print(" ");
lcd.print(now.day(), DEC);
lcd.print("/");
lcd.print(now.month(), DEC);
lcd.print("/");
lcd.print(now.year(), DEC);
lcd.setCursor(0, 1);
lcd.print("Time: ");
lcd.print(now.hour(), DEC);
lcd.print(":");
lcd.print(now.minute(), DEC);
lcd.print(":");
lcd.print(now.second(), DEC);
lcd.print(" ");
}
}
void solartrac(){
valtopR = analogRead(A0);
valtopL = analogRead(A1);
valbotR = analogRead(A2);
valbotL = analogRead(A3);
valtopR = map(valtopR, 0, 1016, 180, 0);
valtopL = map(valtopL, 0, 1016, 180, 0);
valbotR = map(valbotR, 0, 1016, 180, 0);
valbotL = map(valbotL, 0, 1016, 180, 0);
lcd.setCursor(0, 0);
lcd.print("Top R ");
lcd.print(valtopR);
lcd.print(" ");
lcd.print("L ");
lcd.print(valtopL);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("Bot R ");
lcd.print(valbotR);
lcd.print(" ");
lcd.print("L ");
lcd.print(valbotL);
lcd.print(" ");
if (valtopR > valtopL) {
pos++;
myservo.write(pos);
if (pos >= 180) {
pos = 180;
}
}
if (valtopL > valtopR) {
pos--;
myservo.write(pos);
if (pos <= 0) {
pos = 0;
}
}
///////////////////////////////////////////
if (valbotR > valbotL) {
posV++;
myservoV.write(posV);
if (posV >= 180) {
posV = 180;
}
}
if (valbotL > valbotR) {
posV--;
myservoV.write(posV);
if (posV <= 0) {
posV = 0;
}
}
}