Hi guys,
I was already writing here about my project and managed to resolve some coding problems with your help.
I am using Arduino Nano for aquarium lights dimming. It is done by stepper motor which is turning potentiometer which further regulate 1-10V T5 ballast.
My code is working few days, and than it just stops. Stepper motor is not using power from arduino 5v pin, so it shouldn't cause power problem.
Here is the code:
#include <LiquidCrystal_I2C.h>
#include <DS3231.h>
#include <Stepper.h>
DS3231 myRTC;
int count = 0;
int step = 0;
int clockSec = 0;
bool h24;
bool hPM;
bool secondState = false;
bool lastSecondState = false;
Stepper myStepper = Stepper(1050, 2, 3, 4, 5);
LiquidCrystal_I2C lcd(0x27,16,2);
void setup() {
pinMode(9, OUTPUT);
pinMode(8, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
myStepper.setSpeed(10);
Serial.begin(9600);
time_t epochNow = 1659875100UL;
Wire.begin();
/*==============================================================SET THE CLOCK==============================================================*/
/*
bool mode12 = false; // sets the clock mode, 12-hour (`true`) or 24-hour (`false`)
myRTC.setClockMode(mode12); // uploads 'false' (1) to bit 6 of register 0x02
myRTC.setSecond(5); // uploads 42 to register 0x00
myRTC.setMinute(9);
myRTC.setHour(22);
myRTC.setDoW(2); //Day of the Week
myRTC.setDate(16); //day of the month
myRTC.setMonth(1);
myRTC.setYear(24);
*/
/*==========================================================/////SET THE CLOCK==============================================================*/
lcd.init();
lcd.clear();
lcd.backlight();
}
void loop() {
if (digitalRead(8) == LOW) {
myStepper.step(1);
step++;
count = 0;
lcd.setCursor(0,0);
lcd.print(step);
}
else if(digitalRead(7) == LOW){
count = 0;
step=0;
}
else if(digitalRead(6) == LOW){
myStepper.step(-1);
step--;
count = 0;
lcd.setCursor(0,0);
lcd.print(step);
}
else {
/*===============================================================SUNRISE================================================================*/
if ((myRTC.getHour(h24,hPM) >= 8) && (myRTC.getHour(h24,hPM) < 10) ){
if(myRTC.getSecond() == 0 || myRTC.getSecond() == 5 || myRTC.getSecond() == 10 || myRTC.getSecond() == 15 || myRTC.getSecond() == 20 || myRTC.getSecond() == 25 || myRTC.getSecond() == 30 ||
myRTC.getSecond() == 40 || myRTC.getSecond() == 50 ){
secondState = true;
if(secondState != lastSecondState){
if(step < 1050){
step += 1;
myStepper.step(1);
}
else{
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
}
}
}
else{
secondState = false;
}
lastSecondState = secondState;
}
/*==========================================================/////SUNRISE================================================================*/
/*================================================================SUNSET================================================================*/
if ((myRTC.getHour(h24,hPM) >= 17) && (myRTC.getHour(h24,hPM) < 20) ){
if(myRTC.getSecond() == 0 || myRTC.getSecond() == 5 || myRTC.getSecond() == 10 || myRTC.getSecond() == 15 || myRTC.getSecond() == 20 || myRTC.getSecond() == 25 || myRTC.getSecond() == 30 ||
myRTC.getSecond() == 40 || myRTC.getSecond() == 50 ){
secondState = true;
if(secondState != lastSecondState){
if(step > 0){
step -= 1;
myStepper.step(-1);
}
else{
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
}
}
}
else{
secondState = false;
}
lastSecondState = secondState;
}
/*================================================================/////SUNSET===========================================================*/
/*================================================================DISPLAY===============================================================*/
if(myRTC.getSecond() != clockSec){
if(count == 2 || count == 5){
lcd.clear();
}
count++;
if(count == 6){
count = 0;
}
clockSec = myRTC.getSecond();
}
if(count < 3 ){
/*----------------------------------------LIGHT LEVEL---------------------------------------------*/
lcd.setCursor(3,0);
lcd.print("LIGHT LEVEL");
lcd.setCursor(1,1);
lcd.print(step);
lcd.print(" from 1050");
/*-----------------------------------/////LIGHT LEVEL---------------------------------------------*/
}
else{
/*----------------------------------------CLOCK---------------------------------------------------*/
lcd.setCursor(4,0); /*Set cursor to Row 1*/
lcd.print(myRTC.getDate()); /*print message on LCD*/
lcd.print(".");
bool CenturyBit;
lcd.print(myRTC.getMonth(CenturyBit));
lcd.print(".");
lcd.print(myRTC.getYear());
lcd.setCursor(0,1); /*set cursor on row 2*/
bool h24;
bool hPM;
lcd.print(myRTC.getHour(h24,hPM));
lcd.print(":");
lcd.print(myRTC.getMinute());
lcd.print(":");
lcd.print(myRTC.getSecond());
lcd.print(" ");
lcd.print( myRTC.getTemperature());
lcd.print("'C");
/*-----------------------------------/////CLOCK---------------------------------------------------*/
}
/*===========================================================/////DISPLAY===============================================================*/
}
}