Adriuno mega is unable to reload the program after being turned off... In my project i am using dot matrix display along with 7 segment displays to display various times...Everything runs perfectly well without dot matrix... when i call dot matrix function it runs well only up-to power supply is on.. Once power supply is turned off , My code is unable to reload unless i open serial monitor or press the reset button... What is the solution for this?
Below is the code im using for running dot matrix.... THe remaining prtion of code is too big ...Problem of reloading arises only when i add this dot matrix code
// Including the required Arduino libraries
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include "JF_Font_Data.h"
#include <SPI.h>
#include <Wire.h>
#include <ErriezDS3231.h>
ErriezDS3231 rtc;//date time
//#define DATE_STRING_SHORT 3
//// Month names in flash
//const char monthNames_P[] PROGMEM = "JanFebMarAprMayJunJulAugSepOctNovDec";
//// Day of the week names in flash
//const char dayNames_P[] PROGMEM= "SunMonTueWedThuFriSat";
//char name[DATE_STRING_SHORT + 1];
uint8_t hour;
uint8_t min;
uint8_t sec;
uint8_t mday;
uint8_t mon;
uint16_t year;
uint8_t wday;
// Uncomment according to your hardware type
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
//#define HARDWARE_TYPE MD_MAX72XX::GENERIC_HW
// Defining size, and output pins
#define MAX_DEVICES 8
#define CS_PIN 53
#define DIN_PIN 51
#define CLK_PIN 52
int HijYear, HijMonth, HijDay;
// Create a new instance of the MD_Parola class with hardware SPI connection
MD_Parola myDisplay = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
#define SPEED_TIME 200 // speed of the transition
#define PAUSE_TIME 0
#define MAX_MESG 20
// Global variables
char szMnt[10]; // mm:ss\0
char szMesg[MAX_MESG+1] = "";
char szFrac[MAX_MESG+1] = "";
char szNext[MAX_MESG+1] = "";
String Hijri_Month;
String Eng_Month;
uint8_t degC[] = { 6, 3, 3, 56, 68, 68, 68 }; // Deg C
//uint8_t degF[] = { 6, 3, 3, 124, 20, 20, 4 }; // Deg F
void getNxtPrayer(char *psz)
{
const char *strFjr = "Fajir";
strcpy(psz, strFjr);
}
String getEngMonthName(long int mnth)
{
switch (mnth)
{
case 1:
return "January";
case 2:
return "February";
case 3:
return "March";
case 4:
return "April";
case 5:
return "May";
case 6:
return "June";
case 7:
return "July";
case 8:
return "August";
case 9:
return "Sptmbr";
case 10:
return "October";
case 11:
return "November";
case 12:
return "December";
}
return "";
}
void getEngMonth(char *psz, long int eng_mon)
{
String En_Mon;
En_Mon = getEngMonthName(eng_mon);
const char *strHM = En_Mon.c_str();
strcpy(szMesg, strHM);
}
// Code for English to Hijri conversion
long int LastDayOfGregorianMonth(long int mon, long int year) {
// Compute the last date of the month for the Gregorian calendar.
switch (mon) {
case 2:
if ((((year % 4) == 0) && ((year % 100) != 0))
|| ((year % 400) == 0))
return 29;
else
return 28;
case 4:
case 6:
case 9:
case 11: return 30;
default: return 31;
}
}
long int calcAbsGregorianDays(long int d, long int m, long int y) {
long int N = d;
for (long int i = m - 1; i > 0; i--)
N += LastDayOfGregorianMonth(i, y);
return N + (y - 1) * 365
+ (y - 1) / 4
- (y - 1) / 100
+ (y - 1) / 400;
}
bool IsIslamicLeapYear(long int year) {
// True if year is an Islamic leap year
if ((((11 * year) + 14) % 30) < 11)
return true;
else
return false;
}
long int LastDayOfIslamicMonth(long int mon, long int year) {
// Last day in month during year on the Islamic calendar.
if (((mon % 2) == 1) || ((mon == 12) && IsIslamicLeapYear(year)))
return 30;
else
return 29;
}
const float IslamicEpoch = 227014; // Absolute date of start of Islamic calendar
long int IslamicDate(long int mon, long int day, long int year) {
return (day // days so far this month
+ 29 * (mon - 1) // days so far...
+ mon/2 // ...this year
+ 354 * (year - 1) // non-leap days in prior years
+ (3 + (11 * year)) / 30 // leap days in prior years
+ IslamicEpoch); // days before start of calendar
}
String getMonthName(long int mnt)
{
switch (mnt)
{
case 1:
return "Muharram";
case 2:
return "Safar";
case 3:
return "Rabi-Ul-Awwal";
case 4:
return "Rabi-Us-Sani";
case 5:
return "Jamadi-Ul-Awl";
case 6:
return "Jamadi-Us-Sani";
case 7:
return "Rajab";
case 8:
return "Shaban";
case 9:
return "Ramadan";
case 10:
return "Shawal";
case 11:
return "Zil-Qadah";
case 12:
return "Zul-Hijah";
}
return "";
}
String Goergian2hijri(char* psz, long int d, long int m, long int y)
{
d = calcAbsGregorianDays(d, m, y);
long int mon, day, year;
String mnth;
// Search forward year by year from approximate year
year = (d - IslamicEpoch) / 355;
while (d >= IslamicDate(1, 1, year))
year++;
year--;
// Search forward month by month from Muharram
mon = 1;
while (d > IslamicDate(mon, LastDayOfIslamicMonth(mon, year), year))
mon++;
day = d - IslamicDate(mon, 1, year) + 1;
HijYear = year;
HijMonth = mon;
HijDay = day;
mnth = getMonthName(mon);
const char *strHM = mnth.c_str();
strcpy(szMesg, strHM);
return mnth;
//system("pause");
}
//end of conversion
void setup() {
// clock.begin();
// Initialize I2C
Wire.begin();
Wire.setClock(100000);
// Initialize RTC
while (!rtc.begin()) {
Serial.println(F("RTC not found"));
delay(3000);
}
// Set date/time: 12:34:56 31 December 2020 Sunday
if (!rtc.setDateTime(10, 39, 56, 8, 9, 2022, 4)) {
Serial.println(F("Set date/time failed"));
return;
}
// Intialize the object
myDisplay.begin(2);
myDisplay.setInvert(false);
// Set the intensity (brightness) of the display (0-15)
myDisplay.setIntensity(15);
// Clear the display
//myDisplay.displayClear();
myDisplay.setInvert(false); //we don't want to invert anything so it is set to false
Wire.begin();
//myDisplay.setZone(0, MAX_DEVICES-4, MAX_DEVICES-1);
myDisplay.setZone(0, 0, 3);
//myDisplay.setZone(1, MAX_DEVICES-4, MAX_DEVICES-1);
myDisplay.setZone(1, 4, 7);
myDisplay.setFont(0, jF_Custom);
myDisplay.setFont(1, jF_Custom);
myDisplay.displayZoneText(1, szNext, PA_LEFT, SPEED_TIME, PAUSE_TIME, PA_PRINT, PA_NO_EFFECT);
myDisplay.displayZoneText(0, szMesg, PA_CENTER, SPEED_TIME, 0, PA_PRINT , PA_NO_EFFECT);
myDisplay.addChar('$', degC);
//strcpy(szFrac, "Meqat");
//sprintf(szFrac,"Meqat");
//myDisplay.addChar('&', degF);
}
void loop() {
!rtc.getDateTime(&hour, &min, &sec, &mday, &mon, &year, &wday);
static uint8_t display = 0; // current display mode
// dt = clock.getDateTime();
long int m = mon;
long int y = year;
long int d = mday;
long int h = hour;
long int min = min;
long int s = sec;
int8_t temperature = 0;
uint8_t fraction = 0;
if (!rtc.getTemperature(&temperature, &fraction)) {
Serial.println(F("Temp read failed"));
return;
}
myDisplay.displayAnimate();
// Hijri_Month = Goergian2hijri( szMesg, d, m , y);
// Eng_Month = getEngMonthName(m);
//
//
// Serial.println(Hijri_Month);
// Serial.print(HijDay ); Serial.print( " / "); Serial.print (HijMonth); Serial.print( " / "); Serial.print(HijYear );
// Serial.println(" ");
// Serial.println(Eng_Month);
// Serial.println(" ");
if (myDisplay.getZoneStatus(0))
{
switch (display)
{
case 0: /// Next Prayer
myDisplay.setPause(0,5000);
myDisplay.setTextEffect(0, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
getNxtPrayer(szNext);
//strcpy(szNext, "As Salah Meeqat");
delay(2000);
display ++;
break;
case 1: // Temperature
//clock.forceConversion();
myDisplay.setPause(0,5000);
myDisplay.setTextEffect(0, PA_SCROLL_LEFT, PA_SCROLL_UP);
//myDisplay.setTextEffect(0, PA_MESH, PA_BLINDS);
delay(2000);
display ++;
dtostrf(temperature, 2,0, szMesg);
strcat(szMesg , ".");
dtostrf(fraction, 1,0, szFrac);
strcat(szMesg, szFrac);
strcat(szMesg, "$");
break;
case 2: // English Month
myDisplay.setTextEffect(0, PA_PRINT, PA_NO_EFFECT);
myDisplay.setPause(0, 0);
delay(2000);
display++;
getEngMonth(szMnt, m);
myDisplay.setTextEffect(0, PA_PRINT, PA_WIPE_CURSOR);
break;
myDisplay.setTextEffect(0, PA_PRINT, PA_WIPE_CURSOR);
break;
case 3: // Hijri Month
myDisplay.setTextEffect(0, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
myDisplay.setPause(0,0);
delay(2000);
display ++ ;
Hijri_Month = Goergian2hijri(szMesg, d, m , y);
break;
case 4:
//myDisplay.setPause(1,5000);
myDisplay.setTextEffect(0, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
strcpy(szMesg, "Masjid Ahl E Hadees");
myDisplay.setPause(0, 5000);
delay(2000);
display ++;
break;
default:
myDisplay.setPause(0,5000);
myDisplay.setTextEffect(0, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
strcpy(szMesg, "As Salah Meeqat");
delay(2000);
display = 0;
break;
}
myDisplay.displayReset(0);
myDisplay.displayReset(1);
}
}