/*
  Arduino | coding-help
  ɴᴏᴛ ꜱᴋʏ — 6/23/24 at 12:58 PM
  Yuys can someone help with with my aredunio code?
  I am using arduino nano, ds3231 RTC module and MAX7219 dot matrix.
  can toy fix the code, it is not working properly plus there is also
  problem that in some period countdown timer goes in negative and
  also can you add so that when a period starts display the name
  of the subject for 6sec.
  order of period :
  English
  Science
  computer
  Social
  lunch Break
  Nepali
  c maths and last a maths
*/

#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#include <Wire.h>
#include <RTClib.h>

//#define HARDWARE_TYPE MD_MAX72XX::FC16_HW // change "FC16_HW" into "PAROLA_HW" if your display displays random pixel
#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW // change "FC16_HW" into "PAROLA_HW" if your display displays random pixel

const int MAX7219_CLK = 13;    // CLK pin of MAX7219 connected to D13 on Arduino
const int MAX7219_CS = 10;     // CS pin of MAX7219 connected to D10 on Arduino
const int MAX7219_DIN = 11;    // DIN pin of MAX7219 connected to D11 on Arduino
const int NUM_MAX7219 = 8;     // Number of MAX7219 panels

// Scrolling parameters
uint8_t scrollSpeed = 25;    // default frame delay value
textEffect_t scrollEffect = PA_SCROLL_LEFT;
textPosition_t scrollAlign = PA_LEFT;
uint16_t scrollPause = 2000; // in milliseconds

// Global message buffers shared by Serial and Scrolling functions
#define BUF_SIZE 75
char curMessage[BUF_SIZE] = { "" };
char newMessage[BUF_SIZE] = { "" };
bool newMessageAvailable = true;

// Countdown timer parameters
int startHour[] = {10, 11, 11, 12, 1, 14, 15}; // Start hour of each countdown session
int startMinute[] = {0, 0, 45, 30, 15, 0, 45}; // Start minute of each countdown session
int endHour[] = {11, 11, 12, 13, 14, 15, 16}; // End hour of each countdown session
int endMinute[] = {0, 45, 30, 15, 0, 45, 15}; // End minute of each countdown session
int numSessions = sizeof(startHour) / sizeof(startHour[0]); // Number of countdown sessions
int currentSession = -1; // Current countdown session index

//int hour;
//int minute;
//int hours;
//float temperature;
//int temp;

RTC_DS3231 rtc;
MD_Parola P = MD_Parola(HARDWARE_TYPE, MAX7219_CS, NUM_MAX7219);

void displayTimeAndTemperatureandcount() {
  int hours;
  
  DateTime now = rtc.now();
  int hour = now.hour();
  float temperature = rtc.getTemperature();
  int temp = int( temperature);
  int minute = now.minute();
  
  // Check if the current time is within any of the countdown sessions
  for (int i = 0; i < numSessions; i++) {
    if (hour == startHour[i] && minute >= startMinute[i]) {
      currentSession = i;
      break;
    }
    if (hour == endHour[i] && minute < endMinute[i]) {
      currentSession = i;
      break;
    }
    if (hour > startHour[i] && hour < endHour[i]) {
      currentSession = i;
      break;
    }
  }

  if (hour > 24) {
    hour = hour - 24;
  }
  if (hour < 1) hour = 1;
  if (hour > 12) hours = hour - 12;
  if (hours < 1) hours = 1;

  // If the current time is within a countdown session, calculate the remaining minutes and display them
  if (currentSession != -1) {
    int remainingMinutes = (endHour[currentSession] - hour) * 60 + (endMinute[currentSession] - minute);
    snprintf( newMessage, BUF_SIZE, "%2d:%02d %2dm %.2dC", hours, minute, remainingMinutes, temp);
  }
  else {
    // If the current time is not within a countdown session, display only the time and temperature
    snprintf( newMessage, BUF_SIZE, "%2d:%02d %.2dC", hours, minute, temp);
  }

  newMessageAvailable = true;
}

void setup() {
  Serial.begin(9600);
  Wire.begin();
  rtc.begin();
  // Set up Parola
  P.begin(1);
  P.setInvert(false);
  P.setZone(0, 0, NUM_MAX7219 - 1);
  P.displayZoneText(0, curMessage, PA_CENTER, scrollSpeed, scrollPause, PA_PRINT, PA_NO_EFFECT);
  //P.displayText(curMessage, scrollAlign, scrollSpeed, scrollPause, scrollEffect, scrollEffect);
}

void loop() {
  if (P.displayAnimate()) {
    if (newMessageAvailable) {
      strcpy(curMessage, newMessage);
      newMessageAvailable = false;
    }
    P.displayReset();
  }
  displayTimeAndTemperatureandcount();
}

GND5VSDASCLSQWRTCDS1307+