I am having a problem with my Arduino freezing after a small amount of time. I'm using an RTC DS1307 breakout to keep track of the time and display the time on an LCD. It displays hours, minutes and seconds (updated every second).
Then I have 25V which is supplied by a transformer and connected to a set of LEDs through a CAT4101 constant-current sink LED driver. The Arduino board is powered by a 2amp USB transformer.
At a set time, the timer should set off the process to dim the lights.
Everything is working well if I set the lights to dim within a few minutes. However, if I leave the board on for more than 10-15 minutes then everything seems to freeze (i.e. the time on the LCD stops being updated and stays stuck at the moment it froze).
I originally thought it could have been something to do with my code. But I then skimmed everything down to the bare essential and just uploaded the code necessary to update the time on the LCD. And even in this case it freezes.
The only way I managed to avoid things freezing was to disconnect the CAT4101 pin 1 from Arduino's PWM and disconnect pin 2 from Arduino's 5V.
What could be causing this issue?
CAT4101
pin 1 = connected to Arduino PWM
pin 2 = connected to +5v
pin 3 = connected to ground
pin 4 = connected to ground with 2kOhm resistor
pin 5 = connected to LED cathode
UPDATE
Code below (I have stripped down the code by removing any additional functions and leaving only the bare code which is leading the application to crash).
#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal.h>
RTC_DS1307 rtc;
DateTime now;
LiquidCrystal lcd(7, 8, 9, 10, 11 , 12);
void setup() {
Wire.begin();
rtc.begin();
lcd.begin(16,2);
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(__DATE__, __TIME__)); // time = hh:mm:ss format, e.g. 06:01:01
}
lcd.clear();
}
void loop() {
now = rtc.now();
updateDisplay();
}
void updateDisplay(){
int h = now.hour(); // Get the hours right now and store them in an integer called h
int m = now.minute(); // Get the minutes right now and store them in an integer called m
int s = now.second(); // Get the seconds right now and store them in an integer called s
lcd.setCursor(0, 0); // Set the cursor at the column zero, upper row...
lcd.print(" The time is: "); // ...with spaces to clear characters from setting alarm.
lcd.setCursor(4, 1); // Move the cursor to column four, lower row
if (h<10){ // Add a zero, if necessary, as above
lcd.print(0);
}
lcd.print(h); // Display the current hour
lcd.setCursor(6, 1); // Move to the next column
lcd.print(":"); // And print the colon
lcd.setCursor(7, 1); // Move to the next column
if (m<10){ // Add a zero, if necessary, as above
lcd.print(0);
}
lcd.print(m); // Display the current minute
lcd.setCursor(9, 1); // Move to the next column
lcd.print(":"); // And print the colon
lcd.setCursor(10, 1); // Move to the next column
if (s<10){ // Add a zero, if necessary, as above
lcd.print(0);
}
lcd.print(s); // Display the current second
}
millis()
function if you want to avoid the rest of the program to stop. Then I'd update just the fields on the LCD that I need, not all of them every time. So compare theh
,m
ands
variables to the one you stored in memory and update them only if they differ. This will reduce the traffic on the lcd lines and reduce the possibility of a malfunction. And please,h
,m
ands
will never be greater than 255, so PLEASE usebyte
, notint