Advertisement
pleasedontcode

"RGB Control+" rev_01

Mar 19th, 2024
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "RGB Control+"
  13.     - Source Code compiled for: Arduino Mega
  14.     - Source Code created on: 2024-03-20 02:11:10
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Change colours of the led bulb from red to yellow */
  21.     /* to green delay 2 seconds then repeat. Each change */
  22.     /* is to be recorded on excel saved in the sd card */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <SPI.h>
  27. #include <SdFat.h>
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32. void updateOutputs(void);
  33.  
  34. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  35. const uint8_t Rgb_LEDRGB_Red_PIN_D2 = 2;
  36. const uint8_t Rgb_LEDRGB_Green_PIN_D3 = 3;
  37. const uint8_t Rgb_LEDRGB_Blue_PIN_D4 = 4;
  38.  
  39. /***** DEFINITION OF SPI PINS *****/
  40. const uint8_t Sdcard_SDCardModule_SPI_PIN_MOSI_D51 = 51;
  41. const uint8_t Sdcard_SDCardModule_SPI_PIN_MISO_D50 = 50;
  42. const uint8_t Sdcard_SDCardModule_SPI_PIN_SCLK_D52 = 52;
  43. const uint8_t Sdcard_SDCardModule_SPI_PIN_CS_D53 = 53;
  44.  
  45. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  46. /***** used to store raw data *****/
  47. bool Rgb_LEDRGB_Red_PIN_D2_rawData = 0;
  48. bool Rgb_LEDRGB_Green_PIN_D3_rawData = 0;
  49. bool Rgb_LEDRGB_Blue_PIN_D4_rawData = 0;
  50.  
  51. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  52. /***** used to store data after characteristic curve transformation *****/
  53. float Rgb_LEDRGB_Red_PIN_D2_phyData = 0.0;
  54. float Rgb_LEDRGB_Green_PIN_D3_phyData = 0.0;
  55. float Rgb_LEDRGB_Blue_PIN_D4_phyData = 0.0;
  56.  
  57. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  58. SdFat sd; // SdFat object
  59. File dataFile; // File object for data logging
  60.  
  61. const int LOG_INTERVAL = 2000; // 2 seconds delay between color changes
  62. const char FILENAME[] = "log.csv"; // Log file name
  63.  
  64. unsigned long previousMillis = 0; // Stores the previous time for color change
  65.  
  66. void setup(void)
  67. {
  68.   // put your setup code here, to run once:
  69.  
  70.   pinMode(Rgb_LEDRGB_Red_PIN_D2, OUTPUT);
  71.   pinMode(Rgb_LEDRGB_Green_PIN_D3, OUTPUT);
  72.   pinMode(Rgb_LEDRGB_Blue_PIN_D4, OUTPUT);
  73.  
  74.   pinMode(Sdcard_SDCardModule_SPI_PIN_CS_D53, OUTPUT);
  75.   // start the SPI library:
  76.   SPI.begin();
  77.  
  78.   // initialize the SD card
  79.   if (!sd.begin(Sdcard_SDCardModule_SPI_PIN_CS_D53, SPI_HALF_SPEED))
  80.   {
  81.     // SD card initialization failed
  82.     // Handle the error here
  83.   }
  84.  
  85.   // Open the data file for writing
  86.   dataFile = sd.open(FILENAME, FILE_WRITE);
  87.   if (!dataFile)
  88.   {
  89.     // File opening failed
  90.     // Handle the error here
  91.   }
  92.   else
  93.   {
  94.     // Write the header to the file
  95.     dataFile.println("Time,Color");
  96.   }
  97.  
  98. }
  99.  
  100. void loop(void)
  101. {
  102.   // put your main code here, to run repeatedly:
  103.  
  104.   unsigned long currentMillis = millis();
  105.  
  106.   // Check if it's time to change the color
  107.   if (currentMillis - previousMillis >= LOG_INTERVAL)
  108.   {
  109.     // Record the color change in the log file
  110.     recordColorChange();
  111.  
  112.     // Update the previous time
  113.     previousMillis = currentMillis;
  114.   }
  115.  
  116.   updateOutputs(); // Refresh output data
  117. }
  118.  
  119. void updateOutputs()
  120. {
  121.   digitalWrite(Rgb_LEDRGB_Red_PIN_D2, Rgb_LEDRGB_Red_PIN_D2_rawData);
  122.   digitalWrite(Rgb_LEDRGB_Green_PIN_D3, Rgb_LEDRGB_Green_PIN_D3_rawData);
  123.   digitalWrite(Rgb_LEDRGB_Blue_PIN_D4, Rgb_LEDRGB_Blue_PIN_D4_rawData);
  124. }
  125.  
  126. void recordColorChange()
  127. {
  128.   // Get the current time
  129.   unsigned long currentTime = millis();
  130.  
  131.   // Change the LED bulb colors
  132.   if (Rgb_LEDRGB_Red_PIN_D2_rawData == HIGH)
  133.   {
  134.     Rgb_LEDRGB_Red_PIN_D2_rawData = LOW;
  135.     Rgb_LEDRGB_Green_PIN_D3_rawData = HIGH;
  136.   }
  137.   else if (Rgb_LEDRGB_Green_PIN_D3_rawData == HIGH)
  138.   {
  139.     Rgb_LEDRGB_Green_PIN_D3_rawData = LOW;
  140.     Rgb_LEDRGB_Blue_PIN_D4_rawData = HIGH;
  141.   }
  142.   else if (Rgb_LEDRGB_Blue_PIN_D4_rawData == HIGH)
  143.   {
  144.     Rgb_LEDRGB_Blue_PIN_D4_rawData = LOW;
  145.     Rgb_LEDRGB_Red_PIN_D2_rawData = HIGH;
  146.   }
  147.  
  148.   // Write the color change to the log file
  149.   dataFile.print(currentTime);
  150.   dataFile.print(",");
  151.   if (Rgb_LEDRGB_Red_PIN_D2_rawData == HIGH)
  152.   {
  153.     dataFile.println("Yellow");
  154.   }
  155.   else if (Rgb_LEDRGB_Green_PIN_D3_rawData == HIGH)
  156.   {
  157.     dataFile.println("Green");
  158.   }
  159.   else if (Rgb_LEDRGB_Blue_PIN_D4_rawData == HIGH)
  160.   {
  161.     dataFile.println("Blue");
  162.   }
  163. }
  164.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement