Hi, I have a problem with homemade solar tracker based on Arduino Nano. It works for few hours and then Nano "hangs" and doesn't work until I unplug power (LCD displays nothing or some glitches).
First of all, I've tried both original Nano, Uno and Nano clones - the same behavior. Tracker contains Nano, 20x4 LCD (JHD204A-BW, compies with HD44780), one single relay module and one dual relay module and one dpdt relay (singe relay module controls dpdt relay which changes polarity to rotating motor and dual relay module switches power to motor). There are 4 buttons, but they aren't used and were DS1302 RTC module, but it didn't worked for me (library od module were broken - either it reseted time or didn't went while unplugged). For light sensing I use 4 photoresistors (two for each direction). Whole setup is powered by 5V 3A power suplly. If you want some schematic I'll try draw something.
Here is code I use; function sprawdz_noc() checks if values (voltage) from photoresistors are smaller than thresholds (variables noc and margines), function obrot() is used to rotate panels - it read values from photoresistors, and if required switches direction, powers on motor and keep reading values until they smaller than threshold (var. roznica), then it powers off motor. Function powrot() is used to rotate panels to "full east" position and wypisz() is for printing
photoresistor values on LCD.
Rotating is done every ~15 minutes, if there isn't night and if is night and previous was day it should rotate east.
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int zach1 = 0, zach2 = 0, wsch1 = 0, wsch2 = 0;
int roznica = 30, margines = 30;
int noc = 80;
int sekundy = 1000;
bool p_noc = false;
void setup ()
{
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(13, OUTPUT);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(13, LOW);
lcd.begin(20, 4);
Serial.begin(9600);
}
void loop ()
{
lcd.setCursor(0, 2);
String ile_zostalo = "";
int minuty = (900 - sekundy) / 60;
ile_zostalo = "Zostalo: " + String(minuty) + " m " + String(900 - sekundy - (minuty * 60)) + " s";
lcd.print(ile_zostalo);
wypisz();
if (sekundy >= 900)
{
sekundy = 0;
if (!sprawdz_noc())
{
obroc();
}
else
{
lcd.setCursor(0, 4);
lcd.print("OBROT: NIE ");
if (!p_noc) powrot();
}
}
sekundy += 1;
delay(1000);
}
bool sprawdz_noc()
{
zach1 = analogRead(A7);
zach2 = analogRead(A6);
wsch1 = analogRead(A5);
wsch2 = analogRead(A4);
int za = (zach1 + zach2) / 2;
int ws = (wsch1 + wsch2) / 2;
if ((za < noc + margines) && (ws < noc + margines))
{
lcd.setCursor(0, 1);
lcd.print("Noc: " + String(noc) + " TAK");
p_noc = true;
return true;
}
else {
lcd.setCursor(0, 1);
lcd.print("Noc: " + String(noc) + " NIE");
p_noc = false;
return false;
}
}
void obroc()
{
int wsch, zach;
zach1 = analogRead(A7);
zach2 = analogRead(A6);
wsch1 = analogRead(A5);
wsch2 = analogRead(A4);
zach = (zach1 + zach2) / 2;
wsch = (wsch1 + wsch2) / 2;
int rozn = zach - wsch;
if (rozn > 0)
{
if (rozn > roznica)
{
lcd.setCursor(0, 4);
lcd.print("OBROT: ZACH");
digitalWrite(13, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
obroc();
}
else
{
digitalWrite(13, LOW);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
lcd.setCursor(0, 4);
lcd.print("OBROT: NIE ");
return;
}
}
else
{
if (rozn * (-1) > roznica)
{
lcd.setCursor(0, 4);
lcd.print("OBROT: WSCH");
digitalWrite(13, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
obroc();
}
else
{
digitalWrite(13, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
lcd.setCursor(0, 4);
lcd.print("OBROT: NIE ");
return;
}
}
}
void powrot()
{
digitalWrite(13, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
for (int i = 0; i < 180; i++)
{
delay(1000);
}
}
void wypisz()
{
zach1 = analogRead(A7);
zach2 = analogRead(A6);
wsch1 = analogRead(A5);
wsch2 = analogRead(A4);
String z1 = "", z2 = "", w1 = "", w2 = "";
if (zach1 < 10) z1 = "000" + String(zach1); else if (zach1 < 100) z1 = "00" + String(zach1); else if (zach1 < 1000) z1 = "0" + String(zach1); else z1 = String(zach1);
if (zach2 < 10) z2 = "000" + String(zach2); else if (zach2 < 100) z2 = "00" + String(zach2); else if (zach2 < 1000) z2 = "0" + String(zach2); else z1 = String(zach2);
if (wsch1 < 10) w1 = "000" + String(wsch1); else if (wsch1 < 100) w1 = "00" + String(wsch1); else if (wsch1 < 1000) w1 = "0" + String(wsch1); else w1 = String(wsch1);
if (wsch2 < 10) w2 = "000" + String(wsch2); else if (wsch2 < 100) w2 = "00" + String(wsch2); else if (wsch2 < 1000) w2 = "0" + String(wsch2); else w1 = String(wsch2);
String out = w1 + " " + w2 + " " + z1 + " " + z2;
lcd.setCursor(0, 0);
lcd.print(out);
}
And as I said before it works few times and hangs/crashes blanking LCD or displaying some garbage. Any ideas what could be wrong? Is that hardware or program related?