// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include "RTClib.h"
RTC_DS1307 rtc;
//initializing the variables
// Pin numbers on the board
int redPin1 = 15;
int yellowPin1 =2;
int greenPin1 =4;
int redPin2 = 5;
int yellowPin2 =18;
int greenPin2 =19;
int voltagePin =36;
int currentPin= 39;
// the time duration in seconds
const int MILLIS_TO_SEC = 1000;
int deadZoneDuration= 1;
int greenDuration=6;
int yellowDuration=2;
const int SIZE_OF_ARRAY = 3;
int direction1[SIZE_OF_ARRAY]={redPin1, yellowPin1, greenPin1};
int direction2[SIZE_OF_ARRAY]={redPin2, yellowPin2, greenPin2};
//The days of the week for display with RTC
char Week_days[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void trafficLight(int directionOn1, int directionOn2){
for (int i=0; i<SIZE_OF_ARRAY; i++){
if (directionOn1 == direction1[i]){
digitalWrite( direction1[i], HIGH);
}
else{
digitalWrite(direction1[i], LOW);
}
if (directionOn2== direction2[i]){
digitalWrite( direction2[i], HIGH);
}
else{
digitalWrite(direction2[i], LOW);
}
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
for (int i=0; i<SIZE_OF_ARRAY; i++){
pinMode(direction1[i], OUTPUT);
pinMode(direction2[i], OUTPUT);
//Initialising the states
digitalWrite(direction1[i], LOW);
digitalWrite(direction2[i], LOW);
}
// To check connection
#ifndef ESP32
while (!Serial); // wait for serial port to connect. Needed for native USB
#endif
}
void loop() {
// function for the traffic light which takes to arguments where the first argument
trafficLight(redPin1, greenPin2);
delay(greenDuration * MILLIS_TO_SEC);
trafficLight(redPin1, yellowPin2);
delay(yellowDuration * MILLIS_TO_SEC);
trafficLight(redPin1, redPin2);
delay(deadZoneDuration * MILLIS_TO_SEC);
// function for the traffic light which takes to arguments where the first argument
trafficLight(greenPin1, redPin2);
delay(greenDuration * MILLIS_TO_SEC);
trafficLight(yellowPin1, redPin2);
delay(yellowDuration * MILLIS_TO_SEC);
trafficLight(redPin1, redPin2);
delay(deadZoneDuration * MILLIS_TO_SEC);
}
/*
void setup1() {
// put your setup code here, to run once:
Serial.begin(115200);
for (int i=0; i<SIZE_OF_ARRAY; i++){
pinMode(direction1[i], OUTPUT);
pinMode(direction2[i], OUTPUT);
//Initialising the states
digitalWrite(direction1[i], LOW);
digitalWrite(direction2[i], LOW);
}
// To check connection
#ifndef ESP32
while (!Serial); // wait for serial port to connect. Needed for native USB
#endif
// Initializing RTC module
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
// Aligning time according to computer base time
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop1() {
// put your main code here, to run repeatedly:
// calling the functions
!
dtRtc(); // function for the rtc
sensorReading(); // function for the voltage current sensor
trafficLight(); // function for the traffic light
}
//Functions
// To show the operation of the traffic light
void trafficLight()
{
? // turning on the ligts for the two traffic lights
for(int now=0; now<duration1;now++){
digitalWrite(greenPin1, HIGH); // turn on green light for first traffic light
digitalWrite(redPin2, HIGH); // turn on red light for second traffic light
delay(1000);
}
digitalWrite(greenPin1, LOW); // turn off green light for first traffic light
digitalWrite(redPin2, LOW); // turn off red light for second traffic light
for(int now=0; now<duration2;now++){
digitalWrite(yellowPin1, HIGH); // turn on yellow light for first traffic light
digitalWrite(redPin2, HIGH); // turn on red light for second traffic light
delay(1000);
}
digitalWrite(yellowPin1,LOW); // turn off yellow light for first traffic light
digitalWrite(redPin2, LOW); // turn off red light for second traffic light
for(int now=0; now<duration1;now++) {
digitalWrite(redPin1, HIGH); // turn on red light for first traffic light
digitalWrite(greenPin2, HIGH); // turn on green light for second traffic light
delay(1000);
}
digitalWrite(redPin1, LOW); // turn off red light for first traffic light
digitalWrite(greenPin2,LOW ); // turn off green light for second traffic light
for(int now=0; now<duration2;now++){
digitalWrite(redPin1, HIGH); // turn on red light for first traffic light
digitalWrite(yellowPin2, HIGH); // turn on yellow light for second traffic light
delay(1000);
}
digitalWrite(redPin1, LOW); // turn off red light for first traffic light
digitalWrite(yellowPin2, LOW); // turn off yellow light for second traffic light
for(int now=0; now<duration1;now++){
digitalWrite(greenPin1, HIGH); // turn on green light for first traffic light
digitalWrite(redPin2, HIGH); // turn on red light for second traffic light
delay(1000);
}
digitalWrite(greenPin1, LOW); // turn off green light for first traffic light
digitalWrite(redPin2, LOW); // turn off red light for second traffic light
}
void dtRtc (){
//RTC code to set date, time , day , month and year.
// Acquiring current date and time for storage
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(Week_days[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
}
void sensorReading(){
// Taking the analog reading of the sensor
// Reading voltage value
float voltageValue = analogRead(voltagePin);
Serial.println(voltageValue);
Serial.print('V');
Serial.println();
float voltage= voltageValue/ (4095 * 16.5); // converting the reading into 0v-
Serial.println(voltage);
Serial.print('V');
Serial.println();
//delay(10);
// Reading current value
float currentValue = analogRead(currentPin);
Serial.println(currentValue);
Serial.print('A');
Serial.println();
float current= currentValue/ (4095/3.0); // converting the reading into 0A -
Serial.println(current);
Serial.print('A');
Serial.println();
// delay(10);
//Calaculation for power
float power = current * voltage ;
Serial.println( power);
Serial.print('Watts');
Serial.println();
}
//*/