Advertisement
pleasedontcode

Boost Logger rev_01

Oct 1st, 2024
55
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: Boost Logger
  13.     - Source Code NOT compiled for: Arduino Pro Mini 5V
  14.     - Source Code created on: 2024-10-01 16:05:50
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Arduino boost controller low and high boost */
  21.     /* nextion 3 inch rotary encoder eeprom  for config */
  22.     /* digital toggel for low high low and high boost */
  23.     /* settings Boost display max boost hold and status */
  24.     /* led for low high boost and frequency for solenoid */
  25.     /* sd card for */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <SPI.h>
  30. #include <SdFat.h>  // https://github.com/greiman/SdFat
  31. #include <Nextion.h> // Include Nextion library for display handling
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36.  
  37. // Define constants for boost control
  38. const uint8_t LOW_BOOST_PIN = 2; // Digital pin for low boost toggle
  39. const uint8_t HIGH_BOOST_PIN = 3; // Digital pin for high boost toggle
  40. const uint8_t LED_LOW_PIN = 4; // LED pin for low boost indication
  41. const uint8_t LED_HIGH_PIN = 5; // LED pin for high boost indication
  42.  
  43. // Define rotary encoder pins
  44. const uint8_t ENCODER_A_PIN = 6; // Rotary encoder pin A
  45. const uint8_t ENCODER_B_PIN = 7; // Rotary encoder pin B
  46.  
  47. // Define SD card SPI pins
  48. const uint8_t kl_SDCardModule_SPI_PIN_CS_D10 = 10;
  49.  
  50. // Create instances of libraries
  51. SdFat sd; // Create an instance of the SdFat class for SD card handling.
  52. File file; // Create an instance of the File class for file operations.
  53. Nextion myNextion(8, 9); // Create an instance of the Nextion display (TX, RX pins)
  54.  
  55. // Variables to hold boost settings
  56. bool lowBoostEnabled = false;
  57. bool highBoostEnabled = false;
  58.  
  59. // Function to initialize the rotary encoder
  60. void setupEncoder() {
  61.     pinMode(ENCODER_A_PIN, INPUT);
  62.     pinMode(ENCODER_B_PIN, INPUT);
  63. }
  64.  
  65. // Function to read rotary encoder value
  66. void readEncoder() {
  67.     // Implement rotary encoder reading logic here
  68. }
  69.  
  70. // Function to update boost settings
  71. void updateBoostSettings() {
  72.     if (digitalRead(LOW_BOOST_PIN) == HIGH) {
  73.         lowBoostEnabled = !lowBoostEnabled; // Toggle low boost
  74.         digitalWrite(LED_LOW_PIN, lowBoostEnabled ? HIGH : LOW); // Update LED status
  75.     }
  76.     if (digitalRead(HIGH_BOOST_PIN) == HIGH) {
  77.         highBoostEnabled = !highBoostEnabled; // Toggle high boost
  78.         digitalWrite(LED_HIGH_PIN, highBoostEnabled ? HIGH : LOW); // Update LED status
  79.     }
  80. }
  81.  
  82. /****** SETUP FUNCTION *****/
  83. void setup(void)
  84. {
  85.     Serial.begin(9600); // Start serial communication at 9600 baud rate.
  86.  
  87.     // Initialize the chip select pin for the SD card.
  88.     pinMode(kl_SDCardModule_SPI_PIN_CS_D10, OUTPUT);
  89.     digitalWrite(kl_SDCardModule_SPI_PIN_CS_D10, HIGH); // Set CS pin high.
  90.  
  91.     // Start the SPI library.
  92.     SPI.begin();
  93.  
  94.     // Initialize the SD card.
  95.     if (!sd.begin(kl_SDCardModule_SPI_PIN_CS_D10)) {
  96.         Serial.println("SD card initialization failed!");
  97.         return; // Exit setup if SD initialization fails.
  98.     }
  99.     Serial.println("SD card initialized successfully.");
  100.  
  101.     // Initialize boost control pins
  102.     pinMode(LOW_BOOST_PIN, INPUT_PULLUP);
  103.     pinMode(HIGH_BOOST_PIN, INPUT_PULLUP);
  104.     pinMode(LED_LOW_PIN, OUTPUT);
  105.     pinMode(LED_HIGH_PIN, OUTPUT);
  106.  
  107.     // Initialize rotary encoder
  108.     setupEncoder();
  109. }
  110.  
  111. /****** LOOP FUNCTION *****/
  112. void loop(void)
  113. {
  114.     // Update boost settings based on toggle switches
  115.     updateBoostSettings();
  116.  
  117.     // Example of writing to a file on the SD card.
  118.     if (!file.open("test.txt", FILE_WRITE)) {
  119.         Serial.println("Error opening test.txt");
  120.         return; // Exit loop if file opening fails.
  121.     }
  122.  
  123.     // Write a test message to the file.
  124.     file.println("Low Boost: " + String(lowBoostEnabled) + ", High Boost: " + String(highBoostEnabled));
  125.     file.close(); // Close the file after writing.
  126.  
  127.     // Reopen the file for reading.
  128.     if (!file.open("test.txt", FILE_READ)) {
  129.         Serial.println("Error opening test.txt for reading");
  130.         return; // Exit loop if file opening fails.
  131.     }
  132.  
  133.     // Read and print the contents of the file.
  134.     Serial.println("Contents of test.txt:");
  135.     while (file.available()) {
  136.         Serial.write(file.read()); // Read and write each byte to Serial.
  137.     }
  138.     file.close(); // Close the file after reading.
  139.  
  140.     // Read rotary encoder value
  141.     readEncoder();
  142.  
  143.     // Add a delay before the next loop iteration.
  144.     delay(1000); // Delay for 1 second.
  145. }
  146.  
  147. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement