Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //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
- // Libraries for SD card
- #include "FS.h"
- #include "SD.h"
- #include <SPI.h>
- // Define CS pin for the SD card module
- #define SD_CS 5
- 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
- String dataMessage, messageHeader="Pushbutton 1 has been pressed the following number of times:";
- 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 SD card
- SD.begin(SD_CS);
- if(!SD.begin(SD_CS)) {
- Serial.println("Card Mount Failed");
- return;
- }
- uint8_t cardType = SD.cardType();
- if(cardType == CARD_NONE) {
- Serial.println("No SD card attached");
- return;
- }
- Serial.println("Initializing SD card...");
- if (!SD.begin(SD_CS)) {
- Serial.println("ERROR - SD card initialization failed!");
- return; // init failed
- }//End of SD Card Initialization
- // If the data.txt file doesn't exist
- // Create a file on the SD card and write the first line
- File file = SD.open("/pushButton1Data.txt");
- if(!file) {
- Serial.println("File doens't exist");
- Serial.println("Creating file...");
- writeFile(SD, "/pushButton1Data.txt", "This file stores the count of pushbutton 1 each time pb1 is pressed. \r\n");
- }
- else {
- Serial.println("File already exists");
- }
- file.close();
- }//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
- 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
- logSDCard(pb1Cnt); //Call the logSDCard function
- }
- } //End of void loop
- //Function Definitions
- // Write the integer message on the SD card
- void logSDCard(int pb1Cnt) {
- dataMessage = String(messageHeader) + " " + String(pb1Cnt) + "\r\n";
- Serial.print("Save data: ");
- Serial.println(dataMessage);
- appendFile(SD, "/pushButton1Data.txt", dataMessage.c_str());
- }
- // Write to the SD card (DON'T MODIFY THIS FUNCTION)
- void writeFile(fs::FS &fs, const char * path, const char * message) {
- 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();
- }
- // Append data to the SD card (DON'T MODIFY THIS FUNCTION)
- void appendFile(fs::FS &fs, const char * path, const char * message) {
- 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();
- }
- //References
- //Reference 1- https://randomnerdtutorials.com/esp32-data-logging-temperature-to-microsd-card/
- //Reference 2- https://randomnerdtutorials.com/esp32-microsd-card-arduino/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement