#include <Wire.h>
#include <DS3231.h>
#include <Servo.h>
//switch on top simulates tank full sensor reading, click on it to change state in simulation
//green LED is pump
//red LED is HIGH when pump is off
// !!!!! this setup will activate relay for atleast how long the servo movement is working !!!!!!!
//Servo object
const int servoPin = 9;
Servo servo;
int angle; // servo position in degrees
int flag1; // flag for motor movement
DateTime dt; // DateTime object from DS3231 library
const int RELAY_PIN = 4; // the Arduino pin, which connects to the IN pin of relay
const int sensUP = 12; //pin activation for sensor reading (to avoid corrosion sensor is active only when needed)
const int sensor = 8; // sensor pin, switch in simulation
void relay() // relay is activated only when sensor is LOW
{
if (digitalRead(sensor) != HIGH)
{
digitalWrite(RELAY_PIN, HIGH);
}
}
//motor movement operation 0->180 and 180->0
void servo_move()
{
// scan from 0 to 180 degrees
for (angle = 0; angle < 180; angle++) {
servo.write(angle);
delay(5); // this delay controlls how fast motor will move, also needed for stability of program
}
// now scan back from 180 to 0 degrees
for (angle = 180; angle > 0; angle--) {
servo.write(angle);
delay(5); // this delay controlls how fast motor will move, also needed for stability of program
}
}
void serial_display() //to display time and flag1 in serial in simulation/debug
{
Serial.print(dt.hour()) ;
Serial.print(":") ;
Serial.print(dt.minute()) ;
Serial.print(":") ;
Serial.print(dt.second()) ;
Serial.print(" / ");
Serial.print("flag1 = ");
Serial.print(flag1);
Serial.print("\n") ;
delay(1000);
}
// one time loop run
void setup() {
flag1 = 0;
Serial.begin(115200); // needed for debuging
Serial.println("Servo World!\n"); // needed for debuging
servo.attach(servoPin);
pinMode(RELAY_PIN, OUTPUT); // initialize relay pin
digitalWrite(RELAY_PIN, HIGH); // without this line NC LED in simulation does not work, have to be tested IRL
digitalWrite(RELAY_PIN, LOW);
pinMode(sensUP, OUTPUT); // initialize pin 12, power pin for sensor
pinMode(sensor, INPUT); // sensor reading pin initialize
// Start the I2C interface
Wire.begin();
//initialize robot for angle 0
servo.write(angle); // this angle can be changed to what ever you need (instead of angle you can use value from 0 to 180)
delay(100);
}
// constant loop
void loop() {
dt = RTClib::now(); // variable dt for actual time, RTCLib::now() is special function to get present time from DS3231
serial_display(); //to display time and flag1 in serial in simulation/debug
//flag setup, can be added multiple times, but if too frequent will coz some trouble
if (dt.hour() == 8 && dt.minute() == 0 && dt.second() == 0){ // servo_move will delay time update by how long it takes to perform full servo_move, flag1 cannot be "1" for less than that, but trigger is instant
flag1 = 1;
}
if (dt.hour() == 2 && dt.minute() == 15 && dt.second() == 0){ // second timer, more can be added after this, if needed
flag1 = 1;
}
// servo/relay code, added twice, can be adjusted or servo speed can be adjusted by changing delay in servo_move at start of code
if (flag1 == 1){ //activate this code only when time is right (setup above)
digitalWrite(sensUP, HIGH); // activate sensor for reading
delay(10); // allow sensor to activate
relay(); // activate pump relay
servo_move(); // code outside of void loop, so doesn't have to be twice in code
servo_move();
flag1 = 0; // reset flag to allow set it again in future
}
if (digitalRead(sensor) == HIGH) //deactivate pump and sensor reading when tank is full
{
digitalWrite(RELAY_PIN, LOW);
digitalWrite(sensUP, LOW);
}
}