Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* 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: **SD Button**
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2025-05-08 05:18:42
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* write a code in which sd card store the value from */
- /* HMI and rewrite the value as change happens */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <SPI.h>
- #include <SdFat.h> //https://github.com/greiman/SdFat
- #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void writeValueToSDCard(int value);
- void readValueFromSDCard();
- void buttonPressed();
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t myPushButton_PushButton_PIN_D4 = 4;
- /***** DEFINITION OF SPI PINS *****/
- const uint8_t sd__SDCardModule_SPI_PIN_MOSI_D23 = 23;
- const uint8_t sd__SDCardModule_SPI_PIN_MISO_D19 = 19;
- const uint8_t sd__SDCardModule_SPI_PIN_SCLK_D18 = 18;
- const uint8_t sd__SDCardModule_SPI_PIN_CS_D5 = 5;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- // Create an instance of the SD card
- SdFat SD;
- File dataFile;
- // Create an instance of EasyButton
- EasyButton button(myPushButton_PushButton_PIN_D4);
- /****** VARIABLE TO STORE VALUE *****/
- int storedValue = 0;
- void setup(void)
- {
- // put your setup code here, to run once:
- pinMode(myPushButton_PushButton_PIN_D4, INPUT_PULLUP);
- pinMode(sd__SDCardModule_SPI_PIN_CS_D5, OUTPUT);
- SPI.begin();
- // Initialize the SD card
- if (!SD.begin(sd__SDCardModule_SPI_PIN_CS_D5)) {
- Serial.println("SD Card initialization failed!");
- return;
- }
- Serial.println("SD Card initialized.");
- // Initialize the button
- button.begin();
- button.onPressed(buttonPressed);
- // Read the initial value from the SD card
- readValueFromSDCard();
- }
- void loop(void)
- {
- // put your main code here, to run repeatedly:
- button.read();
- }
- // Function to write value to SD card
- void writeValueToSDCard(int value) {
- dataFile = SD.open("value.txt", FILE_WRITE);
- if (dataFile) {
- dataFile.println(value);
- dataFile.close();
- Serial.print("Value written to SD Card: ");
- Serial.println(value);
- } else {
- Serial.println("Error opening file for writing.");
- }
- }
- // Function to read value from SD card
- void readValueFromSDCard() {
- dataFile = SD.open("value.txt");
- if (dataFile) {
- storedValue = dataFile.parseInt();
- dataFile.close();
- Serial.print("Value read from SD Card: ");
- Serial.println(storedValue);
- } else {
- Serial.println("Error opening file for reading.");
- }
- }
- // Function called when the button is pressed
- void buttonPressed() {
- storedValue++; // Increment the value
- writeValueToSDCard(storedValue); // Write the new value to the SD card
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement