Advertisement
pleasedontcode

"Sensor Recorder" rev_01

Dec 17th, 2023
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "Sensor Recorder"
  13.     - Source Code compiled for: Arduino Nano
  14.     - Source Code created on: 2023-12-17 18:08:11
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* record sensor data and save in sd card */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /****** DEFINITION OF LIBRARIES *****/
  24. #include <Arduino.h>
  25. #include <SPI.h>
  26. #include <SdFat.h>
  27. #include <EasyButton.h>
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32.  
  33. /**** DEFINITION OF DIGITAL INPUT PINS *****/
  34. const uint8_t SENSOR_PIN = A0;
  35. const uint8_t BUTTON_PIN = 2;
  36. const uint8_t SD_CS_PIN = 10;
  37.  
  38. /****** DEFINITION OF LIBRARY CLASS INSTANCES*****/
  39. SdFat sd;
  40. File dataFile;
  41. EasyButton button(BUTTON_PIN);
  42.  
  43. void setup(void)
  44. {
  45.   // Initialize Serial communication
  46.   Serial.begin(9600);
  47.  
  48.   // Initialize the button
  49.   button.begin();
  50.  
  51.   // Initialize the SD card
  52.   if (!sd.begin(SD_CS_PIN, SPI_FULL_SPEED))
  53.   {
  54.     Serial.println("SD initialization failed!");
  55.     while (1);
  56.   }
  57.  
  58.   // Open the data file for writing
  59.   dataFile = sd.open("data.txt", FILE_WRITE);
  60.   if (!dataFile)
  61.   {
  62.     Serial.println("Error opening data file!");
  63.     while (1);
  64.   }
  65. }
  66.  
  67. void loop(void)
  68. {
  69.   // Check if the button is pressed
  70.   if (button.read())
  71.   {
  72.     // Record sensor data and save to SD card
  73.     int sensorData = analogRead(SENSOR_PIN);
  74.     dataFile.println(sensorData);
  75.     Serial.println("Data recorded and saved to SD card.");
  76.   }
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement