Neeraj Rane
Published © CC BY-NC-SA

Automated LED Lighting for Planted Aquarium

Never forget to turn ON/OFF your aquarium lights.

IntermediateShowcase (no instructions)14,021
Automated LED Lighting for Planted Aquarium

Things used in this project

Story

Read more

Schematics

Fishtank Controller

Code

Fishtank Controller.ino

C/C++
/*Automated LED Lighting for Planted Aquarium. 
 * 
 *Author: Neeraj R. Rane  
 *Please note that the time is in 24-hour format.
 *So, enter the time accordingly for proper functioning.
*/


#define OLED_RESET 4
#include <Adafruit_SSD1306.h>
#include <DS3231.h>

DS3231  rtc(SDA, SCL);

//Please note that the time is in 24-hours format. Set the time accordingly.
int ledStart = 21;       //Set the LED start hour
int ledStop = 23;        //Set the LED stop hour
int fanStart = 21;        //Set the Fan start hour
int fanStop = 23;         //Set the fan stop hour

int minutes,hours;
int potPin = A0;
int manualSwitch = 6;
int fanPin = 2;
int relay1 = 3;
int relay2 = 4;
int relay3 = 5;
int wLed = 9;
int rbLed = 10;

Adafruit_SSD1306 display(OLED_RESET);

void setup()
{
  pinMode(manualSwitch, INPUT);
  
  pinMode(wLed, OUTPUT);
  pinMode(rbLed, OUTPUT);
  pinMode(fanPin, OUTPUT);

  digitalWrite(relay1, LOW);
  digitalWrite(relay2, LOW);
  digitalWrite(relay3, LOW);
  
  rtc.begin();
  
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.display();
  delay(1000);
  display.clearDisplay();

  display.setTextColor(WHITE);
}

void loop(){
  String time_3231 = rtc.getTimeStr();
  hours = time_3231.substring(0,2).toInt();
  minutes = time_3231.substring(3,5).toInt();
  display.setTextSize(2);
  display.setCursor(32,1);
  display.print(hours);
  display.print(":");
  display.println(minutes);

  if(hours >= fanStart && hours < fanStop){
    digitalWrite(fanPin, HIGH);
  }
  else{
    digitalWrite(fanPin, LOW);
  }
  
  if(hours == ledStart){
    if(minutes >=0 && minutes <=59){
      wLedStart();
    }
    digitalWrite(rbLed, HIGH);
  }
  
  if(hours>ledStart && hours<ledStop-1){
    digitalWrite(wLed, HIGH);
    digitalWrite(rbLed, HIGH);
  }
  
  if(hours<ledStart || hours>ledStop){
    digitalWrite(wLed, LOW);
    digitalWrite(rbLed, LOW);
  }
  
  if(hours == ledStop - 1){
    if(minutes>=0 && minutes<=59){
      wLedStop();
    }
  }

  if(hours == ledStop){
    digitalWrite(rbLed, LOW);
  }
  
  if(digitalRead(manualSwitch)==HIGH){
    manualMode();
  }
  
  display.display();
  display.clearDisplay();
}//main loop ends here

void wLedStart(){
  int fade = map(minutes,0,59,1,255);
  analogWrite(wLed, fade);
}

void wLedStop(){
  int fade = map(minutes,0,59,255,0);
  analogWrite(wLed, fade);
}

void manualMode(){
  digitalWrite(rbLed, HIGH);
  while(digitalRead(manualSwitch)==HIGH){
    display.setTextSize(1);
    display.setCursor(0,20);
    display.println("Manual Mode");
    display.display();
    int brightness = analogRead(potPin);
    brightness = map(brightness,0,1023,0,255);
    analogWrite(wLed, brightness);
  }
  digitalWrite(wLed, LOW);
  digitalWrite(rbLed, LOW);
}

Credits

Neeraj Rane

Neeraj Rane

18 projects • 46 followers
Electrical Engineer and a Maker from India. Engineering is fun once you start applying it!

Comments