Advertisement
pleasedontcode

**SD Button** rev_01

May 8th, 2025
236
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: **SD Button**
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-05-08 05:18:42
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* write a code in which sd card store the value from */
  21.     /* HMI and rewrite the value as change happens */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /* START CODE */
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <SPI.h>
  28. #include <SdFat.h>  //https://github.com/greiman/SdFat
  29. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void writeValueToSDCard(int value);
  35. void readValueFromSDCard();
  36. void buttonPressed();
  37.  
  38. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  39. const uint8_t myPushButton_PushButton_PIN_D4        = 4;
  40.  
  41. /***** DEFINITION OF SPI PINS *****/
  42. const uint8_t sd__SDCardModule_SPI_PIN_MOSI_D23     = 23;
  43. const uint8_t sd__SDCardModule_SPI_PIN_MISO_D19     = 19;
  44. const uint8_t sd__SDCardModule_SPI_PIN_SCLK_D18     = 18;
  45. const uint8_t sd__SDCardModule_SPI_PIN_CS_D5        = 5;
  46.  
  47. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  48. // Create an instance of the SD card
  49. SdFat SD;
  50. File dataFile;
  51.  
  52. // Create an instance of EasyButton
  53. EasyButton button(myPushButton_PushButton_PIN_D4);
  54.  
  55. /****** VARIABLE TO STORE VALUE *****/
  56. int storedValue = 0;
  57.  
  58. void setup(void)
  59. {
  60.     // put your setup code here, to run once:
  61.     pinMode(myPushButton_PushButton_PIN_D4, INPUT_PULLUP);
  62.     pinMode(sd__SDCardModule_SPI_PIN_CS_D5, OUTPUT);
  63.     SPI.begin();
  64.  
  65.     // Initialize the SD card
  66.     if (!SD.begin(sd__SDCardModule_SPI_PIN_CS_D5)) {
  67.         Serial.println("SD Card initialization failed!");
  68.         return;
  69.     }
  70.     Serial.println("SD Card initialized.");
  71.  
  72.     // Initialize the button
  73.     button.begin();
  74.     button.onPressed(buttonPressed);
  75.  
  76.     // Read the initial value from the SD card
  77.     readValueFromSDCard();
  78. }
  79.  
  80. void loop(void)
  81. {
  82.     // put your main code here, to run repeatedly:
  83.     button.read();
  84. }
  85.  
  86. // Function to write value to SD card
  87. void writeValueToSDCard(int value) {
  88.     dataFile = SD.open("value.txt", FILE_WRITE);
  89.     if (dataFile) {
  90.         dataFile.println(value);
  91.         dataFile.close();
  92.         Serial.print("Value written to SD Card: ");
  93.         Serial.println(value);
  94.     } else {
  95.         Serial.println("Error opening file for writing.");
  96.     }
  97. }
  98.  
  99. // Function to read value from SD card
  100. void readValueFromSDCard() {
  101.     dataFile = SD.open("value.txt");
  102.     if (dataFile) {
  103.         storedValue = dataFile.parseInt();
  104.         dataFile.close();
  105.         Serial.print("Value read from SD Card: ");
  106.         Serial.println(storedValue);
  107.     } else {
  108.         Serial.println("Error opening file for reading.");
  109.     }
  110. }
  111.  
  112. // Function called when the button is pressed
  113. void buttonPressed() {
  114.     storedValue++; // Increment the value
  115.     writeValueToSDCard(storedValue); // Write the new value to the SD card
  116. }
  117.  
  118. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement