Arduino fishtank light control

Here is how the final product looks like:

 

The required parts are:

– an Arduino Uno board;
– a LCD shield;
– a relay;
– two brick buttons;
– cables.

The case is optional, but here is the 3D model if you want to 3D print it:
http://www.thingiverse.com/thing:2008460

We will connect our fishtank’s lamp to a relay, that we will turn on and off at certain times a day.
In my case, I turn the light on at 9am and turn it off at 10pm.

Here is the code:

 

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

unsigned long previousTime = 0;
byte s = 00;
byte m = 00;
byte h = 12;

int clockSetupMode = 0;//0=no setup, 1=hour, 2=minutes

int relayOne = 8;//pin for relay
int relayWasHit = 0;

void setup() {
Serial.begin(9600);
lcd.begin(16, 2);

pinMode(7, INPUT);// button selection time
pinMode(5, INPUT);// button change value
pinMode(relayOne, OUTPUT);//relay

}

void loop() {

if (millis() >= (previousTime))
{
previousTime = previousTime + 1000; // change this to a lower value if  the clock starts falling behind the actual time
s = s +1;

//seconds ———————————-
if (s >= 60)
{
s = 0;
m = m +1;
}

lcd.setCursor(6, 0);
if(s > 9)
{
lcd.print(s);
}else{
lcd.print(0);
lcd.setCursor(7, 0);
lcd.print(s);
}

//minutes ———————————-
if (m >= 60)
{
m = 0;
h = h +1;
relayWasHit = 0;
}

lcd.setCursor(3, 0);
if(m > 9)
{
lcd.print(m);
}else{
lcd.print(0);
lcd.setCursor(4, 0);
lcd.print(m);
}
lcd.setCursor(5, 0);
lcd.print(“:”);

//hours ———————————-
if (h >= 24)
{
h = 0;
}

lcd.setCursor(0, 0);
if(h > 9)
{
lcd.print(h);
}else{
lcd.print(0);
lcd.setCursor(1, 0);
lcd.print(h);
}

lcd.setCursor(2, 0);
lcd.print(“:”);
}

//button time mode
int valBtnMode = digitalRead(7);
int valBtnChange = digitalRead(5);

if(valBtnMode == 1)
{
if (millis()+1000 >= (previousTime))
{
clockSetupMode++;
if(clockSetupMode > 2){clockSetupMode = 0;}
}

}

if(clockSetupMode == 0)//we don’t change anything
{
lcd.setCursor(0, 1);
lcd.print(” “);
}

if(clockSetupMode == 1)//we change the hour
{
lcd.setCursor(0, 1);
lcd.print(“^^ “);

if(valBtnChange == 1)
{
h++;
relayWasHit = 0;
}
}

if(clockSetupMode == 2)//we change the minute
{
lcd.setCursor(0, 1);
lcd.print(” ^^ “);

if(valBtnChange == 1)
{
m++;
}
}

//activate or deactivate the relay
if(h>8 && h<22)
{
//relay on
lcd.setCursor(11, 0);
lcd.print(” ON “);

if(relayWasHit == 0)
{
digitalWrite(relayOne, HIGH);
Serial.println(“releu 1″);
relayWasHit = 1;
}

}else{
//relay off
lcd.setCursor(11, 0);
lcd.print(” OFF”);

if(relayWasHit == 0)
{
digitalWrite(relayOne, LOW);
Serial.println(“releu 0”);
relayWasHit = 1;
}
}
delay(1000);
}

The left button toggles the time mode (it has 3 states: change hour, change minutes and change nothing), and the right button changes the values.

Leave a Reply

Your email address will not be published. Required fields are marked *