Advertisement
JonD1988

microSDPBSaveRev0

May 19th, 2022
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const int pbPin1 = 12;   // GPIO pin used to connect pushbutton 1 (digital in) Labeled G12 on board
  2. 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
  3.  
  4. #include "FS.h"
  5. #include "SD.h"
  6. #include "SPI.h"
  7.  
  8. //Function Definitions
  9. void writeFile(fs::FS &fs, const char * path, const char * message){ //To write content to a file - Reference 1
  10.   Serial.printf("Writing file: %s\n", path);
  11.  
  12.   File file = fs.open(path, FILE_WRITE);
  13.   if(!file){
  14.     Serial.println("Failed to open file for writing");
  15.     return;
  16.   }
  17.   if(file.print(message)){
  18.     Serial.println("File written");
  19.   } else {
  20.     Serial.println("Write failed");
  21.   }
  22.   file.close();
  23. }
  24.  
  25. void appendFile(fs::FS &fs, const char * path, const char * message){ //To append content to a file without overwriting previous content - Reference 1
  26.   Serial.printf("Appending to file: %s\n", path);
  27.  
  28.   File file = fs.open(path, FILE_APPEND);
  29.   if(!file){
  30.     Serial.println("Failed to open file for appending");
  31.     return;
  32.   }
  33.   if(file.print(message)){
  34.     Serial.println("Message appended");
  35.   } else {
  36.     Serial.println("Append failed");
  37.   }
  38.   file.close();
  39. }
  40.  
  41. void setup() {
  42.  
  43.   Serial.begin(115200);
  44.   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
  45.  
  46.   //Initialize the SD Card - See Reference 1
  47.   if(!SD.begin())
  48.   {
  49.   Serial.println("Card Mount Failed");
  50.   return;
  51.   }
  52.   uint8_t cardType = SD.cardType();
  53.  
  54.   if(cardType == CARD_NONE)
  55.   {
  56.     Serial.println("No SD card attached");
  57.     return;
  58.   }//End of SD Card Initialization
  59.  
  60. }//End of void setup
  61.  
  62. void loop() {
  63.   pb1State = digitalRead(pbPin1); //Read the value of Pushbutton 1 (value of either 0 or 1)
  64.   delay(20); //Debouncing Delay
  65.   Serial.println("Pushbutton 1 State is: " + String(pb1State));
  66.   Serial.println("The number of times Pushbutton 1 has been pressed is: " + String(pb1Cnt));
  67.   if (pb1State==LOW)
  68.   {
  69.   pb1Cnt+=1; //Increment the counter by 1 when pushbutton 1 has been pressed
  70.   delay(200); //Gives user time to release the button - don't make it a long press
  71.   }
  72.   String pb1CntS = String(pb1Cnt); //Converts pb1Cnt to a String
  73.   String pb1Cntc = pb1CntS.c_str();
  74.   if(pb1Cnt==1) //First time button is pressed create the file and save the first line with the writeFile function
  75.   {
  76.     writeFile(SD, "/PushbuttonTest.txt", "Pushbutton 1 has been pressed the following number of times: " + pb1Cntc);
  77.   }
  78.   else if (pb1Cnt>1)
  79.   {
  80.     appendFile(SD, "/PushbuttonTest.txt", "Pushbutton 1 has been pressed the following number of times: " + pb1Cntc + "\n");
  81.   }
  82. }
  83.  
  84. //References
  85. /* Reference 1
  86.   Rui Santos
  87.   Complete project details at https://RandomNerdTutorials.com/esp32-microsd-card-arduino/
  88.  
  89.   This sketch can be found at: Examples > SD(esp32) > SD_Test
  90. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement