/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: "RGB Control+"
	- Source Code compiled for: Arduino Mega
	- Source Code created on: 2024-03-20 02:11:10

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Change colours of the led bulb from red to yellow */
	/* to green delay 2 seconds then repeat. Each change */
	/* is to be recorded on excel saved in the sd card */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include <SPI.h>
#include <SdFat.h>

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs(void);

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t Rgb_LEDRGB_Red_PIN_D2 = 2;
const uint8_t Rgb_LEDRGB_Green_PIN_D3 = 3;
const uint8_t Rgb_LEDRGB_Blue_PIN_D4 = 4;

/***** DEFINITION OF SPI PINS *****/
const uint8_t Sdcard_SDCardModule_SPI_PIN_MOSI_D51 = 51;
const uint8_t Sdcard_SDCardModule_SPI_PIN_MISO_D50 = 50;
const uint8_t Sdcard_SDCardModule_SPI_PIN_SCLK_D52 = 52;
const uint8_t Sdcard_SDCardModule_SPI_PIN_CS_D53 = 53;

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
bool Rgb_LEDRGB_Red_PIN_D2_rawData = 0;
bool Rgb_LEDRGB_Green_PIN_D3_rawData = 0;
bool Rgb_LEDRGB_Blue_PIN_D4_rawData = 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float Rgb_LEDRGB_Red_PIN_D2_phyData = 0.0;
float Rgb_LEDRGB_Green_PIN_D3_phyData = 0.0;
float Rgb_LEDRGB_Blue_PIN_D4_phyData = 0.0;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
SdFat sd; // SdFat object
File dataFile; // File object for data logging

const int LOG_INTERVAL = 2000; // 2 seconds delay between color changes
const char FILENAME[] = "log.csv"; // Log file name

unsigned long previousMillis = 0; // Stores the previous time for color change

void setup(void)
{
  // put your setup code here, to run once:

  pinMode(Rgb_LEDRGB_Red_PIN_D2, OUTPUT);
  pinMode(Rgb_LEDRGB_Green_PIN_D3, OUTPUT);
  pinMode(Rgb_LEDRGB_Blue_PIN_D4, OUTPUT);

  pinMode(Sdcard_SDCardModule_SPI_PIN_CS_D53, OUTPUT);
  // start the SPI library:
  SPI.begin();

  // initialize the SD card
  if (!sd.begin(Sdcard_SDCardModule_SPI_PIN_CS_D53, SPI_HALF_SPEED))
  {
    // SD card initialization failed
    // Handle the error here
  }

  // Open the data file for writing
  dataFile = sd.open(FILENAME, FILE_WRITE);
  if (!dataFile)
  {
    // File opening failed
    // Handle the error here
  }
  else
  {
    // Write the header to the file
    dataFile.println("Time,Color");
  }

}

void loop(void)
{
  // put your main code here, to run repeatedly:

  unsigned long currentMillis = millis();

  // Check if it's time to change the color
  if (currentMillis - previousMillis >= LOG_INTERVAL)
  {
    // Record the color change in the log file
    recordColorChange();

    // Update the previous time
    previousMillis = currentMillis;
  }

  updateOutputs(); // Refresh output data
}

void updateOutputs()
{
  digitalWrite(Rgb_LEDRGB_Red_PIN_D2, Rgb_LEDRGB_Red_PIN_D2_rawData);
  digitalWrite(Rgb_LEDRGB_Green_PIN_D3, Rgb_LEDRGB_Green_PIN_D3_rawData);
  digitalWrite(Rgb_LEDRGB_Blue_PIN_D4, Rgb_LEDRGB_Blue_PIN_D4_rawData);
}

void recordColorChange()
{
  // Get the current time
  unsigned long currentTime = millis();

  // Change the LED bulb colors
  if (Rgb_LEDRGB_Red_PIN_D2_rawData == HIGH)
  {
    Rgb_LEDRGB_Red_PIN_D2_rawData = LOW;
    Rgb_LEDRGB_Green_PIN_D3_rawData = HIGH;
  }
  else if (Rgb_LEDRGB_Green_PIN_D3_rawData == HIGH)
  {
    Rgb_LEDRGB_Green_PIN_D3_rawData = LOW;
    Rgb_LEDRGB_Blue_PIN_D4_rawData = HIGH;
  }
  else if (Rgb_LEDRGB_Blue_PIN_D4_rawData == HIGH)
  {
    Rgb_LEDRGB_Blue_PIN_D4_rawData = LOW;
    Rgb_LEDRGB_Red_PIN_D2_rawData = HIGH;
  }

  // Write the color change to the log file
  dataFile.print(currentTime);
  dataFile.print(",");
  if (Rgb_LEDRGB_Red_PIN_D2_rawData == HIGH)
  {
    dataFile.println("Yellow");
  }
  else if (Rgb_LEDRGB_Green_PIN_D3_rawData == HIGH)
  {
    dataFile.println("Green");
  }
  else if (Rgb_LEDRGB_Blue_PIN_D4_rawData == HIGH)
  {
    dataFile.println("Blue");
  }
}
