Hi. My alarm clock sketch stops running after five or six days, so I am presuming it is a memory problem. I have cut down the original code but still have a problem. I am using a Arduino Nano Board I have put up as much of the code as I can and attach a schematic.
This is my first project so if I am doing things wrong please let me know.
[code]
//RTC
#include <RTClib.h>
RTC_DS1307 rtc;
int hourupg;
int minupg;
int yearupg;
int monthupg;
int dayupg;
//RotarySwitch
#include <RotaryEncoder.h>
RotaryEncoder encoder(5, 6);
int pos = 0;
int newPos = 0;
int AdjUp;
int AdjDn;
//The Menu
int menuCounter = 0;
int menuState = 0;
int menuLastState = 0;
int menuLive = 0;
//Debounce
#include <Bounce2.h>
Bounce MenuButton = Bounce();
Bounce AlarmSw1 = Bounce();
Bounce AlarmSw2 = Bounce();
Bounce RadioSw1 = Bounce();
Bounce RadioSw2 = Bounce();
Bounce EndButton = Bounce();
//SwitchLogic
int MenuPin = 7;
int AlarmS1Pin = 11;
int AlarmS2Pin = 10;
int RadioS1Pin = 9;
int RadioS2Pin = 8;
int EndPin = 4;
int RelayPin = 12;
int Alarm1;
int SetAlarm1;
int AlarmSw1State;
int AlarmSw2State;
int RadioSw1State;
int RadioSw2State;
int EndButtonState;
int adjTime;
int adjAlarm1;
int relay;
int RelayState;
int LastRelayState;
int LastAlarm1State;
int Alarm1State;
//Alarms
int Alarm1Minupg;
int Alarm1Hourupg;
int displayMinupg;
int displayHourupg;
//Display
#include "Adafruit_LEDBackpack.h"
Adafruit_7segment Display = Adafruit_7segment();
int setBrightness ;
int displayValue;
int brightnessAdj;
//-------------------------------------------------------------Setup------
void setup() {
//rtc.adjust(DateTime(2020,12,21,11,50,0));
//Serial.begin(9600);
MenuButton.attach( MenuPin, INPUT );
MenuButton.interval(5);
AlarmSw1.attach( AlarmS1Pin, INPUT );
AlarmSw1.interval(5);
AlarmSw2.attach( AlarmS2Pin, INPUT );
AlarmSw2.interval(5);
RadioSw1.attach( RadioS1Pin, INPUT );
RadioSw1.interval(5);
RadioSw2.attach( RadioS2Pin, INPUT );
RadioSw2.interval(5);
EndButton.attach( EndPin, INPUT );
EndButton.interval(5);
pinMode (RelayPin, OUTPUT);
digitalWrite(RelayPin, LOW);
Display.begin (0x70);
Alarm1Minupg = rtc.readnvram(0);
Alarm1Hourupg = rtc.readnvram(1);
// Serial.println(rtc.readnvram(0));
// Serial.println(rtc.readnvram(1));
// Serial.println(rtc.readnvram(2));
// Serial.println(rtc.readnvram(3));
} //End Setup
//--------------------------------------------------------------Loop-----------
void loop() {
Rtc();
Glow();
SwitchLogic();
RotarySwitch();
Clock();
RelayControl();
encoder.tick();
Menu();
MenuButton.update();
AlarmSw1.update();
AlarmSw2.update();
RadioSw1.update();
RadioSw2.update();
EndButton.update();
} //End loop
//----------------------------------------------------------RelayControl------
void RelayControl() {
if ( SetAlarm1 == HIGH && minupg == Alarm1Minupg && hourupg == Alarm1Hourupg) {
RelayState = HIGH;// This goes high once at alarm time for 1 minute
}
else {
RelayState = LOW;
}
if (RelayState != LastRelayState) { // A new state Relay High only for 1 loop
if (RelayState == HIGH) {
relay = HIGH ; // used to enable alarm to be reset during first minute
}
}
LastRelayState = RelayState;
if (relay == HIGH && EndButtonState == LOW ) {
relay = HIGH; //Relay stays high untill endbutton operated
digitalWrite (RelayPin, HIGH);
}
else {
relay = LOW;
digitalWrite (RelayPin, LOW);
}
} // End Relay Control
//-------------------------------------------------------Switch Logic---------
void SwitchLogic() {
AlarmSw1State = AlarmSw1.read();
AlarmSw2State = AlarmSw2.read();
RadioSw1State = RadioSw1.read();
RadioSw2State = RadioSw2.read();
EndButtonState = EndButton.read();
if (RadioSw1State == LOW) { //Relay energised always
relay = HIGH;
}
if (RadioSw2State == LOW ) { //Radio switch in the Alarm position
SetAlarm1 = HIGH; //So alarm can be set
}
else {
SetAlarm1 = LOW;
}
if (RadioSw1State == HIGH && RadioSw2State == HIGH) {
adjTime = HIGH; //Radio switch in middle position so Time can be adjusted
}
else {
adjTime = LOW;
}
if (AlarmSw1State == HIGH && AlarmSw2State == HIGH) {
adjAlarm1 = HIGH; //Alarm switch in middle position so Alarm can be adjusted
}
else {
adjAlarm1 = LOW;
}
if (EndButtonState == HIGH) {
menuLive = LOW;
}
} //EndSwitchLogic
//------------------------------------------------------Menu----------------
void Menu() {
if (MenuButton.read() == LOW) { //Press of Menu Button
menuState = HIGH;
menuLive = HIGH;
}
else {
menuState = LOW;
}
if (menuState != menuLastState) { //menuState goes high on button press
if (menuState == LOW) { //When buton released, steps
if (menuCounter < 3) {
menuCounter++;
}
}
}
menuLastState = menuState; // sets the lastState to the state
if (menuLive == LOW) { //Resets Menu
menuCounter = 0;
}
if (menuLive == HIGH && adjTime == HIGH) {
if (menuCounter == 2) {
SetTimeMinute();
}
if (menuCounter == 3) {
SetTimeHour();
}
}
if (menuLive == HIGH && adjAlarm1 == HIGH) {
if (menuCounter == 2) {
SetAlarm1Minute();
}
if (menuCounter == 3) {
SetAlarm1Hour();
}
}
if (EndButtonState == HIGH) {
menuLive = LOW;
}
} // End Menu
[/code]