Advertisement
JonD1988

sdPBSaveRev0

May 20th, 2022
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Rev 0 - This program monitors the number of times a pushbutton has been pressed and stores the pushbutton count in a text file on an SD card module.  This uses an ESP32 Node MCU.  The wiring is per Reference 2.  The example used to make this possible was the SD card portions of the datalogger shown in Reference 1 and a program I made from that called esp32SDCardInt.ino
  2.  
  3. // Libraries for SD card
  4. #include "FS.h"
  5. #include "SD.h"
  6. #include <SPI.h>
  7.  
  8. // Define CS pin for the SD card module
  9. #define SD_CS 5
  10.  
  11. const int pbPin1 = 12;   // GPIO pin used to connect pushbutton 1 (digital in) Labeled G12 on board
  12. int pb1State, pb1Cnt=0;   //pb1State≡variable to store the state of Pushbutton 1. pb1Cnt≡variable to store number of times pushbutton 1 has been pressed
  13.  
  14. String dataMessage, messageHeader="Pushbutton 1 has been pressed the following number of times:";
  15.  
  16. void setup() {
  17.  
  18.   Serial.begin(115200);
  19.   pinMode(pbPin1,INPUT_PULLUP); //Configure pin tied to Pushbutton 1 as an input and enable the internal pull-up resistor - sets output HIGH so that when button is pressed reading is LOW - tie input between digital pin and ground
  20.  
  21.   // Initialize SD card
  22.   SD.begin(SD_CS);  
  23.   if(!SD.begin(SD_CS)) {
  24.     Serial.println("Card Mount Failed");
  25.     return;
  26.   }
  27.   uint8_t cardType = SD.cardType();
  28.   if(cardType == CARD_NONE) {
  29.     Serial.println("No SD card attached");
  30.     return;
  31.   }
  32.   Serial.println("Initializing SD card...");
  33.   if (!SD.begin(SD_CS)) {
  34.     Serial.println("ERROR - SD card initialization failed!");
  35.     return;    // init failed
  36.   }//End of SD Card Initialization
  37.  
  38.   // If the data.txt file doesn't exist
  39.   // Create a file on the SD card and write the first line
  40.   File file = SD.open("/pushButton1Data.txt");
  41.   if(!file) {
  42.     Serial.println("File doens't exist");
  43.     Serial.println("Creating file...");
  44.     writeFile(SD, "/pushButton1Data.txt", "This file stores the count of pushbutton 1 each time pb1 is pressed. \r\n");
  45.   }
  46.   else {
  47.     Serial.println("File already exists");  
  48.   }
  49.   file.close();
  50.  
  51. }//End of void setup
  52.  
  53. void loop() {
  54.   pb1State = digitalRead(pbPin1); //Read the value of Pushbutton 1 (value of either 0 or 1)
  55.   delay(20); //Debouncing Delay
  56.  
  57.   if (pb1State==LOW)
  58.   {
  59.   pb1Cnt+=1; //Increment the counter by 1 when pushbutton 1 has been pressed
  60.   delay(200); //Gives user time to release the button - don't make it a long press
  61.   logSDCard(pb1Cnt); //Call the logSDCard function
  62.   }
  63. } //End of void loop
  64.  
  65. //Function Definitions
  66. // Write the integer message on the SD card
  67. void logSDCard(int pb1Cnt) {
  68.   dataMessage = String(messageHeader) + " " + String(pb1Cnt) + "\r\n";
  69.   Serial.print("Save data: ");
  70.   Serial.println(dataMessage);
  71.   appendFile(SD, "/pushButton1Data.txt", dataMessage.c_str());
  72. }
  73.  
  74. // Write to the SD card (DON'T MODIFY THIS FUNCTION)
  75. void writeFile(fs::FS &fs, const char * path, const char * message) {
  76.   Serial.printf("Writing file: %s\n", path);
  77.  
  78.   File file = fs.open(path, FILE_WRITE);
  79.   if(!file) {
  80.     Serial.println("Failed to open file for writing");
  81.     return;
  82.   }
  83.   if(file.print(message)) {
  84.     Serial.println("File written");
  85.   } else {
  86.     Serial.println("Write failed");
  87.   }
  88.   file.close();
  89. }
  90.  
  91. // Append data to the SD card (DON'T MODIFY THIS FUNCTION)
  92. void appendFile(fs::FS &fs, const char * path, const char * message) {
  93.   Serial.printf("Appending to file: %s\n", path);
  94.  
  95.   File file = fs.open(path, FILE_APPEND);
  96.   if(!file) {
  97.     Serial.println("Failed to open file for appending");
  98.     return;
  99.   }
  100.   if(file.print(message)) {
  101.     Serial.println("Message appended");
  102.   } else {
  103.     Serial.println("Append failed");
  104.   }
  105.   file.close();
  106. }
  107.  
  108. //References
  109. //Reference 1- https://randomnerdtutorials.com/esp32-data-logging-temperature-to-microsd-card/
  110. //Reference 2- https://randomnerdtutorials.com/esp32-microsd-card-arduino/
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement