Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int pbPin1 = 12; // GPIO pin used to connect pushbutton 1 (digital in) Labeled G12 on board
- 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
- #include "FS.h"
- #include "SD.h"
- #include "SPI.h"
- //Function Definitions
- void writeFile(fs::FS &fs, const char * path, const char * message){ //To write content to a file - Reference 1
- Serial.printf("Writing file: %s\n", path);
- File file = fs.open(path, FILE_WRITE);
- if(!file){
- Serial.println("Failed to open file for writing");
- return;
- }
- if(file.print(message)){
- Serial.println("File written");
- } else {
- Serial.println("Write failed");
- }
- file.close();
- }
- void appendFile(fs::FS &fs, const char * path, const char * message){ //To append content to a file without overwriting previous content - Reference 1
- Serial.printf("Appending to file: %s\n", path);
- File file = fs.open(path, FILE_APPEND);
- if(!file){
- Serial.println("Failed to open file for appending");
- return;
- }
- if(file.print(message)){
- Serial.println("Message appended");
- } else {
- Serial.println("Append failed");
- }
- file.close();
- }
- void setup() {
- Serial.begin(115200);
- 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
- //Initialize the SD Card - See Reference 1
- if(!SD.begin())
- {
- Serial.println("Card Mount Failed");
- return;
- }
- uint8_t cardType = SD.cardType();
- if(cardType == CARD_NONE)
- {
- Serial.println("No SD card attached");
- return;
- }//End of SD Card Initialization
- }//End of void setup
- void loop() {
- pb1State = digitalRead(pbPin1); //Read the value of Pushbutton 1 (value of either 0 or 1)
- delay(20); //Debouncing Delay
- Serial.println("Pushbutton 1 State is: " + String(pb1State));
- Serial.println("The number of times Pushbutton 1 has been pressed is: " + String(pb1Cnt));
- if (pb1State==LOW)
- {
- pb1Cnt+=1; //Increment the counter by 1 when pushbutton 1 has been pressed
- delay(200); //Gives user time to release the button - don't make it a long press
- }
- String pb1CntS = String(pb1Cnt); //Converts pb1Cnt to a String
- String pb1Cntc = pb1CntS.c_str();
- if(pb1Cnt==1) //First time button is pressed create the file and save the first line with the writeFile function
- {
- writeFile(SD, "/PushbuttonTest.txt", "Pushbutton 1 has been pressed the following number of times: " + pb1Cntc);
- }
- else if (pb1Cnt>1)
- {
- appendFile(SD, "/PushbuttonTest.txt", "Pushbutton 1 has been pressed the following number of times: " + pb1Cntc + "\n");
- }
- }
- //References
- /* Reference 1
- Rui Santos
- Complete project details at https://RandomNerdTutorials.com/esp32-microsd-card-arduino/
- This sketch can be found at: Examples > SD(esp32) > SD_Test
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement